Skip to main content

nectarine

Library to load configuration from various sources.

Requirements

Nectarine requires Python 3.7 or above, and has been tested using Python 3.7 and 3.8.

Installing

Nectarine can be installed through pip.

$ pip3 install nectarine        # Install the basic version
$ pip3 install nectarine[yaml]  # Install the YAML extension

Providers

Nectarine loads the configuration from different back-ends called providers. The table below lists the available providers.

Name Description
arguments A provider that reads from the program arguments
env A provider that reads from the program environment variables
dictionary A provider that reads from a user-provided dictionary
json A provider that reads from a user-provided JSON file
yaml A provider that reads from a user-provided YAML file

Step-by-step example

Nectarine gives you the ability to describe a configuration using Python's dataclasses, and load it from various sources (program arguments, configuration files, ...).

Let's consider a program that consumes events from a RabbitMQ broker (for example, using the awesome pika library!). That little program should of course allow the user to configure connection parameters such as the broker's host and listening port, the credentials to use, etc.

Our program will accept configuration from 3 different providers:

  • the program arguments
  • the program environment
  • a JSON configuration file, say ./conf.json with fields for the host / port / username / password

The idea here is to allow the user to quickly override the configuration file by using the environment or the arguments.

In order to represent those connection parameters, we create the following dataclass:

from dataclasses import dataclass

@dataclass
class RabbitMQ:
    host: str
    username: str
    password: str
    port: int = 5671

The next step is to communicate with the user, obtain the configuration, and create an instance of RabbitMQ with it. Let's see how we can achieve that with Nectarine.

Nectarine provides a load function, which needs two things: the type to load, and a list of providers to load from.

We already have our target type (which is RabbitMQ), thus we need to select our providers:

  • for program arguments, the arguments provider
  • for program environment, the env provider
  • for the JSON file, the json provider

Now, we can call the load function like below and retrieve its result:

from nectarine import load, arguments, env, json

rabbitmq = load(
    target=RabbitMQ,
    providers=[
        arguments(),
        env(prefix="RABBITMQ_"),
        json("./conf.json")
    ]
)
print(rabbitmq)

That's pretty much it! The rabbitmq variable is now loaded with our configuration when we run the program:

$ echo '{"host": "localhost", "port": 5671, "username": "user", "password": "pass"}' > ./conf.json
$ ./consumer.py
RabbitMQ(host='localhost', username='user', password='pass', port=5671)
$ ./consumer.py --port 1234
RabbitMQ(host='localhost', username='user', password='pass', port=1234)

Features

  • Multiple configuration providers: program arguments, environment, configuration files, ...
  • Value overriding: higher-priority providers override values from lower-priority providers
  • Complex structures: nested dataclasses and collections are supported
  • Type checking: configuration data is checked against the type hints provided in your dataclasses
  • Dataclasses features: default values or factories can be specified just as in regular dataclasses

Metadata

Release files for nectarine 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for nectarine 1.1.1
File Interpreter ABI Platform
nectarine-1.1.1-py3-none-any.whl Python 3 none any Details

Release files / nectarine-1.1.1-py3-none-any.whl

Download URL nectarine-1.1.1-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8c5f32990233fb75e75ca4bb7b6391a0892c2aa41686212d2a0aa420af8f1d38
BLAKE2b-256 checksum
How to use checksums
3910c57d653c97a195ac6f4af42eb452abe07bce511a91eac9fb0e571065d06e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.7.0 pkginfo/1.7.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5

Security for nectarine 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page