def is_module_2(x):
return x%2==0
= Condition(is_module_2,'(0=input)',name='input is even')
is_input_even
assert is_input_even({"input": 2})
assert not is_input_even({"input": 3})
Standard Library
Here we have a collection of standard functions that help writing stringdale diagrams effectively
Nested Conditions made simple
Condition
Condition (func:Union[Callable,str], mapping:Optional[str]=None, name:Optional[str]=None)
*A utility function for creating condition functions using stringdale’s port mapping logic.
Args: func: A function or string to match against the input. If string, it will be interpreted as a regex pattern to match the input against. mapping: A mapping of the input to the function. by default, the input is the first argument of the function. name: A name for the condition. by default, is func_name->mapping_string*
def is_sum_42(x,y):
return x+y==42
= Condition(is_sum_42,'(x=input,y=output)',name='i+o=42?')
is_io_42
assert is_io_42({"input": 2,'output':40})
= Condition
C
= C("^3",'(0=input)',name='input is 3') & C("^5",'(0=output)',name='output is 5') | C("^2",'(0=input)')
complex_cond complex_cond
((input is 3 & output is 5) | Condition( (0=input) ) -> (/^2/))
assert complex_cond({"input": "345", "output": "567"})
assert complex_cond({"input": "244", "output": "567"})
assert not complex_cond({"input": "145", "soutput": "567"})
assert not complex_cond({"input": "345", "output": "67"})
assert not complex_cond({"not_input": "145"})
assert complex_cond({ "input": "244"})
= C("^2")
simple_cond assert not simple_cond({"input": "245", "output": "567"})
assert not simple_cond({"input": "245"})
assert simple_cond("245")
Structuring objects
StructureJson
StructureJson (*assignments)
*A class for restructuring JSON objects by nested paths This class allows restructuring JSON objects by specifying path assignments in the form ‘target_path=source_path’. It parses these assignments and creates a new JSON object with the specified structure.
Args: assignments: Variable number of strings specifying path assignments in the format ‘target_path=source_path’
Returns: A new JSON object with the specified structure.*
'a.b.c=d.e.f','a.b.d=d.x') StructureJson(
StructureJson(a.b.c=d.e.f, a.b.d=d.x)
= {'d':{'e':{'f':'foo'},'x':'bar'}}
source_obj = StructureJson('a.b.c=father.d.e.f','a.b.d=father.d.x')(father=source_obj)
out assert out == {'a':{'b':{'c':'foo','d':'bar'}}}
JsonRenderer
JsonRenderer
JsonRenderer (json_obj, **kwargs)
*A class for rendering JSON objects with nested jinja2 templates.
Allows setting template variables both during init and when calling the object.
Args: json_obj: A JSON object to render. **kwargs: Context variables to use in the rendering.
Returns: A rendered JSON object.*
= {
template_json 'name': '{{name}}',
'age': '{{age}}',
'city_path': '{%for city in cities%} {{city}} - {%endfor%}'
}
= JsonRenderer(template_json,age=30,cities=['SF','LA','NY'])
rend rend
JsonRenderer(missing_keys={'name'})
= rend(name='Dean')
out assert out == {'name': 'Dean', 'age': '30', 'city_path': 'SF - LA - NY -'},out