Skip to main content

Simple configuration loader for python.

Project description

zen-config

tests version python

Simple configuration loader for python.

Compared to other solutions, the goal is to bring:

  • simple usage for simple use cases
  • multiple format support
  • use objects rather than plain dict to interact with the config
  • optionally use the power of pydantic for validation

Simple usage

If you don't want to configure much, pass the config path through the env variable CONFIG, and simply use:

from dataclasses import dataclass
from zenconfig import Config

@dataclass
class MyConfig(Config):
    some_key: str
    some_optional_key: bool = False


cfg = MyConfig(some_key="hello")
cfg.save()
...
cfg = MyConfig.load()
cfg.some_optional_key = True
cfg.save()
...
cfg.clear()

Config file loading

When creating your config, you can specify at least one of those two attributes:

  • ENV_PATH the environment variable name containing the path to the config file, defaults to CONFIG
  • PATH directly the config path

💡 When supplying both, if the env var is not set, it will use PATH.

User constructs will be expanded. If the file does not exist it will be created. You can specify the file mode via Config.FILE_MODE.

The config can be loaded from multiple files, see fnmatch for syntax. Note that you will not be able to save if not handling exactly one file.

Read only

If you do not want to be able to modify the config from your code, you can use ReadOnlyConfig.

Supported formats

Currently, those formats are supported:

  • JSON
  • YAML - requires the yaml extra
  • TOML - requires the toml extra

The format is automatically inferred from the config file extension. When loading from multiple files, files can be of multiple formats.

Other formats can be added by subclassing Format: Config.register_format(MyFormat(...), ".ext1", ".ext2").

💡 You can re-register a format to change dumping options.

Supported schemas

Currently, those schemas are supported:

  • plain dict
  • dataclasses
  • pydantic models - requires the pydantic extra
  • attrs - requires the attrs extra

The schema is automatically inferred from the config class.

Other schemas can be added by subclassing Schema: Config.register_schema(MySchema(...)).

You can also force the schema by directly overriding the SCHEMA class attribute on your config. This can be used to disable auto selection, or pass arguments to the schema instance.

⚠️ When using pydantic, you have to supply the ClassVar type annotations to all class variable you override otherwise pydantic will treat those as its own fields and complain.

Conversions

For all schemas and formats, common built in types are handled when dumping.

⚠️ Keep in mind that only attrs and pydantic support casting when loading the config.

You can add custom encoders with Config.ENCODERS. For pydantic, stick with the standard way of doing it.

Contributing

See contributing guide.

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

zenconfig-2.1.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

zenconfig-2.1.1-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file zenconfig-2.1.1.tar.gz.

File metadata

  • Download URL: zenconfig-2.1.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Linux/6.5.0-1016-azure

File hashes

Hashes for zenconfig-2.1.1.tar.gz
Algorithm Hash digest
SHA256 282a52a3e3ec0cf7030485276fa3a3b12cdd4f186fda99a65fb6cdb64bd5511d
MD5 65337a096da92647e3d206b7575f4d06
BLAKE2b-256 38b2636fff4cd3a3c7417cbc2a8030e18c577b0741210a09f4c3c9cc4039c989

See more details on using hashes here.

File details

Details for the file zenconfig-2.1.1-py3-none-any.whl.

File metadata

  • Download URL: zenconfig-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Linux/6.5.0-1016-azure

File hashes

Hashes for zenconfig-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a3a8c63a6d0b6f5ac000f70c327c31a575542037aca8a473872dafb71e235125
MD5 521e61891d28556f5f9ff3ee5f9f93fc
BLAKE2b-256 f2a3ce691c9177f1f035bc4f802df091f23989c128466299026b08c0a42f7c99

See more details on using hashes here.

Supported by

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