Source code for mcfine.utils

import os
import pickle
import tomllib

DEFAULT_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_DEFAULT_PATH = os.path.join(
    DEFAULT_DIR,
    "toml",
    "config_defaults.toml",
)
LOCAL_DEFAULT_PATH = os.path.join(
    DEFAULT_DIR,
    "toml",
    "local_defaults.toml",
)






[docs] def get_dict_val( val_dict, default_val_dict, table, key, logger=None, ): """Get value from a dictionary, falling to default if not present. Args: val_dict (dict): Provided dictionary of values default_val_dict (dict): Dictionary of fallback values table (str): Table key to search for key (str): Dictionary key to search for logger: Optional logger instance Returns: Dictionary value """ if table in val_dict: if key in val_dict[table]: dict_val = val_dict[table][key] return dict_val dict_val = default_val_dict[table][key] if logger is not None: logger.info(f"No {key} provided. Defaulting to {dict_val}") return dict_val
[docs] def check_overwrite( val_dict, key, ): """Check if overwrite is in a dict. Args: val_dict (dict): Dictionary of overwrites. key (str): Key to check. Returns: Bool of whether to overwrite or not. """ if "overwrites" in val_dict: if key in val_dict["overwrites"]: return val_dict["overwrites"][key] else: return False return False
[docs] def save_pkl( data, file_name, ): """Save data to a pkl file Args: data: Data to save file_name (str): file to save to """ d = os.path.dirname(file_name) if not os.path.exists(d): os.makedirs(d) with open(file_name, "wb") as f: pickle.dump(data, f)
[docs] def load_pkl( file_name, ): """Load a pkl file Args: file_name (str): file name to load Returns: Unpickled file """ with open(file_name, "rb") as f: pkl = pickle.load(f) return pkl