table_spec module

Define the base objects constraining values/types of config parameters.

class TableConfSpec(configured_object: Literal['beam', 'beam_calculator', 'beam_calculator_post', 'design_space', 'evaluators', 'files', 'plots', 'wtf'], table_entry: str, specs: Collection[KeyValConfSpec] | dict[str, Collection[KeyValConfSpec]] | dict[bool, Collection[KeyValConfSpec]], is_mandatory: bool = True, can_have_untested_keys: bool = False, selectkey_n_default: tuple[str, str | bool] | None = None, monkey_patches: dict[str, dict[str, Callable]] | dict[bool, dict[str, Callable]] | None = None)

Bases: object

Set specifications for a table, which holds several key-value pairs.

Note

This object can be subclassed for specific configuration needs, eg BeamTableConfSpec.

__init__(configured_object: Literal['beam', 'beam_calculator', 'beam_calculator_post', 'design_space', 'evaluators', 'files', 'plots', 'wtf'], table_entry: str, specs: Collection[KeyValConfSpec] | dict[str, Collection[KeyValConfSpec]] | dict[bool, Collection[KeyValConfSpec]], is_mandatory: bool = True, can_have_untested_keys: bool = False, selectkey_n_default: tuple[str, str | bool] | None = None, monkey_patches: dict[str, dict[str, Callable]] | dict[bool, dict[str, Callable]] | None = None) None

Set a table of properties. Correspond to a [table] in the .toml.

Parameters:
  • configured_object (str) – Name of the object that will receive associated parameters.

  • table_entry (str) – Name of the table in the .toml file, without brackets.

  • specs (Collection[KeyValConfSpec] | dict[str, Collection[KeyValConfSpec]] | dict[str, Collection[KeyValConfSpec]]) – The KeyValConfSpec objects in the current table. When the format of the table depends on the value of a key, provide a dictionary linking every possible table with the corresponding value.

  • is_mandatory (bool, optional) – If the current table must be provided. The default is True.

  • can_have_untested_keys (bool, optional) – If LightWin should remain calm when some keys are provided in the .toml but do not correspond to any KeyValConfSpec. The default is False.

  • selectkey_n_default (tuple[str, str | bool] | None, optional) – Must be given if specs is a dict. First value is name of the spec, second value is default value. We will look for this spec in the configuration file and select the proper Collection of KeyValConfSpec accordingly.

  • monkey_patches (dict[str, dict[str, Callable]] | dict[bool, dict[str, Callable]] | None, optional) – Same keys as specs, to override some default methods. The default is None.

_get_specs(toml_subdict: dict[str, Any] | None = None) list[KeyValConfSpec]

Get the proper list of KeyValConfSpec.

Used when we need to read the value of _selectkey_n_default in the .toml to choose precisely which configuration we should match.

Parameters:

toml_subdict (dict[str, Any] | None, optional) – The content of the toml file. We use it only if self._specs is not already a Collection. We look for the value of self._selectkey_n_default[0] and use it to select the proper table. If not provided, we fall back on a default value.

_set_specs_as_dict(toml_subdict: dict[str, Any] | None = None) None

Set the dict of specifications.

Used when we need to read the value of _selectkey_n_default in the .toml to choose precisely which configuration we should match. If toml_subdict is not provided, we use a default value.

_get_proper_spec(spec_name: str) KeyValConfSpec | None

Get the specification for the property named spec_name.

to_toml_strings(toml_subdict: dict[str, Any], original_toml_folder: Path | None = None, **kwargs) list[str]

Convert the given dict in string that can be put in a .toml.

Parameters:
  • toml_subdict (dict[str, Any]) – A dictionary corresponding to a .toml table.

  • original_toml_folder (pathlib.Path | None, optional) – Where the original .toml was; this is used to resolve paths relative to this location.

Returns:

All the .toml lines corresponding to the table under study.

Return type:

list[str]

_pre_treat(toml_subdict: dict[str, Any], **kwargs) None

Edit some values, create new ones. To call before validation.

Note

In general, the edited values will undergo the validation process.

prepare(toml_subdict: dict[str, Any], **kwargs) bool

Validate the config dict and edit some values.

_validate(toml_subdict: dict[str, Any], **kwargs) bool

Check that key-values in toml_subdict are valid.

This method is defined to keep an implementation of the original method even when validate is overriden by a monkey patch.

_post_treat(toml_subdict: dict[str, Any], **kwargs) None

Edit some values, create new ones. To call after validation.

Note

In general, the edited values will not be validated. To handle with care.

_make_paths_absolute(toml_subdict: dict[str, Any], toml_folder: Path | None = None, **kwargs) None

Transform the paths to their absolute resolved version.

_mandatory_keys_are_present(toml_keys: Collection[str]) bool

Ensure that all the mandatory parameters are defined.

generate_dummy_dict(only_mandatory: bool = True) dict[str, Any]

Generate a default dummy dict that should let LightWin work.

_apply_monkey_patches(monkey_patches: dict[str, Callable]) None

Override the base methods.

_remove_overriden_keys(specs: Collection[KeyValConfSpec]) list[KeyValConfSpec]

Remove the KeyValConfSpec objects to override.

Todo

Not Pythonic at all.