Skip to main content

Configuration Managment in Python

Project description

ConfigField

Configuration managment for Python. Inspired by ml_collections

Features

  • Dot-Based access to fields
  • Type-safe
  • Field value validation
  • Intermediate attribute creation
  • Freezing
from fieldconfig import Config
from fieldconfig import Field
from fieldconfig.field import ValidationError

cfg = Config()
cfg.str = "John"
cfg.nest = Config()
cfg.nest.pos_int = Field(1, validator=lambda x: x > 0)
cfg.nest.tup = Field(None, ftype=tuple)
cfg["nest.tup"] = [1, 2]

print(cfg.to_dict())  # {'str': 'John', 'nest': {'pos_int': 1, 'tup': (1, 2)}}

try:
    cfg.str = 1
except TypeError as e:
    print(e)  # Cannot cast 1 from type float to type str

try:
    cfg.nest.pos_int = -1
except ValidationError as e:
    print(e)  # The provided value -1 (int) does not meet the criteria: lambda x: x > 0

cfg.freeze()
try:
    cfg.str = "Doe"
except ValueError as e:
    print(e)  # Config is frozen


cfg = Config(create_intermediate_attributes=True)
cfg.branch.twig.nleafs = 3
print(cfg.to_dict())  # {'branch': {'twig': {'nleafs': 3}}}
cfg.disable_intermediate_attribute_creation()
try:
    cfg.ranch.twig.color = "green"
except AttributeError as e:
    print(e)
    # Cannot add key ranch because the config has intermediate attribute
    # creation disabled. Did you mean "branch" instead of "ranch"?

Project details


Download files

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

Source Distribution

fieldconfig-0.1.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

fieldconfig-0.1.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file fieldconfig-0.1.2.tar.gz.

File metadata

  • Download URL: fieldconfig-0.1.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.1 CPython/3.12.2 Linux/6.5.0-21-generic

File hashes

Hashes for fieldconfig-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1ae18f061efaae400383471b2fe31f50efc665f25765c9d00de184827e6706d0
MD5 e2c1e53d631bb29cbce068acebf21779
BLAKE2b-256 6c912dd1f48ed723fabbc3694820a677e42c390cfcc3f5698f60698509171e07

See more details on using hashes here.

File details

Details for the file fieldconfig-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: fieldconfig-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.1 CPython/3.12.2 Linux/6.5.0-21-generic

File hashes

Hashes for fieldconfig-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 27eb922d9d8c5fad8a1b4970d5a435f4452254b95ad9d90c44b3a2c363a55e6d
MD5 c1ffe5216a95edb48adf871af6788baf
BLAKE2b-256 e68d7bb5c58368894f007df51aac56e8400790618ee49f016c7fe31e34037295

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