Skip to main content

Config Library for cross module configuration

Project description

easy-reed

A Python library for easily configuring applications across multiple projects

The easy_reed.config_object.config object

the core of the easy_reed library is in the easy_reed.config_object.config object. This class provides an interface to define new configuration fields that an end user can manipulate through a .ini file. While this functionality exists within the Python library in a module called ConfigParser (Which this library uses), the unique problem that it aims to solve is to provide a single location that different packages can use to define their own configuration, across multiple environments. To avoid naming conflicts, this libarary implements a NameSpace concept for defining config fields.

For the most part, developers should not instantiate a new Config object. Instead, this library intializes a Config object to be used by any package within its init file. This can be imported like so

from easy_reed import config

Then, a developer can define fields for their library under an easy_reed.namespace.NameSpace object and add them to the Config object. the Config object acts like a dictionary of dictionaries. If a developer wishes to access "field1" of namespace "example", they would write

config["example"]["field1"]

in the .ini file, the final config key will be the namespace name, followed by a '.' character, followed by the field name.

[DEFAULT]
example.field1 = "Testing"

the .ini file is read once at module startup, but the final config field is not read into the Config Object itself until it is added via a namespace. This is so that developers can define the expected type for the value and have the Config automatically convert the string into the final type when the NameSpace is added.

Defining new fields under a project namespace

To define config fields for a library, a developer must define a Namespace

from easy_reed.namespace import NameSpace

project_namespace = NameSpace("project")

Once that namespace is defined, a developer can then add however many fields they want through the NameSpace.add_entry method.

project_namespace.add_entry("field1", str, "example")

add_entry takes three parameters:

  1. field_name - a string representing the config field key
  2. typ - a python type specifying what type to read the field as
  3. default - the default value to use if the end_user does not include the field in the .ini file

By default, the config object is able to read in these types

  • string
  • int
  • float
  • bool
  • list

New type handlers can be added through the Config.add_handler method. This allows it to parse strings into new types.

Any number of new entries can be added in this way, though all field_names must be unique.

project_namespace.add_entry("field2", int, 0)
project_namespace.add_entry("field3", float, 5.5)
project_namespace.add_entry("field4", bool, True)
project_namespace.add_entry("field5", list, ["one", "two", "three"])

Once all entries have been defined, the NameSpace can be added to the config via the add_namespace method

from easy_reed import config
config.add_namespace(project_namespace)

Although the fields are read in by a ConfigParser at module startup, they are not made accessible through the config object until this registration has occured. This allows the library to convert each field to the correct type underneath the hood with no developer intervention outside of defining the expected type.

Adding new type parsers

lets look at a class called 'Person'

class Person():
    def __init__(self, name: str, age: int):
        self.name: str = name
        self.age: int = age

    def get_name(self) -> str:
        return self.name

    def get_age(self) -> int:
        return self.age

Let's say we want to be able to represent a Person object as a string in the config dictionary. A person consists of a name and an age, so we could represent that with "{name}:{age}" in a string. Let's define a function that can take a string and parse it to a Person when it is in this format.

    def parse_person(person_string: str):
        # the argument must be a string
        if not isinstance(person_string, str):
            raise TypeError(msg)

        # There should be exactly 1 ':' character in the string
        if person_string.count(':') != 1:
            raise ValueError(msg)
        
        parse = person_string.split(':')

        # After splitting by ':', the second element should be readable as int
        if not parse[1].isdigit():
            raise ValueError(msg)
        
        return Person(parse[0], int(parse[1]))

Now we can pass this function to the Config object as a new type handler for the Person type.

config.add_handler(Person, parse_person)

Then we can add it to the NameSpace before passing it to the Config.

project_namespace.add_entry("guts", Person, Person("John", 23))

Now we could add a person into the .ini file.

[DEFAULT]
example.guts = guts:28

once the namespace is added, this Person object can be accessed with the following.

config["example"]["guts"]

Sections

Since this library uses ConfigParser to read the .ini file, it uses the same format. This means you can define multiple 'sections'. This library treats each section like a different environment.

[DEFAULT]
field1=hello
field2=yes
field3=0

[LOCAL]
field2=no
field3=1

[QA]
field3=2

The section that will be used by the Config object is determined by the APP_ENVIRONMENT environment variable. If a key is missing from a section but is available under DEFAULT, then the DEFAULT value will be used.

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

easy_reed-0.9.2.tar.gz (28.0 kB view details)

Uploaded Source

Built Distribution

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

easy_reed-0.9.2-py3-none-any.whl (27.6 kB view details)

Uploaded Python 3

File details

Details for the file easy_reed-0.9.2.tar.gz.

File metadata

  • Download URL: easy_reed-0.9.2.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for easy_reed-0.9.2.tar.gz
Algorithm Hash digest
SHA256 b02d454e0911fab6d0c8cd5474827dd1a54acd6fb940f7bea95032afb616c695
MD5 e7206ac13c461044889948d3280cbb68
BLAKE2b-256 2729ea0bffc034ec21bbcd05d4b4ec8c6fa7c3957b3a8e1c20636599ced56327

See more details on using hashes here.

File details

Details for the file easy_reed-0.9.2-py3-none-any.whl.

File metadata

  • Download URL: easy_reed-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 27.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for easy_reed-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d78340114f4e39f9272cdbfdc9bf3d128db4c785501aff7990b980373423b0b2
MD5 6dca0f0e3ee58457560b7ff8add18c0a
BLAKE2b-256 67b1a6549895e51ff2fa97b644918e94bf246d1357ae219ef9f144cdca35fdb3

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