Skip to main content

Simple configuration loader for python.

Project description

zen-config

Tests Status Stable Version

Simple configuration loader for python.

It requires python 3.7+.

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 (not parent directories though). You can specify the file mode via Config.FILE_MODE.

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.

Other formats can be added by subclassing Format.

To register more formats: Config.register_format(MyFormat).

You can also force the format using Config.FORMAT = MyFormat(...). This can be used to disable auto selection, or pass parameters to the format.

Supported schemas

Currently, those schemas are supported:

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

The schema is automatically inferred from the config class.

Other schemas can be added by subclassing Schema.

To register more schemas: Config.register_schema(MySchema).

You can also force the schema using Config.SCHEMA = MySchema(...). This can be used to disable auto selection, or pass parameters to the schema.

To use pydantic:

from pydantic import BaseModel
from zenconfig import Config

class MyPydanticConfig(Config, BaseModel):
    ...

⚠️ 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.

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-1.1.1.tar.gz (7.3 kB view hashes)

Uploaded Source

Built Distribution

zenconfig-1.1.1-py3-none-any.whl (9.1 kB view hashes)

Uploaded Python 3

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