Skip to main content

InfoScout GarlicConfig

Project description

CircleCI codecov

GarlicConfig

GarlicConfig is a framework that makes it easy to deal with configurations in an easy yet flexible way. The core of this package is written in C++ and this Python package wraps the native code to provide an easy access to the configuration and some extra feature on top of it. The native library allows for quick retrieval of the configurations which can be used on any platform, this wrapper, however allows for defining advanced validation and config retrieval logic. For example, you could define your own conventions for getting localized version of configs.

The whole thing starts by defining the structure of your configs using models.

You can define models by inheriting from ConfigModel:

from garlicconfig import models

class DatabaseConfig(models.ConfigModel):
    pass

By adding attributes (or properties), you can define what this model recognizes and what format should each one of these attributes have.

For properties, you can use ConfigField. There are a set of built-in fields:

  • StringField
  • IntegerField
  • ArrayField
  • ModelField
  • BooleanField

You can also make your own custom ConfigModel and/or ConfigField.

for example:

from garlicconfig import fields
from garlicconfig.exceptions import ValidationError

class EvenIntegerField(IntegerField):

    def validate(self, value):
        if value % 2 != 0:
            raise ValidationError('bad integer')

    def to_model_value(self, value):
        return int(value)

    def to_garlic_value(self, value):
        return str(value)

The field class above stores str in the python dictionary representation. However, for the materialized config model, int is used. It also invalidates unaccepted values by raising ValidationError, in this case odd values.

Note that to_model_value is responsible for converting a basic type to a more complicated python type, perhaps constructing a Python-defined class.

to_garlic_value does the exact opposite, it should convert the value to a basic value. Basic value is defined to be one of the following types:

  • str
  • int
  • float
  • dict
  • set & list

This is primarily used for ease of encoding/decoding. By default, both of these methods return the given value without making any changes and I'd recommend not creating very complicated objects since it'll limit the accessibility of them in different platforms should you need to support them.

Next, you can define your own config model and use the custom ConfigField we just created.

for example:

class SomeRandomConfig(ConfigModel):

	value = EvenIntegerField(nullable=False, default=2)

You can use py_value method on config models to get a python dictionary with basic value types in it. This is handy to cache it in memory, or to use it for serialization.

from_dict will create a new config model from a python dictionary.

Furthermore, you can use garlic_value to construct a GarlicValue from the current config model and use from_garlic to construct a model from a GarlicValue.

GarlicValue is a type that keeps configuration objects in the native code and loads them in Python lazily. This allows you to lower memory usage while speeding up all operations. It also comes with a set of handy methods:

resolve

Allows for requested a specific value by providing a dot separated path.

for example:

class ParentConfig(models.ConfigModel):

    random_config_field = models.ModelField(SomeRandomConfig)


foo = ParentConfig()
foo.random_config_field.value = 8
garlic_value = foo.garlic_value()
print(garlic_value.resolve('random_config_field.value'))

In the above code, if value to the given path exists, the python representation of the value gets returned. Otherwise, None gets returned. This is helpful because you can simply give the path to the final value and get the requested value. Since all of this is happening in the native code, it's significantly faster to use GarlicValue over python dictionary or regular models.

Your goal should be to validate models using ConfigModel and store/read configurations using GarlicValue.

clone

Copy operations, specially deep copies in Python are very expensive. You can, however, clone GarlicValue instances much faster by using the native clone which copies the object without the need to use deep copy yet accomplish the same result.

for example:

garlic_value_1 = foo.garlic_value()
garlic_value_2 = foo.clone()

Serialization

You can use the following code to encode/decode configs. The default encoder is Json. However, you can write your own encoder and support other formats as needed.

from garlicconfig import encoding

config = DatabaseConfig()
serialized_string = encoding.encode(config, pretty=True)

Merging layers

You merge two configuration layers in order to support inheritance. Something that will come very handy if you plan to use localization or multi-layered configurations.

Any GarlicValue instance will have a apply method that will basically applies a second GarlicValue on top of itself.

for example:

from garlicconfig import models
from garlicconfig import fields


class ExtraConfig(models.ConfigModel):

    has_id = fields.BooleanField(default=False)
    has_degree = fields.BooleanField(default=False)


class DumbConfig(models.ConfigModel):

    name = fields.StringField(nullable=False)
    numbers = fields.ArrayField(IntegerField())
    extra = models.ModelField(ExtraConfig)

    def validate(self):
        super(DumbConfig, self).validate()
        if not self.name and not self.numbers:
            raise garlicconfig.exceptions.ValidationError('invalid config for some reason!')


config_1 = DumbConfig.from_dict({
    'name': 'Peyman',
    'numbers': [1, 2, 3]
    'extra': {
        'has_id': True
    }
}).garlic_value()

config_2 = DumbConfig.from_dict({
    'name': 'Patrick',
    'numbers': [4, 5, 6]
    'extra': {
        'has_degree': True
    }
}).garlic_value()

config_1.apply(config_2)
config_1.resolve('numbers')  # returns [4, 5, 6]
config_1.resolve('name')  # returns 'Patrick'
config_1.resolve('extra.has_id')  # returns True (from config_1)
config_1.resolve('extra.has_degree')  # returns True (from config_2)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

garlicconfig-1.1.9-cp37-cp37m-win_amd64.whl (438.6 kB view details)

Uploaded CPython 3.7mWindows x86-64

garlicconfig-1.1.9-cp37-cp37m-win32.whl (405.1 kB view details)

Uploaded CPython 3.7mWindows x86

garlicconfig-1.1.9-cp37-cp37m-manylinux1_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.7m

garlicconfig-1.1.9-cp36-cp36m-win_amd64.whl (439.1 kB view details)

Uploaded CPython 3.6mWindows x86-64

garlicconfig-1.1.9-cp36-cp36m-win32.whl (405.9 kB view details)

Uploaded CPython 3.6mWindows x86

garlicconfig-1.1.9-cp36-cp36m-manylinux1_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.6m

garlicconfig-1.1.9-cp35-cp35m-win_amd64.whl (431.2 kB view details)

Uploaded CPython 3.5mWindows x86-64

garlicconfig-1.1.9-cp35-cp35m-win32.whl (398.7 kB view details)

Uploaded CPython 3.5mWindows x86

garlicconfig-1.1.9-cp35-cp35m-manylinux1_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.5m

garlicconfig-1.1.9-cp34-cp34m-manylinux1_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.4m

garlicconfig-1.1.9-cp27-cp27mu-manylinux1_x86_64.whl (1.2 MB view details)

Uploaded CPython 2.7mu

garlicconfig-1.1.9-cp27-cp27m-manylinux1_x86_64.whl (1.2 MB view details)

Uploaded CPython 2.7m

File details

Details for the file garlicconfig-1.1.9-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: garlicconfig-1.1.9-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 438.6 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.5

File hashes

Hashes for garlicconfig-1.1.9-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 031d01127af54672f11aea1585553f9e9816a6a44de3562dbd765627afb21b06
MD5 32a75d18dbd7dff20c6296ad7674a642
BLAKE2b-256 9d966e2082661f276e20f2b6e1f4ea4dec7ffd22a633972497c32d292a00fcdf

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.9-cp37-cp37m-win32.whl.

File metadata

  • Download URL: garlicconfig-1.1.9-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 405.1 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.5

File hashes

Hashes for garlicconfig-1.1.9-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 a3663b3d93848341a8db5f4882c5260913f8883d8f7c40efd96223c0fbaa857b
MD5 1b386b914de19039d3294da6b0cbb6a8
BLAKE2b-256 24544b0889b11bc19abbf44936e94fb98a26e16091170f93e612ddd84700f7f0

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.9-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: garlicconfig-1.1.9-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.0

File hashes

Hashes for garlicconfig-1.1.9-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 076883ee888f1a8ec32b38aa2804acb6f32ac343a809698eb530c5d1d7faf524
MD5 733e780386e1be40d71e85dad925034e
BLAKE2b-256 98bbc819b67e09762fba0554458aee62bfce390fe5193d090c7cfc896178e005

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.9-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: garlicconfig-1.1.9-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 439.1 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.8

File hashes

Hashes for garlicconfig-1.1.9-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 042aeee2a3272c6381d5e14e828d51634b669a624ff92085c52ec7c9d9e17f72
MD5 04637dd9064debb8f4816291c6df0f7d
BLAKE2b-256 01f492f91a9a5957a77125983f01d4cf0c2bccd078c918ed66fa7c1c3c4f4631

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.9-cp36-cp36m-win32.whl.

File metadata

  • Download URL: garlicconfig-1.1.9-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 405.9 kB
  • Tags: CPython 3.6m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.8

File hashes

Hashes for garlicconfig-1.1.9-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 53ed7efb3f6015f3e330506dc823b617dc7aa0813975ea6359e7f2aa24436d08
MD5 1f146ff6ef6f7c878fb799b310e1c01c
BLAKE2b-256 f82184a15ed37310c6e63c0c5d1fb3d1fec711cdbbc67baa15e00f3f9686ecea

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.9-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: garlicconfig-1.1.9-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.0

File hashes

Hashes for garlicconfig-1.1.9-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 33ec2d57a08af07c8ef3e29edc3c3c12d685018925c44669843a85965d6a299c
MD5 394f269338442d29c91d7eae1f994ffe
BLAKE2b-256 0023189277b89986bbebb56423ad6e55e643174019a2760cdca87d8802944ba4

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.9-cp35-cp35m-win_amd64.whl.

File metadata

  • Download URL: garlicconfig-1.1.9-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 431.2 kB
  • Tags: CPython 3.5m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.5.4

File hashes

Hashes for garlicconfig-1.1.9-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 18c80e442d2d43108d9f0175fa86fa8f60c6c8e32887731516fecc85fee5e13c
MD5 56a3f618429a077f192ea773d2c3c65f
BLAKE2b-256 0a7cb657b136ff7c0b9227101df8777fa759939e62818a865456f05cdcef595a

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.9-cp35-cp35m-win32.whl.

File metadata

  • Download URL: garlicconfig-1.1.9-cp35-cp35m-win32.whl
  • Upload date:
  • Size: 398.7 kB
  • Tags: CPython 3.5m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.5.4

File hashes

Hashes for garlicconfig-1.1.9-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 535fcca20a5fd87025223917e6df131c1c77d3b5d39bd9f1f4eb12f6ba25fa48
MD5 2dbbda7750a55e13446d52060211b443
BLAKE2b-256 04b56c2a55538a2a0d038d44129f6b53f5f584288c3ab24317938d6027bb7075

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.9-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: garlicconfig-1.1.9-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.0

File hashes

Hashes for garlicconfig-1.1.9-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6b00d8083a462a47ac675e6fe2c99fc08615a7c922765eb33226e479a7d528ad
MD5 5f3d0d9476ca18af7b5c2244dc0590e3
BLAKE2b-256 d62d84875a1ed61063ea5792cf73487ffc7b7d57b97c93234300c3ddaa8b390a

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.9-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

  • Download URL: garlicconfig-1.1.9-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.0

File hashes

Hashes for garlicconfig-1.1.9-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e4342619a23ae38a93cefa6bceeca2410e3d01867cd37961b41f880048165850
MD5 d1dae56f3754a98d7aae7526a4473836
BLAKE2b-256 a0a6f456ea0a9d871bfd18112989685b2a773a9edc047cb04eda2f03910e5f8f

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.9-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

  • Download URL: garlicconfig-1.1.9-cp27-cp27mu-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 2.7mu
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.0

File hashes

Hashes for garlicconfig-1.1.9-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4aaf5dc67361ead43f767825f4961246de969b31fee0d841bdc34e34620f91e4
MD5 411e72c548742d62894961c51d01d711
BLAKE2b-256 ed4df90ec0cfd39b2d563037ca956e5974cb81a2aeec84091a01129a4cac4331

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.9-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

  • Download URL: garlicconfig-1.1.9-cp27-cp27m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 2.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.0

File hashes

Hashes for garlicconfig-1.1.9-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 58599c52f6c4737ac8ff0016fc5669fb8d460d79f7c8bf5822e7f194d7e77b2d
MD5 bd4f5a589154813959e845368d67a1e2
BLAKE2b-256 ca9edaeb5a05764631b4a75301615c5ee8393608115596749f6af2c84caf2a9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page