magni.utils.config module¶
Module providing a robust configger class.
Routine listings¶
- Configger(object)
- Provide functionality to access a set of configuration options.
Notes
This module does not itself contain any configuration options and thus has no
access to any configuration options unlike the other config modules of magni
.
-
class
magni.utils.config.
Configger
(params, valids)[source]¶ Bases:
object
Provide functionality to access a set of configuration options.
The set of configuration options, their default values, and their validation schemes are specified upon initialisation.
Parameters: - params (dict) – The configuration options and their default values.
- valids (dict) – The validation schemes of the configuration options.
See also
magni.utils.validation
- Validation.
Notes
valids must contain the same keys as params. For each key in ‘valids’, the first value is the validation function (‘generic’, ‘levels’, or ‘numeric’), whereas the remaining values are passed to that validation function.
Examples
Instantiate Configger with the parameter ‘key’ with default value ‘default’ which can only assume string values.
>>> import magni >>> from magni.utils.config import Configger >>> valid = magni.utils.validation.validate_generic(None, 'string') >>> config = Configger({'key': 'default'}, {'key': valid})
The number of parameters can be retrieved as the length:
>>> len(config) 1
That parameter can be retrieved in a number of ways:
>>> config['key'] 'default'
>>> for key, value in config.items(): ... print('key: {!r}, value: {!r}'.format(key, value)) key: 'key', value: 'default'
>>> for key in config.keys(): ... print('key: {!r}'.format(key)) key: 'key'
>>> for value in config.values(): ... print('value: {!r}'.format(value)) value: 'default'
Likewise, the parameter can be changed in a number of ways:
>>> config['key'] = 'value' >>> config['key'] 'value'
>>> config.update({'key': 'value changed by dict'}) >>> config['key'] 'value changed by dict'
>>> config.update(key='value changed by keyword') >>> config['key'] 'value changed by keyword'
Finally, the parameter can be reset to the default value at any point:
>>> config.reset() >>> config['key'] 'default'
-
_funcs
= {'levels': <function validate_levels>, 'generic': <function validate_generic>, 'numeric': <function validate_numeric>}¶
-
__getitem__
(name)[source]¶ Get the value of a configuration parameter.
Parameters: name (str) – The name of the parameter. Returns: value (None) – The value of the parameter.
-
__len__
()[source]¶ Get the number of configuration parameters.
Returns: length (int) – The number of parameters.
-
__setitem__
(name, value)[source]¶ Set the value of a configuration parameter.
The value is validated according to the validation scheme of that parameter.
Parameters: - name (str) – The name of the parameter.
- value (None) – The new value of the parameter.
-
get
(key=None)[source]¶ Deprecated method.
See also
Configger.__getitem__()
- Replacing method.
Configger.items()
- Replacing method.
Configger.keys()
- Replacing method.
Configger.values()
- Replacing method.
-
items
()[source]¶ Get the configuration parameters as key, value pairs.
Returns: items (set-like) – The list of parameters.
-
set
(dictionary={}, **kwargs)[source]¶ Deprecated method.
See also
Configger.__setitem__()
- Replacing function.
-
update
(params={}, **kwargs)[source]¶ Update the value of one or more configuration parameters.
Each value is validated according to the validation scheme of that parameter.
Parameters: - params (dict, optional) – A dictionary containing the key and values to update. (the default value is an empty dictionary)
- kwargs (dict) – Keyword arguments being the key and values to update.