CodeEntropy.config.argparse module¶
Configuration and CLI argument management for CodeEntropy.
This module provides:
A declarative argument specification (ARG_SPECS) used to build an
argparse.ArgumentParser.A ConfigResolver that:
loads YAML configuration (if present),
merges YAML values with CLI values (CLI wins),
adjusts logging verbosity,
validates a subset of runtime inputs against the trajectory.
Notes:
Boolean arguments are parsed via str2bool to support YAML/CLI interop and common string forms like “true”/”false”.
- class CodeEntropy.config.argparse.ArgSpec(help: str, default: Any = None, type: Any = None, action: str | None = None, nargs: str | None = None)[source]¶
Bases:
objectArgument specification used to build an argparse parser.
- Variables:
help (str) – Help text shown in CLI usage.
default (Any) – Default value if not provided via CLI or YAML.
type (Any) – Python type for parsing (e.g., int, float, str, bool). If bool, ConfigResolver.str2bool will be used.
action (str | None) – Optional argparse action (e.g., “store_true”).
nargs (str | None) – Optional nargs spec (e.g., “+”).
- action: str | None = None¶
- default: Any = None¶
- help: str¶
- nargs: str | None = None¶
- type: Any = None¶
- class CodeEntropy.config.argparse.ConfigResolver(arg_specs: dict[str, ArgSpec] | None = None)[source]¶
Bases:
objectLoad, merge, and validate CodeEntropy configuration.
- This class provides a consistent interface for:
YAML config discovery/loading
CLI parser construction
merging YAML values with CLI values (CLI wins)
setting logging verbosity
validating trajectory-related numeric parameters
- build_parser() ArgumentParser[source]¶
Build an ArgumentParser from argument specs.
- Returns:
An argparse.ArgumentParser configured with all supported flags.
- load_config(directory_path: str) dict[str, Any][source]¶
Load the first YAML config file found in a directory.
The current behavior matches your existing workflow: - searches for
*.yamlin directory_path, - loads the first match, - returns{"run1": {}}if none found or file is empty/invalid.- Parameters:
directory_path – Directory to search for YAML files.
- Returns:
A configuration dictionary.
- resolve(args: Namespace, run_config: dict[str, Any] | None) Namespace[source]¶
Merge CLI arguments with YAML configuration and adjust logging level.
- Merge rule:
CLI explicitly-provided values take precedence.
YAML values fill in missing values.
Defaults fill in anything still unset.
- Parameters:
args – Parsed CLI arguments.
run_config – Dict of YAML values for a specific run, or None.
- Returns:
The mutated argparse.Namespace with merged values.
- Raises:
TypeError – If run_config is not a dict or None.
- static str2bool(value: Any) bool[source]¶
Convert a string or boolean input into a boolean.
- Accepts common string representations:
True values: “true”, “t”, “yes”, “1”
False values: “false”, “f”, “no”, “0”
If the input is already a boolean, it is returned as-is.
- Parameters:
value – Input value to convert.
- Returns:
The corresponding boolean.
- Raises:
argparse.ArgumentTypeError – If the input cannot be interpreted as a boolean.