config

Configuration module for global configuration settings.

class frequent.config.Configuration(*args, **kwargs)[source]

Bases: collections.abc.MutableMapping

Configuration storage object.

This object is basically a dict with some additional bells and whistles, including:

  • The ability to access/modify items like attributes.

  • Serialize to/from strings via dumps and loads.

  • save and load to/from files.

  • Easily convert standard dict objects with to_dict and from_dict.

Examples

This object works like a dict, where settings can be retrieved and set using:

>>> config = Configuration()
>>> config['answer'] = 42
>>> config['answer']
42

Additionally, you can nest settings using the . as a seperator, for instance:

>>> config['nested.setting'] = 'value'
>>> config['nested']
{'setting': 'value'}
>>> config['nested.setting']
'value'
>>> config['nested']['setting']
'value'

Furthermore, you can work with settings as if they were attributes:

>>> config.nested.setting
'value'
>>> config.dirs.temp = '/home/doug/tmp'
>>> config['dirs.temp']
'/home/doug/tmp'
clear() → None[source]

Clears all the settings stored in this configuration.

copy() → frequent.config.Configuration[source]

Creates a copy of this configuration object.

Returns

A copy of this configuration object.

Return type

Configuration

dumps(compact: bool = True, **kwargs) → str[source]

Serializes this configuration object to a string.

The default method uses the built-in python json library to convert this configuration to a JSON string. To use another method or format override this method.

Parameters

compact (bool, optional) – Make the returned representation as compact as possible (default is True).

Returns

String-serialized representation of this configuration.

Return type

str

classmethod from_dict(data: Dict[str, Any]) → frequent.config.Configuration[source]

Creates a configuration object from the given dict.

Parameters

data (dict) – Dictionary to generate the new configuration object with.

Returns

The new configuration from the given dictionary data.

Return type

Configuration

classmethod load(path: str, **kwargs) → frequent.config.Configuration[source]

Loads a configuration object from the file path specified.

Parameters
  • path (str) – File path to load the configuration object from.

  • kwargs (optional) – Additional parameters to pass through to the loads call.

Returns

The Configuration object loaded from the path given.

Return type

Configuration

See also

loads()

classmethod loads(text: str, **kwargs) → frequent.config.Configuration[source]

Creates a new configuration from the given string data.

Parameters

text (str) – String-serialized representation to create the new object from.

Returns

The newly created configuration from the given string data.

Return type

Configuration

save(path: str, **kwargs) → None[source]

Saves this configuration object to the file path specified.

Parameters
  • path (str) – File path to save the configuration object to.

  • kwargs (optional) – Additional parameters to pass through to the dumps call.

See also

dumps()

to_dict() → Dict[str, Any][source]

Converts this configuration object to a standard dict.

Returns

Standard dict version of this configuration object.

Return type

dict

frequent.config.clear_config() → None[source]

Clears the currently-set global configuration.

frequent.config.get_config(name: str = None, default: Any = Sentinel('_MISSING')) → Any[source]

Gets the global configuration.

Parameters
  • name (str, optional) – The name of the setting to get the value for. If no name is given then the whole Configuration object is returned.

  • default (optional) – The default value to return if name is provided but the setting doesn’t exist in the global configuration.

Returns

The global configuration object or the configuration setting requested.

Return type

Configuration or object

frequent.config.load_config(path: str = None, config_cls: Type[frequent.config.Configuration] = <class 'frequent.config.Configuration'>) → None[source]

Loads the global configuration from the given file path.

Parameters
  • path (str, optional) – The file path to load the configuration file from. If this is not provided a new, empty Configuration is loaded.

  • config_cls (type, optional) – The type of Configuration to load (the default is the standard Configuration class).

frequent.config.save_config(path: str) → None[source]

Saves the current global configuration to the given file path.

Parameters

path (str) – The file path to save the configuration to.

frequent.config.set_config(name: str, value: Any) → None[source]

Sets a configuration setting.

Parameters
  • name (str) – The setting to set the value for.

  • value (object) – The value to set for the given name.

frequent.config.temp_config(**settings) → frequent.config.Configuration[source]

Gets a context with a temporary configuration.

Any changes made to the configuration via calls to set_config (or otherwise) will be made and persisted only within the context. The original configuration will be restored upon leaving the context.

Parameters

settings (optional) – Any temporary settings to set in the temporary configuration context.

Yields

Configuration – The temporary configuration object.