Generics¶
The generics library contains some useful wrappers to ease common tasks as logging and configuration access.
valuea_framework.generic.remap_dict¶
The remap_dict function is a simple function which renames the keys in a dictionary and leaves out all unknown keys, this can be practical to map a structure straight into a model object.
Usage is as follows:
1import valuea_framework.generic
2
3in_dict = {'a': 1, 'b': 2, 'c': 3}
4out_dict = valuea_framework.generic.remap_dict(in_dict, {'a': 'a1', 'b': 'b1'})
5print (out_dict)
Which should produce a result like this:
{'a1': 1, 'b1': 2}
valuea_framework.generic.item_to_object¶
The item to object function converts a dictionary in an object.
1import valuea_framework.generic
2
3in_dict = {'a': 1, 'b': 2, 'c': 3}
4out_object = valuea_framework.generic.item_to_object(in_dict)
5
6print (out_object.a)
Which should produce a result like this:
1
valuea_framework.generic.get_config¶
Using get_config you can fetch a config file object from the same directory as the broker configuration, using the standard python configparser
1import valuea_framework.generic
2
3conf = valuea_framework.generic.get_config('rdbms.conf')
4print conf.get('defaults', 'host')
valuea_framework.generic.log¶
The log functions provide access to our logging framework, which is based on the standard logging library, but adds context for our messaging, to support message id’s, service paths and users for example.
In the config directory there should be a log.conf file which includes the settings.
All different levels can be called as follows, depending on configuration they might end up in a logfile (or syslog for example).
1import valuea_framework.generic.log
2
3valuea_framework.generic.log.debug("output debug message")
4valuea_framework.generic.log.info("output info message")
5valuea_framework.generic.log.warning("output warning message")
6valuea_framework.generic.log.error("output error message")
7
Our log file for the example might look like this:
2018-06-02 18:37:40,978 valuea __main__.<module> pid:8685 parent:None message:None user:None DEBUG output debug message
2018-06-02 18:37:40,979 valuea __main__.<module> pid:8685 parent:None message:None user:None INFO output info message
2018-06-02 18:37:40,980 valuea __main__.<module> pid:8685 parent:None message:None user:None WARNING output warning message
2018-06-02 18:37:40,980 valuea __main__.<module> pid:8685 parent:None message:None user:None ERROR output error message
Since we don’t have a signed in user and are not executing our logging using a service call, both message and user are
None.