Package: objconf
Objconf - Object Configuration for PythonWhat is objconf?Objconf provides object configuration for Python.
It allows accessing the configuration values as attributes of a configuration object.Unlike Python built-in configparser, all values have a type. Objconf also supports transformation
and validation of values. For example, you can tell objconf to transform relative path to absolute
and verify that the file exists.Here is an example of what would this simple configuration object look like:importobjconf,osclassAppConfig(objconf.Config):key_path=objconf.Attribute(str,transformer=lambdax:os.path.abspath(x),validator=lambdax:os.path.isfile(x))Supported configuration formats:YAMLJSONPython ini-like configparser formatInstallationObjconf can be installed using pip:python3-mpipinstallobjconfDocumentationConfig classConfigis a base class for your configuration class.
Inherit from Config when defining configuration for your application.fromobjconfimportConfig,Attributeimportio# Define the configurationclassAppConfig(Config):simple_string_attr=Attribute(str)int_attr_with_default=Attribute(int,default=5)bool_attr_different_key=Attribute(bool,key='actual_bool_key')int_over_ten=Attribute(int,validator=lambdax:x>10)upper_str_attr=Attribute(str,transformer=str.upper)# Load the configurationyaml_config='''simple_string_attr: string_valueactual_bool_key: Trueint_over_ten: 42upper_str_attr: lower'''app_config=AppConfig.load_yaml(io.StringIO(yaml_config))# Use the configurationprint(app_config.int_attr_with_default)# 5print(app_config.upper_str_attr)# 'LOWER'Loading configurationThe configuration is loaded during creation of theConfiginstance that defines
the configuration values for your application. The instance is created by calling the corresponding
factory method:load_<format>(stream: TextIO, extra_vals: ExtraVals = ExtraVals.WARNING) -> 'Config'Arguments:stream: A file-like object containing configuration values in the corresponding
format. (Exceptload_dict, it takes a dictionary
and  all other loading methods use it internally.)extra_vals: It specifies behavior when encountering an undefined attribute in the configuration.
Defaults towarning, other options areignoreanderror.The loading functions take also other arguments specific for the configuration format
and modifying the behaviour of underlying parser.load_yaml- Load configuration from YAMLload_json- Load configuration from JSONload_ini- Load configuration from python ini-like configparser formatAttributesAttribute stores the value in the owning class (Config) instance.
Constructor has the following parameters:type_: The only required parameter - type of the attribute
(bool,str,int,list(if supported), …).
It is a callable that converts the value to the correct type.
Some types might not be supported; e.g. the Python ini-like format
does not support lists by default.default: Default value to use if not present in the configuration file.key: Specify key in configuration file if different from attribute name.validator: Callable that takes the configuration value and checks whether
it is valid - returnTrue, otherwise returnFalse.transformer: Transform the value from configuration file. This happens
after the type conversion but before the validation.
Can be used for example for transforming paths
(expanding user home dire, changing relative paths to absolute, …).TestsObjconf uses tox to run the tests.python3-mpipinstalltox
python3-mtox