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.4.1-cp312-cp312-musllinux_1_2_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

garlicconfig-1.4.1-cp312-cp312-musllinux_1_2_aarch64.whl (2.6 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ ARM64

garlicconfig-1.4.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

garlicconfig-1.4.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (1.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

garlicconfig-1.4.1-cp311-cp311-musllinux_1_2_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

garlicconfig-1.4.1-cp311-cp311-musllinux_1_2_aarch64.whl (2.5 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ ARM64

garlicconfig-1.4.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

garlicconfig-1.4.1-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (1.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

garlicconfig-1.4.1-cp310-cp310-musllinux_1_2_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

garlicconfig-1.4.1-cp310-cp310-musllinux_1_2_aarch64.whl (2.5 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ ARM64

garlicconfig-1.4.1-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

garlicconfig-1.4.1-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (1.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

garlicconfig-1.4.1-cp39-cp39-musllinux_1_2_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

garlicconfig-1.4.1-cp39-cp39-musllinux_1_2_aarch64.whl (2.5 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ ARM64

garlicconfig-1.4.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

garlicconfig-1.4.1-cp39-cp39-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (1.6 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

File details

Details for the file garlicconfig-1.4.1-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8cf7e897ce79c822f59a45cb6b63297350e13b79bacb585a99d4a235c014491a
MD5 40e1b1ac3fc5473a6e7b343642f6d19c
BLAKE2b-256 d83d54f4021cfca085b822e60b507313076bc7f2249bc5c0e6a1defdd2e0e7b3

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp312-cp312-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp312-cp312-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 10ca222061ff3df953a2d68a118b9af981a95e49d083cc8708746c7bec7cc0b4
MD5 f0f0556fb875bd68bcfcc6a0c7460a82
BLAKE2b-256 e71ff6aa51f2d6a11d28eac9074c61885efb283573a7ea2870ee02953234e477

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8ad21d7f519b8721fc2e1bbe386db292d5211046c8c6454ab564813fc49452c2
MD5 6eb8296f9be7f495fa604cc8f27957b2
BLAKE2b-256 74ccbd575349e6be8aa13b6ffd96aafe859d9a0cd9df0f383b066423e1aeff98

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b665b759b24512b563d91f806a5f663bc706a0922bb28d8307adbffbe3f56779
MD5 07cedafae9785112e0c61f561e1b1e1c
BLAKE2b-256 c5dd589ca3ab16781345e157ff47017776d0bbcbc214116149a9a6f363ca79b6

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 77d45e1b8f563c5fe144bf46d1f5cf3d46d08cd445d8b2ac510c6ba09d908ed0
MD5 bbb67096a8c38828d81edaa949b0eb47
BLAKE2b-256 fc2522b546a37196315b7fca52cb8b38b0e137b1af25cd06dea410faf5082bb2

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp311-cp311-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp311-cp311-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 f67e367155109bea225caa6fadba60bd2c2641c5ac243e093c4a292bd6368fae
MD5 6d808c1b7daed3e981e9958d949269f5
BLAKE2b-256 97e036593a0f0d79680eeff1f63dd22fb48ee508dc9062c5c4186786f7f5db35

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 65bb0e73591a29a69f749fc48134cb379bac272e002c3ac699fc2b98f9046ec0
MD5 d6c3c21ddbc50856d6b603402634d423
BLAKE2b-256 ff5a1343677b5388fe9d62ac943bcbb0c4c12c3828f508bcc189fcccb5889747

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 485add563dd1a67eddfb9ed48ddf96424939837da2eb549be5385eac99d0dc7c
MD5 e59e731a0e0da7962047b0c14f6db0a1
BLAKE2b-256 0e2c385313ba219f5c5d9636a5476b75ad82362de70b4dc347ed42ccaed975d2

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 85e899d7fcc3c270177c3f018d8efa30b5b879c0d0bde803c245a72eb9f26c14
MD5 cdbffa656924dc55cd340d827b3e321e
BLAKE2b-256 8b0ed2b09ae35ab8be0d4107ab56aa201d8ef823b9a3c7acafb319ce9321d90b

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp310-cp310-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp310-cp310-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 41da8d620d7d692f25762f8f632a1848d1a971618a1a0fa25ff3d75272e4dc08
MD5 8fff093a28edc710b7d38be13c04b3ec
BLAKE2b-256 af5bd947a8e445ce1fb458e3bd877105430b3bf673f81332747129bcfdf9e4ce

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 17e5a272ecf4d3929661c3218efb493b0c53ebb4ec24b31f3c985d2831076984
MD5 88f6bfcff6ef895522553ccf08836afe
BLAKE2b-256 c9bcedda91a4fb303d5e6f8982f35608112f6dd1477fb227390194ec4a3b690e

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c17e191ca03d71269a22544b889a372517ba9bbef49b43242c754f16138096d4
MD5 c858668b2951e0c9053bc83cdb8ec2e6
BLAKE2b-256 e30725bbe785d7d97339d1b87a04586a7a2d54f479e15a7c8f80c17d6698e0f8

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 27089eafcbb06bdade4dee2061a471e3509f025d0a4e704903515312f6cdbbc3
MD5 ef8b42a0d1671805a5fbf5c22f294c9d
BLAKE2b-256 ada37a4d9dc7e348a924c07f2092692085da9718b3a8dd32a2216509529653eb

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp39-cp39-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp39-cp39-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 f6442dbef1f1991ed7046071db831c53fa1761cd5f8c822541053cdbb95d2f51
MD5 908711593c68af6bd955b9bc18bc4be6
BLAKE2b-256 d08752c1449e47d1cb29d6e21f39bd6ff1e1261f9e3600c95ad3db9e43af6803

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fdec8b0c69e4733a56bc1cec02e473b605838e20ec8dbe56a751115e6635b194
MD5 f0a6b943f1f77995084ebba1aec6b313
BLAKE2b-256 59e6b01ae7c3abf8c9abbb8f60a9f14ed35db267944f4c4ed8003341e17fcf4a

See more details on using hashes here.

File details

Details for the file garlicconfig-1.4.1-cp39-cp39-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for garlicconfig-1.4.1-cp39-cp39-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 0c80fd367eb9eff8c72c8ff700121e2b612ac879c1ae1e5d3ebec45c3d1cfd23
MD5 bec0589fa69d0f580337c69e22d96bd1
BLAKE2b-256 4b3115e882b342d85d32caffebb24af3fd64063e8896c5c785a925f3bbfa1668

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