Fundamentals
#
Config ClassesAnyfig allows you to specify configs in Python code. Instead of defining values in json or yaml, you add them to an Anyfig config-class as attributes.
#
What Can You Put in a Config Class?Defining configurations in Python has multiple benefits, one of them being that you can define configs at runtime. Any Python code can go into the config. You can for example call a function and put the result into the config, even if the return value is a custom object.
Just because you can put anything into the config doesn't mean that you should. Keep your configs simple and readable. Complex logic code is better suited for the main program. With that said, it's ultimately up to you... With great power comes great responsibility 🕷️
#
Initialize ConfigTo access the config-values you need to instantiate an object from the config class. This is done via Anyfig's init_config
function. The returned object behaves as a normal class, with the exception of some added Anyfig functionality, for example a custom __str__
function.
#
Using the ConfigOnce the config object is initialized, the config-values can be accessed via the dot notation.
#
Global ConfigMost people pass the config-object around their code, accessing the config-values at various places to initilize their other components. Anyfig offers an alternative to this. The anyfig.init_config()
function registers the config-object with Anyfig, allowing it to be accessed from any file through anyfig.get_config()
or the proxy class anyfig.global_cfg
.
Global objects are often considered bad practice because multiple components can change the same global object which complicates understandability and debugging. Anyfig's config-objects are immutable by default to mitigate this.
#
Saving & LoadingComing soon...