Skip to main content

🤖 Auto Config

Memory-based global configuration for Python projects -- in 10 lines of code (including empty lines). Made with the intention of ridding the need to pass Config objects everywhere. Option to use namedtupled if wanted.

Installing

pip install aconf

Why?

Honestly? Because why not. Was tired of having to pass Config objects left and right in small personal projects, so created this.

Using

This module comes with three main functions:

  • make_config(**kwargs): Creates the configuration in memory.
  • config(): Loads configuration from memory as standard dictionary.
  • conf(): Loads configuration from memory as namedtuple object for cleaner access.
from aconf import make_config, config, conf

# Creates a global configuration that can be accessed by any other portion of the runtime.
make_config(database={"user": "admin", "password": "db_password", "host": "localhost", "port": "3306"}, method="GET")

# Accessing the global configuration as a dictionary.
print(config()['database']['user'])
# >>> admin

# Accessing the global configuration as a namedtuple object.
print(conf().database.user)
# >>> admin

A single file example doesn't encapsulate the usefulness of this module. Instead, imagine the following project:

.
├── project
│   ├── __init__.py
│   ├── config.py
│   └── functionality.py
└── main.py

config.py

""" 'Config' class to hold our desired configuration parameters. 

Note:
    This is technically not needed. We do this so that the user knows what he/she should pass 
    as a config for the specific project. Note how we also take in a function object - this is
    to demonstrate that one can have absolutely any type in the global config and is not subjected
    to any limitations.
"""

from aconf import make_config

class Config:
    def __init__(self, arg, func):
        make_config(arg=arg, func=func)

functionality.py

""" Use of the global configuration through the `conf` function. """

from aconf import conf

class Example:
    def __init__(self):
        func = conf().func
        arg = conf().arg

        self.arg = func(arg)

main.py

from project.config import Config
from project.functionality import Example

# Random function to demonstrate we can pass _anything_ to 'make_config' inside 'Config'.
def uppercase(words):
    return words.upper()

# We create our custom configuration without saving it.
Config(arg="hello world", func=uppercase)

# We initialize our Example object without passing the 'Config' object to it.
example = Example()
print(example.arg) 
# >>> "HELLO WORLD"

Performance

Absolutely no idea. I wrote this for small projects that I don't intend on releasing and so I have not bothered to benchmark it. If anyone runs the number it would be lovely if you reported either as an Issue, or directly by shooting a pull request with this portion of the README.md updated. The project in essence does the following:

  • make_config(**kwargs): Pickles the kwargs dictionary and saves it to memory.
  • config(): Loads the pickled dictionary from memory.
  • conf(): Loads the pickled dictionary from memory and transforms it into namedtuple.

It would be reasonable to assume conf() performance is slower than config(). If I had to assume the largest performance drop is within the dumping and loading of pickled objects (even if from memory).

Project

This is the entirety of the project, which is inside __init__.py. Uses namedtuple:

import namedtupled

def make_config(**kwargs):
    globals()["aconf"] = kwargs

conf = lambda: namedtupled.map(globals()["aconf"])
config = lambda: globals()["aconf"]

Metadata

Release files for aconf 1.0.1

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

Source distribution (sdist)

Source distribution for aconf 1.0.1
File Size Uploaded
aconf-1.0.1.tar.gz 3.4 kB Details

Built distribution (wheel)

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

Total release size: 6.4 kB

Release files / aconf-1.0.1.tar.gz

Download URL aconf-1.0.1.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c969a5a9001eccabb154957bb95171e4e5d44f71445fd3da938ee97ebf0872d9
BLAKE2b-256 checksum
How to use checksums
dca83eb9db76d7fd660d4909989e09a9b2a253c884920c382ab0be6c25e4fc14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

Release files / aconf-1.0.1-py3-none-any.whl

Download URL aconf-1.0.1-py3-none-any.whl
Size 3.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
943c973d05f5692da64b02b3f1a4ecec577636017ca3b7926ad01be1c0b106eb
BLAKE2b-256 checksum
How to use checksums
a14d21da6ef48f63f07843b17caaaff53257e3e12771aabb22c87e098d8dca0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

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