Quick Start¶
Start¶
To create any object, we need to specify initial state and steps to apply on. Pycnfg offers pattern to pipeline-wise code execution, that naturally allows to control all parameters via single configuration dictionary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | """Configuration example to produce object."""
import pycnfg
class CustomProducer(pycnfg.Producer):
"""Specify custom methods to produce object."""
def __init__(self, objects, oid):
# Mandatory.
super().__init__(objects, oid)
def set(self, obj, key, val=42):
obj[key] = val
return obj
def print(self, obj, key='a'):
print(obj[key])
return obj
def method_id(self, obj, *args, **kwargs):
# Some logic..
return obj
# Configuration.
# Set `init` object state {'a': 7}.
# Set `producer` class CustomProducer.
# Set `steps` to execute.
CNFG = {
'section_1': {
'conf_1': {
'init': {'a': 7},
'producer': CustomProducer,
'steps': [
('set', {'key': 'b', 'val': 42}),
('print', {}),
('print', {'key': 'b'}),
('method_id', {}),
],
},
# 'conf_2': {...}
}
# 'section_2': {...}
}
if __name__ == '__main__':
# Execute configuration(s) in priority.
objects = pycnfg.run(CNFG)
# => 7
# => 42
# Storage for produced object(s).
print(objects['section_1__conf_1'])
# => {'a': 7, 'b': 42}
|
There are could be arbitrary number of sections/configurations. Cross-configurations interaction goes through special storage objects.