table_spec module
Define the base objects constraining values/types of config parameters.
- class TableConfSpec(configured_object, table_entry, specs, is_mandatory=True, can_have_untested_keys=False, selectkey_n_default=None, monkey_patches=None)[source]
Bases:
objectSet specifications for a table, which holds several key-value pairs.
Note
This object can be subclassed for specific configuration needs, eg
BeamTableConfSpec.- Parameters:
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, default:True)can_have_untested_keys (
bool, default:False)selectkey_n_default (
tuple[str,str|bool] |None, default:None)monkey_patches (
dict[str,dict[str,Callable]] |dict[bool,dict[str,Callable]] |None, default:None)
- __init__(configured_object, table_entry, specs, is_mandatory=True, can_have_untested_keys=False, selectkey_n_default=None, monkey_patches=None)[source]
Set a table of properties. Correspond to a [table] in the
TOML.- Parameters:
configured_object (
Literal['beam','beam_calculator','beam_calculator_post','design_space','evaluators','files','plots','wtf']) – Name of the object that will receive associated parameters.table_entry (
str) – Name of the table in theTOMLfile, without brackets.specs (
Collection[KeyValConfSpec] |dict[str,Collection[KeyValConfSpec]] |dict[bool,Collection[KeyValConfSpec]]) – TheKeyValConfSpecobjects 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, default:True) – If the current table must be provided.can_have_untested_keys (
bool, default:False) – If LightWin should remain calm when some keys are provided in theTOMLbut do not correspond to anyKeyValConfSpec.selectkey_n_default (
tuple[str,str|bool] |None, default:None) – Must be given ifspecsis 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 properCollectionofKeyValConfSpecaccordingly.monkey_patches (
dict[str,dict[str,Callable]] |dict[bool,dict[str,Callable]] |None, default:None) – Same keys asspecs, to override some default methods.
- Return type:
None
- _selectkey_n_default
Selector used when
specsis a dictionary. Whenspecsis given as a dictionary (e.g.{ "modeA": [...], "modeB": [...] }), the configuration format depends on the value of a specific key inside theTOMLtable. This argument tellsTableConfSpecwhichTOMLkey to read and which default value to use if that key is absent. The tuple must contain:the name of the selector key (a key expected in the
TOMLtable),the default value to fall back on if the selector key is not present.
Example
specs = { "Envelope1D": envelope_1d_specs, "TraceWin": tracewin_specs, } selectkey_n_default = ("beam_calculator", "Envelope1D")
then:
the value of
toml_table["beam_calculator"]determines whetherenvelope_1d_specsortracewin_specsis used;if
"beam_calculator"is not provided in theTOML,"Envelope1D"is used.
This parameter must be provided whenever
specsis a dictionary. It must beNonewhenspecsis a flat collection.
- _get_specs(toml_table=None)[source]
Get the proper list of
KeyValConfSpec.Used when we need to read the value of
_selectkey_n_defaultin theTOMLto choose precisely which configuration we should match.
- _set_specs_as_dict(toml_table=None)[source]
Select and prepare
KeyValConfSpecused to validate this table.This method is responsible for determining which specification set applies to the current table, especially when the available specs depend on the value of a key inside the
TOMLtable (via_selectkey_n_default).The returned value is a dictionary mapping spec names to
KeyValConfSpecinstances. It performs the following steps:Determine the correct list of
KeyValConfSpecobjects by calling_get_specs(toml_table). Ifspecswas provided as a dictionary, this uses the selector key defined in_selectkey_n_defaultto choose the appropriate spec set. Ifspecsis a flat collection, that collection is returned unchanged.Apply override rules and remove any earlier specs that should be replaced (
overrides_previously_defined=True) using_remove_overriden_keys().Return the cleaned specifications as a
{spec.key: spec}dictionary.
This method is called multiple times during
TableConfSpec.prepare():once before validation, to build the spec set according to the raw
TOMLinput;once after post-treatment, to ensure the final
specs_as_dictreflects any modifications (e.g. inserted defaults, resolved paths, or monkey patches applied during spec selection).
- Parameters:
toml_table (
dict[str,Any] |None, default:None) – A table from theTOMLconfiguration file. Required only when spec selection depends on user-provided values. When omitted, default values from_selectkey_n_defaultare used.- Returns:
The active specification dictionary for this table.
- Return type:
- _get_proper_spec(spec_name)[source]
Get the specification for the property named
spec_name.- Parameters:
spec_name (
str)- Return type:
- to_toml_strings(toml_table, original_toml_folder=None, **kwargs)[source]
Convert the given dict in string that can be put in a
TOML.- Parameters:
- Returns:
All the
TOMLlines corresponding to the table under study.- Return type:
- _pre_treat(toml_table, **kwargs)[source]
Insert default values for missing keys.
You can inherit this method to perform additional pre-treating logic.
- _validate(toml_table, **kwargs)[source]
Check that key-values in
toml_tableare valid.This method is defined to keep an implementation of the original method even when
validateis overriden by a monkey patch.
- _post_treat(toml_table, **kwargs)[source]
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_table, toml_folder=None, **kwargs)[source]
Transform the paths to their absolute resolved version.
- _mandatory_keys_are_present(toml_keys)[source]
Ensure that all the mandatory parameters are defined.
- Parameters:
toml_keys (
Collection[str])- Return type:
- _remove_overriden_keys(specs)[source]
Remove the
KeyValConfSpecobjects to override.Todo
Not Pythonic at all.
- Parameters:
specs (
Collection[KeyValConfSpec])- Return type: