janehmueller / python-json-config Goto Github PK
View Code? Open in Web Editor NEWA config library for python
License: MIT License
A config library for python
License: MIT License
Pass the value of the member strict_access
to children spawned at a later point in time
Pickle tries to call __getstate__
which fails since it is not handled in the custom __getattr__
method.
Use this solution.
It would be great if it were possible to iterate over the configuration.
For example:
for key, value in config.items():
print(f'{key} --> {value}')
Add a return self
statement at the end of the merge_with_env_variables
function
E.g., we add value x
to path [foo, bar]
but foo
does not exist yet. The subpath foo
is not added and an error is thrown.
It would be great if one could also serialize the config to a binary format that can also be read by other languages such as Java or C++. The msgpack library is ideal for this purpose. For example, there is a msgpack library in all common programming languages, which makes communication easy.
Therefore I propose to add two instance methods to the config object called to_bytes
and from_bytes
which use msgpack to serialize and deserialize the configuration from and to a sequence of bytes.
Instead of merging the current config with the ENV variables by doing:
builder = ConfigBuilder()
# you can also just pass a single prefix (builder.merge_with_env_variables("MYPROJECT")
builder.merge_with_env_variables(["MYPROJECT", "MYPYTHONPROJECTS"])
config = builder.parse_config({"server.host": "0.0.0.0"})
It should be possible to do it within the builder patter like this:
builder = ConfigBuilder()
builder.parse_config({"server.host": "0.0.0.0"}).merge_with_env_variables("MYPROJECT").merge_with_env_variables("MYPYTHONPROJECTS")
Instead of configuring the CofigBuilder directly in the code, it should be possible to pass a JSONSchema file which describes the configuration. This tutorial describes how JSONSchema works.
This file should define the schema of the configuration. Then the corresponding validators and converters can be used while reading the configuration.
It would be good to have the possibility to check if a certain configuration parameter is defined within the configuration or not. So basically something like this should be possible:
if 'backend.media.type' in config:
return True
else:
return False
Unfortunately, it is often the case that other libraries only accept dictionaries as configuration input. For this reason, it would be a nice feature to be able to access all or part of the configuration as a normal dictionary.
There should be two submodules that already provide a number of validators and converters for certain objects.
For example, validators for IP addresses, port numbers, etc.
It would be nice if one could specify the prefix or list of prefixes for environment variables that should be merged into the configuration. For example to merge all environment variables starting with "FOO_"
into the configuration. If a key already exists, the corresponding value will be overwritten, it doesn't exist, it will be added.
A possible implementation might look like this:
def _merge_with_env_variables(self, prefix: Union[str, List[str]]):
"""
This method takes all environment variables that start with the specified prefixes,
and merges them into the config. If the key is not already present in the config,
it will be added, if the key is present in the config it will be overwritten
:param prefix: a prefix or list of prefixes for the env variables to merge
"""
prefixes = []
if isinstance(prefix, str):
prefixes = [prefix]
for key in os.environ.keys():
for prefix in prefixes:
if key.startswith(prefix):
value = os.environ[key]
key = key.replace(prefix, '').lower().replace('_', '.')
# TODO: Merge key and value into the configurtation
self.config.update(key, value)
If you define a key for which certain checks are to be performed, it is implicitly assumed that this key is always present. However, this assumption does not hold in all cases. For example it is quite possible that a key is optional. So if it is defined, then certain checks should be done, but if it is missing, then this is OK. In this case no error should be thrown. For example in the following case:
config.validate_field_type('backend.username', str)
it would be OK if the key is missing. All in all it should be possible to define which keys are optional.
There is currently no way to specify what exactly failed while parsing the config. In the optimal case, every validator should be able to define a message to show the user what exactly went wrong.
For example: "There was an error while validating the host field, 127.0.0." is not a valid IP address."
It needs to be possible to attach new keys to config during runtime.
So for example:
config['foo.bar'] = 'xyz'
and/or config.foo.bar = 'xyz'
or config.add('foo.bar', 'xyz')
It would be great if one would be able to execute several validator functions in succession. So instead of having something like:
builder.validate_field_value('server.port', function_1)
it would be great if one could do:
builder.validate_field_value('server.port', [function_1, function_2])
... so that the specified validators get executed one after another.
E.g. with prefix TEST
the following should happen
TEST_VALUE
TESTSUITE_VERSION
Currently it matches both
It would be great if a parsed configuration could also be transformed back into a dictionary or json string format. For example like: config.dump()
or config.to_json()
Currently if a field has an underscore in its name (e.g., log_file
), the field can't be overwritten via environment variables. This can be enabled by escaping these underscores with another underscore. (In this case setting the variable PREFIX_LOG__FILE
)
This was an oversight in #8
For python 3.7 to work with travis the build needs to run on xenial (Ubuntu 16.04) instead of trusty (Ubuntu 14.04) and require sudo. (reference).
The travis file should have the following
python:
- "3.6"
matrix:
include:
- python: 3.7
dist: xenial
sudo: true
Also add caching for pip dependencies:
cache: pip
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.