Package: aconf
🤖 Auto ConfigMemory-based global configuration for Python projects -- in 10 lines of code (including empty lines). Made with the intention of ridding the need to passConfigobjects everywhere. Option to usenamedtupledif wanted.Installingpip install aconfWhy?Honestly? Because why not. Was tired of having to passConfigobjects left and right in small personal projects, so created this.UsingThis 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.fromaconfimportmake_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)# >>> adminA single file example doesn't encapsulate the usefulness of this module. Instead, imagine the following project:.
├── project
│   ├── __init__.py
│   ├── config.py
│   └── functionality.py
└── main.pyconfig.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 passas a config for the specific project. Note how we also take in a function object - this isto demonstrate that one can have absolutely any type in the global config and is not subjectedto any limitations."""fromaconfimportmake_configclassConfig:def__init__(self,arg,func):make_config(arg=arg,func=func)functionality.py""" Use of the global configuration through the `conf` function. """fromaconfimportconfclassExample:def__init__(self):func=conf().funcarg=conf().argself.arg=func(arg)main.pyfromproject.configimportConfigfromproject.functionalityimportExample# Random function to demonstrate we can pass _anything_ to 'make_config' inside 'Config'.defuppercase(words):returnwords.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"PerformanceAbsolutely 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 theREADME.mdupdated. The project in essence does the following:make_config(**kwargs): Pickles thekwargsdictionary and saves it to memory.config(): Loads the pickled dictionary from memory.conf(): Loads the pickled dictionary from memory and transforms it intonamedtuple.It would be reasonable to assumeconf()performance is slower thanconfig(). If I had to assume the largest performance drop is within the dumping and loading of pickled objects (even if from memory).ProjectThis is the entirety of the project, which is inside__init__.py. Usesnamedtuple:importnamedtupleddefmake_config(**kwargs):globals()["aconf"]=kwargsconf=lambda:namedtupled.map(globals()["aconf"])config=lambda:globals()["aconf"]