Standard Library

Here we have a collection of standard functions that help writing stringdale diagrams effectively

Nested Conditions made simple


source

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_module_2(x):
    return x%2==0

is_input_even = Condition(is_module_2,'(0=input)',name='input is even')

assert is_input_even({"input": 2})
assert not is_input_even({"input": 3})
def is_sum_42(x,y):
    return x+y==42

is_io_42 = Condition(is_sum_42,'(x=input,y=output)',name='i+o=42?')

assert is_io_42({"input": 2,'output':40})
C = Condition

complex_cond = C("^3",'(0=input)',name='input is 3') & C("^5",'(0=output)',name='output is 5') | C("^2",'(0=input)')
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"})
simple_cond = C("^2")
assert not simple_cond({"input": "245", "output": "567"})
assert not simple_cond({"input": "245"})
assert simple_cond("245")

Structuring objects


source

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.*

StructureJson('a.b.c=d.e.f','a.b.d=d.x')
StructureJson(a.b.c=d.e.f, a.b.d=d.x)
source_obj = {'d':{'e':{'f':'foo'},'x':'bar'}}
out = StructureJson('a.b.c=father.d.e.f','a.b.d=father.d.x')(father=source_obj)
assert out == {'a':{'b':{'c':'foo','d':'bar'}}}

JsonRenderer


source

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%}'
}

rend = JsonRenderer(template_json,age=30,cities=['SF','LA','NY'])
rend
JsonRenderer(missing_keys={'name'})
out = rend(name='Dean')
assert out == {'name': 'Dean', 'age': '30', 'city_path': 'SF -  LA -  NY -'},out

Export