Giter Site home page Giter Site logo

python-json-config's People

Contributors

dependabot[bot] avatar janehmueller avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

python-json-config's Issues

Enable config iterations

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}')

Use msgpack for binary serialization

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.

Revise ENV merging

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")

JSONSchema support

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.

Check if key exists in config

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

Return as dict

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.

Submodule for validators and converters

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.

Merge with ENV variables

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)

Handling of optional values

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.

Better error reporting

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."

Add new keys to the config

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')

Chain validator functions

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.

Serialization methods (JSON)

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()

Use proper python 3.7 for travis and cache pip dependencies

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.