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.6-cp37-cp37m-manylinux1_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.7m

garlicconfig-1.1.6-cp37-cp37m-macosx_10_13_x86_64.whl (444.7 kB view details)

Uploaded CPython 3.7mmacOS 10.13+ x86-64

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

Uploaded CPython 3.6m

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

Uploaded CPython 3.5m

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

Uploaded CPython 3.4m

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

Uploaded CPython 2.7mu

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

Uploaded CPython 2.7m

garlicconfig-1.1.6-cp27-cp27m-macosx_10_13_x86_64.whl (448.5 kB view details)

Uploaded CPython 2.7mmacOS 10.13+ x86-64

File details

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

File metadata

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

File hashes

Hashes for garlicconfig-1.1.6-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cfab9d9834e54bc41c63592bc9187143d0b6ab25e830400fac32234a02ccdfbc
MD5 6e25bd20249fed06ba08ba43343470c1
BLAKE2b-256 d866c90ea87ee0bbb62d9fad2cd81474c438d5b86f4ef97ffd4f80e397bc62e9

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.6-cp37-cp37m-macosx_10_13_x86_64.whl.

File metadata

  • Download URL: garlicconfig-1.1.6-cp37-cp37m-macosx_10_13_x86_64.whl
  • Upload date:
  • Size: 444.7 kB
  • Tags: CPython 3.7m, macOS 10.13+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.0

File hashes

Hashes for garlicconfig-1.1.6-cp37-cp37m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 ae36a5b2dcecc70536c0bd0ff0d6adacf8a17a41e1955ad1295ae77947bc34cf
MD5 d48c0c15cd98952f3633b2d7e35ecea6
BLAKE2b-256 29597e750f48b82058d5e3658570b5064ea5becfd3b4faf848e6fb28f314f0cd

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for garlicconfig-1.1.6-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4b2d4f76e24609a8e04d079351db7e718d5daa6527cdddb2d8b0a066db8a1c87
MD5 8b7944a2663d72d4de70ef64f1474150
BLAKE2b-256 5d799149aa2c26ae5ad8b5daa8eb155959e5b3db09b5ae43fb8f575be63631d1

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for garlicconfig-1.1.6-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 20371d658e0ec670ebd55b40c364243d0a69516e3f15c5c2b57dc1145bab40b1
MD5 2c8874efe566a33b24e92ac1e60aebf9
BLAKE2b-256 e339b33bc56af02289d570ba737d16486974a25d9552a3e5967f55e9734b14c0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for garlicconfig-1.1.6-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 299bf569646be35789176ea125adc90e59f7cdc02f774ec4ba656076382696b5
MD5 d56ee936ec25816a3bbe1a28561d960e
BLAKE2b-256 3047defa2589a7178a1f80d2133552ba67c3d0a49ce4e703d18c207ca34c9c04

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for garlicconfig-1.1.6-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8ee880d5b8616797e35d35843effc3f79e1e8b09a3c0cd7f28e365a91dad7898
MD5 1563a2cf6827fdc98b8ef75474159262
BLAKE2b-256 bcfe1ff34d812166a84ad0124a546949def7a3c54efda726752f739ff1555cb6

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for garlicconfig-1.1.6-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dc3410067d8cc0d56ecc411e50e851bdef34a7051c4b4e3938ab4d4524514d21
MD5 b9d9844838b0d6d51bcd2b5c69d7cb92
BLAKE2b-256 9be539f69bcb4a47a9c8b679f3beb5841f01c89d422cbf3f0a40e4522ff539ba

See more details on using hashes here.

File details

Details for the file garlicconfig-1.1.6-cp27-cp27m-macosx_10_13_x86_64.whl.

File metadata

  • Download URL: garlicconfig-1.1.6-cp27-cp27m-macosx_10_13_x86_64.whl
  • Upload date:
  • Size: 448.5 kB
  • Tags: CPython 2.7m, macOS 10.13+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.15

File hashes

Hashes for garlicconfig-1.1.6-cp27-cp27m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 e23a3b614238b090ab4682fa85a10aeff7016dbc6ace0b77892ba2682c3ea7f2
MD5 d9d4a50d3054b315d8d31d7b03600412
BLAKE2b-256 ae251d6d2d4468e2fe576f81aa99fc201df5b32c59ba3d884830ff59d41c6fb9

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