Skip to content

utils

utils

Utility modules.

Config

Config(config: Optional[dict] = None)

Configuration manager for FPLX.

PARAMETER DESCRIPTION
config

Configuration dictionary

TYPE: Optional[Dict] DEFAULT: None

Source code in fplx/utils/config.py
def __init__(self, config: Optional[dict] = None):
    self.config = {**self.DEFAULT_CONFIG}
    if config:
        self._update_nested(self.config, config)

get

get(key: str, default: Any = None) -> Any

Get configuration value.

PARAMETER DESCRIPTION
key

Configuration key (supports nested keys with '.')

TYPE: str

default

Default value if key not found

TYPE: Any DEFAULT: None

RETURNS DESCRIPTION
Any

Configuration value

Source code in fplx/utils/config.py
def get(self, key: str, default: Any = None) -> Any:
    """
    Get configuration value.

    Parameters
    ----------
    key : str
        Configuration key (supports nested keys with '.')
    default : Any
        Default value if key not found

    Returns
    -------
    Any
        Configuration value
    """
    keys = key.split(".")
    value = self.config

    for k in keys:
        if isinstance(value, dict) and k in value:
            value = value[k]
        else:
            return default

    return value

set

set(key: str, value: Any)

Set configuration value.

PARAMETER DESCRIPTION
key

Configuration key (supports nested keys with '.')

TYPE: str

value

Value to set

TYPE: Any

Source code in fplx/utils/config.py
def set(self, key: str, value: Any):
    """
    Set configuration value.

    Parameters
    ----------
    key : str
        Configuration key (supports nested keys with '.')
    value : Any
        Value to set
    """
    keys = key.split(".")
    config = self.config

    for k in keys[:-1]:
        if k not in config:
            config[k] = {}
        config = config[k]

    config[keys[-1]] = value

load_from_file

load_from_file(filepath: Path)

Load configuration from JSON file.

PARAMETER DESCRIPTION
filepath

Path to configuration file

TYPE: Path

Source code in fplx/utils/config.py
def load_from_file(self, filepath: Path):
    """
    Load configuration from JSON file.

    Parameters
    ----------
    filepath : Path
        Path to configuration file
    """
    with open(filepath) as f:
        file_config = json.load(f)

    self._update_nested(self.config, file_config)

save_to_file

save_to_file(filepath: Path)

Save configuration to JSON file.

PARAMETER DESCRIPTION
filepath

Path to save configuration

TYPE: Path

Source code in fplx/utils/config.py
def save_to_file(self, filepath: Path):
    """
    Save configuration to JSON file.

    Parameters
    ----------
    filepath : Path
        Path to save configuration
    """
    with open(filepath, "w") as f:
        json.dump(self.config, f, indent=2)

to_dict

to_dict() -> dict

Get configuration as dictionary.

RETURNS DESCRIPTION
Dict

Configuration dictionary

Source code in fplx/utils/config.py
def to_dict(self) -> dict:
    """
    Get configuration as dictionary.

    Returns
    -------
    Dict
        Configuration dictionary
    """
    return self.config.copy()

validate_data

validate_data(
    df: DataFrame, required_columns: list[str]
) -> bool

Validate that dataframe has required columns.

PARAMETER DESCRIPTION
df

Dataframe to validate

TYPE: DataFrame

required_columns

Required column names

TYPE: list[str]

RETURNS DESCRIPTION
bool

True if valid

Source code in fplx/utils/validation.py
def validate_data(df: pd.DataFrame, required_columns: list[str]) -> bool:
    """
    Validate that dataframe has required columns.

    Parameters
    ----------
    df : pd.DataFrame
        Dataframe to validate
    required_columns : list[str]
        Required column names

    Returns
    -------
    bool
        True if valid
    """
    missing = set(required_columns) - set(df.columns)

    if missing:
        logger.error(f"Missing required columns: {missing}")
        return False

    return True