mlpy.tools.configuration.ConfigMgr

class mlpy.tools.configuration.ConfigMgr(filename, import_modules=None, eval_key=None)[source]

Bases: object

The configuration manager.

The configuration manager provides access to configuration files (usually in JSON format) for client applications.

Parameters:

filename : str

The name of the configuration file.

import_modules : str or list[str]

Modules required by the configuration file that must be imported first.

eval_key : bool

Whether to evaluate the key. If this is True, the key will be evaluated as a statement by a call to eval.

Raises:

TypeError

If the configuration file is not read in a dictionary

Examples

Assuming there exists a file events_map.json containing the following configuration:

{
    "keyboard": {
        "down": {
            "pygame.K_ESCAPE": "QUIT",
            "pygame.K_SPACE": [-1.0],
            "pygame.K_LEFT" : [-0.004],
            "pygame.K_RIGHT":  [0.004]
        }
    }
}

The keys can be mapped to the PyGame keyboard constants, when the file is loaded by the configuration manager as follows:

>>> cfg = ConfigMgr("events_map.json", "pygame", eval_key=True)

This allows to retrieve the values for the keys in the configuration file by using the PyGame keyboard constants, which are returned in the key attribute of the PyGame event:

>>> import pygame
>>> for event in pygame.event.get():
>>>     print cfg.get("keyboard.down." + str(event.key))

Methods

get(key) Return the value for the given key.
has_config(key) Checks if the given key exists in the configuration.