Source code for models.utils
from colorama import Fore, Style
from typing import Optional
from pathlib import Path
import pickle
import yaml
[docs]
def print_banner(message: str):
line = "*" * len(message)
print(f"{Fore.GREEN}{Style.BRIGHT}{line}{Style.RESET_ALL}")
print(f"{Fore.GREEN}{Style.BRIGHT}{message}{Style.RESET_ALL}")
print(f"{Fore.GREEN}{Style.BRIGHT}{line}{Style.RESET_ALL}")
[docs]
def print_update(level: int=None,
message: str="--",
alert:Optional[bool]=False):
if level is not None:
if level == 1:
color = Fore.YELLOW
prefix="└"
elif level == 2:
color = Fore.CYAN
prefix=" └"
elif level == 3:
color = Fore.LIGHTBLACK_EX
prefix=" └"
elif level > 3:
color = Fore.LIGHTBLACK_EX + Style.DIM
prefix=" └─"
elif alert:
level=2
color = Fore.RED
prefix=" └ X "
else:
color = Fore.MAGENTA + Style.DIM
prefix=" ─"
print(f"{color}{prefix}> {message}{Style.RESET_ALL}")
[docs]
def save_pickle(data, file_path):
file_path = Path(file_path)
# Ensure file_path is not a directory
if file_path.is_dir():
file_path = file_path / "default.pkl" # Append a default filename if needed
file_path.parent.mkdir(parents=True, exist_ok=True)
with open(file_path, 'wb') as file:
pickle.dump(data, file)
[docs]
def load_config(config_file):
'''
This function loads the configuration file for PyPSA_BC
config_file: Path + filename of the configuration file. (i.e. ../config/config.yaml)
'''
with open(config_file, 'r') as file:
cfg = yaml.safe_load(file)
return cfg