Package: nectarine
nectarineLibrary to load configuration from various sources.RequirementsNectarine requires Python 3.7 or above, and has been tested using Python 3.7 and 3.8.InstallingNectarine can be installed through pip.$ pip3 install nectarine        # Install the basic version
$ pip3 install nectarine[yaml]  # Install the YAML extensionProvidersNectarine loads the configuration from different back-ends calledproviders. The table below lists the available
providers.NameDescriptionargumentsA provider that reads from the program argumentsenvA provider that reads from the program environment variablesdictionaryA provider that reads from a user-provided dictionaryjsonA provider that reads from a user-provided JSON fileyamlA provider that reads from a user-provided YAML fileStep-by-step exampleNectarine 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 awesomepikalibrary!).
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 argumentsthe program environmenta JSON configuration file, say./conf.jsonwith fields for the host / port / username / passwordThe 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:fromdataclassesimportdataclass@dataclassclassRabbitMQ:host:strusername:strpassword:strport:int=5671The next step is to communicate with the user, obtain the configuration, and create an instance ofRabbitMQwith it. Let's see how we can achieve that with Nectarine.Nectarine provides aloadfunction, which needs two things: the type to load, and a list of providers to load from.We already have our target type (which isRabbitMQ), thus we need to select our providers:for program arguments, theargumentsproviderfor program environment, theenvproviderfor the JSON file, thejsonproviderNow, we can call theloadfunction like below and retrieve its result:fromnectarineimportload,arguments,env,jsonrabbitmq=load(target=RabbitMQ,providers=[arguments(),env(prefix="RABBITMQ_"),json("./conf.json")])print(rabbitmq)That's pretty much it! Therabbitmqvariable 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)FeaturesMultiple configuration providers: program arguments, environment, configuration files, ...Value overriding: higher-priority providers override values from lower-priority providersComplex structures: nested dataclasses and collections are supportedType checking: configuration data is checked against the type hints provided in your dataclassesDataclasses features: default values or factories can be specified just as in regular dataclasses