deepr.config package

Submodules

deepr.config.base module

Evaluate objects from arbitrary nested dictionaries.

deepr.config.base.from_config(item)[source]

Instantiate item from config.

Raises:

ValueError – If item is not a valid config (unexpected eval method)

Return type:

Any

deepr.config.base.parse_config(config, macros=None)[source]

Fill macro parameters and references in config from macros.

Example

>>> from deepr.config import parse_config
>>> config = {"x": "$params:x", "y": 2}
>>> macros = {"params": {"x": 1}}
>>> parse_config(config, macros)
{'x': 1, 'y': 2}
Parameters:
  • config (Dict) – Config dictionary

  • macros (Dict, optional) – Dictionary of macro parameters.

Returns:

Parsed Config, without macro parameters and references.

Return type:

Dict

Raises:

ValueError – If some macro parameter in config not found in macros. If some references not found.

deepr.config.experimental module

Experimental utilities for config

deepr.config.experimental.add_macro_params(config, macro, params)[source]

Add new macro parameters in config automatically.

Parameters:
  • config (Dict) – Config to modify

  • macro (str) – Name of the new macro

  • params (List[str]) – List of new parameter names

Returns:

A new config with new macro parameters

Return type:

Dict

Raises:

ValueError – If one param has no reference in config after adding new params.

deepr.config.experimental.find_values(item, keys)[source]

Find values for keys in item, if present.

Parameters:
  • item (Any) – Any item

  • keys (List[str]) – Keys whose value to retrieve in item

Returns:

Mapping of key -> value for keys found in item.

Return type:

Dict

Raises:

ValueError – If one key is found whose value is a tuple, list or dict.

deepr.config.experimental.replace_values(item, values)[source]

Replace values for dictionary keys defined in values.

WARNING: if a a key in item already has a value that is either a dict, tuple, or list, raise ValueError.

Parameters:
  • item (Any) – Config item

  • values (Dict[str, Any]) – New values

Returns:

Return type:

item whose keys in values have a new value.

Raises:

ValueError – If one key is found whose value is a tuple, list or dict.

deepr.config.experimental.to_config(obj)[source]

Experimental utility to generate config of objects

deepr.config.macros module

Helpers for macros

deepr.config.macros.assert_no_macros(item)[source]

Raises a ValueError if item has macro parameters.

Parameters:

item (Any) – Item to be checked

Raises:

ValueError – If any parameter if a macro parameter.

deepr.config.macros.fill_macros(item, macros=None)[source]

Create item whose macro params present in macros are filled.

Returns a new dictionary, tuple or list or item depending on item’s type. String params that use the macro syntax “$macro:param” are replaced by the relevant entry from macros (macros[macro][param]) ONLY IF FOUND.

WARNING: No exception is raised if a macro value is not found in macros. Use assert_no_macros() to check that a config contains no macro parameters.

Parameters:
  • item (Any) – Item to filled

  • macros (Dict[str, Dict[str, Any]], optional) – Dictionary of macros

Returns:

Return type:

item

deepr.config.macros.find_macro_params(item, macro)[source]

Find macro params in item

Return type:

List[str]

deepr.config.macros.get_macro_and_param(item)[source]

Return name of the macro and param for the item.

Example

>>> from deepr.config import get_macro_and_param
>>> get_macro_and_param("$macro:param")
('macro', 'param')
Return type:

Tuple[str, str]

deepr.config.macros.ismacro(item)[source]

True if item is a string that looks like ‘$macro:param’.

Return type:

bool

deepr.config.macros.macros_eval_order(macros=None)[source]

Resolve order of macros evaluation to account for inter macros.

Parameters:

macros (Dict, optional) – Dictionary of macros

Returns:

Return type:

List[str]

deepr.config.references module

Helpers for references

deepr.config.references.default_references(config, macros=None, macros_eval=None)[source]

Create default references from config, macros and macros_eval.

Evaluation mode for the default references is set to “skip” to avoid double evaluation of those nested references.

Return type:

Dict[str, Any]

deepr.config.references.fill_references(item, references=None)[source]

Fill all params that are references, fail if not found.

Returns a new dictionary, tuple or list or item depending on item’s type.

Parameters that use the ref syntax “@reference” are replaced by the relevant entry from references (references[‘@reference’]).

If a reference is not found in references, raise ValueError

Parameters:
  • item (Any) – Any item, but typically a Dict

  • references (Dict[str, Any], optional) – Mapping of names to reference objects

Returns:

Return type:

Any

Raises:

ValueError – If some references are not found

deepr.config.references.isreference(item)[source]

True if item is a string that looks like @reference’.

Return type:

bool

Module contents