When a .Setting
is defined, a type is also declared. By default, the value of a Setting is str
, but any class or function that accepts a single parameter and returns a class instance can be used. The class or function passed to _type will be called with the value to process as its only parameter.
Special cases are added to support dict
, list
, tuple
and bool
, which are processed by ast
. The implementation can be found in the unrepr
method in omniconf.config
:
This means that a Setting declared as such:
from omniconf import setting
setting("items", _type=list)
Which is provided by a backend with the following string:
"['foo', 'bar', 'baz']"
Will return a list that looks like this:
from omniconf import config
print(config("items"))
# ['foo', 'bar', 'baz']
For detailed information, see the ast
documentation.
The most simple custom type looks like this:
def custom_type(src):
return src
This example simply takes the input as provided, and returns it as-is. Custom types are not limited to functions, classes can also be used. Any class that has exactly one (mandatory) parameter is valid):
class CustomType(object):
def __init__(self, src, foo=bar):
self.src = src
Some custom types are provided with , which may be used as-is, but also serve as examples.
A somewhat fancy name for what one might normally call a comma separated list. The implementation is not limited to just commas however, and can use any string.