Lightweight configuration framework for Python, combining the most notable aspects of Gin and Sacred.
Project description
tonic-config
📜 Tonic is a lightweight configuration framework and experiment manager for Python, combining the most notable aspects of Gin and Sacred.
Why?
-
Gin-config is designed around their own configuration files and syntax, and is difficult to work with programatically.
-
Sacred has a larger yet familiar featureset, but the configuration syntax is very different. The additional advantage of tonic-config is that it has the concept of global variables.
Getting Started
1. Minimal Example
Configurations are handled via annotating functions. With tonic, only default parameters of functions can be configured.
The most simple tonic example looks as follows:
import tonic
@tonic.config
def foobar(foo, bar=None):
print(foo, bar)
# no configuration used for call
foobar(1000)
# set configuration and reconfigure registered functions
# tonic.config.reset() resets configuration
# tonic.config.update() merges the given configuration with the previous, overwriting values.
tonic.config.set({
'foobar.bar': 1337
})
# call functions with new configuration
foobar(1000)
foobar(1000, bar='bar')
When run, the above will output:
>>> 1000 None
>>> 1000 1337
>>> 1000 bar
Notice in the above example even if a function has been configured, manually specifing the named values when calling the function takes priority.
2. Configuring Classes
If a class is annotated, the configuration will apply to parameters of the init method.
Other methods within the class also need to be annotated separately.
import tonic
@tonic.config
class Fizz(object):
def __init__(self, foo=None):
print(foo)
@tonic.config
def buzz(bar=None):
print(bar)
Fizz().buzz()
tonic.config.set({
'Fizz.foo': 1,
'Fizz.buzz.bar': 100,
})
Fizz().buzz()
The output of the above will be:
>>> None
>>> None
>>> 1
>>> 100
3. Namespaces
Tonic groups parameters of registered functions under their own namespace by default, corresponding to the hierarchy of objects within the file to that function.
If you manually specify the namespace of a configured function, any other configured function with the same namespace will also share the same configurations for parameters.
But can no longer access the function under the default name. this condition might be relaxed in future versions
import tonic
@tonic.config('fizz.buzz')
def foobar1(foo=1, bar=None):
print(foo, bar)
@tonic.config('fizz.buzz')
def foobar2(foo=2, bar=None):
print(foo, bar)
tonic.config.set({
'fizz.buzz.bar': 'bar'
})
foobar1()
foobar2()
Outputs:
>>> 1 bar
>>> 2 bar
4. Global Configurations
Tonic also supports global parameter configurations by using the *
namespace.
Any function with a parameter that matches the global namespace will be configured.
Explicit configuration of a namespace with matching parameters will take priority.
import tonic
@tonic.config
def foobar(foo=None, bar=None, buzz=None):
print(foo, bar, buzz)
@tonic.config
def fizzbang(fizz=None, bang=None, buzz=None):
print(fizz, bang, buzz)
tonic.config.set({
'*.buzz': 'global',
# configure foobar
'foobar.foo': 'foo',
'foobar.bar': 'bar',
# configure fizzbang
'fizzbang.fizz': 'fizz',
'fizzbang.bang': 'bang',
})
foobar()
fizzbang()
# merge the given config with the previous
# reset config instead with tonic.config.reset()
tonic.config.update({
'fizzbang.buzz': 'overwritten'
})
foobar()
fizzbang()
The above will output:
>>> foo bar global
>>> fizz bang global
>>> foo bar global
>>> fizz bang overwritten
5. Instanced Values
prefixing any key in the configuration with an @
marks the
corresponding value as an instanced value.
The requirement for a value that is instanced, is that it is an already registered/configured class or function.
Marking a parameter as instanced means that the function/class is called on every function with a matching parameter, with the resulting value from the call taking its place.
Every time the configuration is updated, these instanced values are lazily recomputed.
import tonic
COUNT = 0
@tonic.config
def counter(step_size=1):
global COUNT
COUNT += step_size
return COUNT
@tonic.config
def print_count(count=None):
print(count)
print_count()
print_count()
tonic.config.set({
'counter.step_size': 2,
'@print_count.count': 'counter'
})
print_count()
print_count()
tonic.config.update({
'counter.step_size': 5,
})
print_count()
print_count()
The above will output the following:
>>> None
>>> None
>>> 2
>>> 2
>>> 7
>>> 7
6. Saving/Loading Configurations
Save and load your configurations using tonic.config.save('file.toml')
and tonic.config.load('file.toml')
7. Multiple Configurations
tonic.config
is an instance of tonic.Config()
you can instantiate your own version for example: my_config = tonic.Config()
and use my_config
instead of tonic.config
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tonic-config-0.1.7.tar.gz
.
File metadata
- Download URL: tonic-config-0.1.7.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8fde1f4d0e1c07d1b9f56c6d80f88ca6bea1c12d912f270c201d683cc62a503 |
|
MD5 | b012dd38c8cc9cb6c8c9b04af9df713a |
|
BLAKE2b-256 | 1d922a47a1aade9b61b56b3e227b0d31810e6be133c216ca261b69665744d288 |
File details
Details for the file tonic_config-0.1.7-py3-none-any.whl
.
File metadata
- Download URL: tonic_config-0.1.7-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1aec0b72b02676718130121b66117ac6b5b7c2f2d5800726b86f62fe6fe4ccb8 |
|
MD5 | cf58ea45582bbd87553f13207d257271 |
|
BLAKE2b-256 | ea2ee9020579c8bc3a785f927e54abe1796b3d865c819f018cb64f6db586a08a |