meshed.scrap.collapse_and_expand#

Ideas on collapsing and expanding nodes See “Collapse and expand nodes” discussion: i2mint/meshed#54

Functions

collapse_function_calls(src[, ...])

Contract function calls in a source code string.

expand_function_calls(src[, call_func_name, ...])

Inverse of collapse_function_calls.

expand_nodes(dag[, nodes, is_node, ...])

get_src_string(src)

remove_decorator_code(src[, decorator_names])

Remove the code corresponding to decorators from a source code string.

Classes

CollapsedDAG(dag)

To collapse a DAG into a single function

class meshed.scrap.collapse_and_expand.CollapsedDAG(dag)[source]#

Bases: object

To collapse a DAG into a single function

This is useful for when you want to use a DAG as a function, but you don’t want to see all the arguments.

meshed.scrap.collapse_and_expand.collapse_function_calls(src, call_func_name='call', *, rm_decorator='code_to_dag', include=None)[source]#

Contract function calls in a source code string.

That is, in source code, or a dag made from code_to_dag, replace calls of the form call(func, arg) with func(arg).

Note

Doesn’t work with arbitrary DAG src, only those made from code_to_dag.

meshed.scrap.collapse_and_expand.expand_function_calls(src, call_func_name='call', *, include=None)[source]#

Inverse of collapse_function_calls. It replaces calls of the form func(arg) with call(func, arg), except when the function call is part of a function definition header. If include is None, it expands all function calls. If include is a list of function names, only those functions are expanded. If include is a callable, it’s used as a filter function.

Return type:

str

meshed.scrap.collapse_and_expand.remove_decorator_code(src, decorator_names=None)[source]#

Remove the code corresponding to decorators from a source code string. If decorator_names is None, will remove all decorators. If decorator_names is an iterable of strings, will remove the decorators with those names.

Return type:

str

Examples

>>> src = '''
... @decorator
... def func():
...     pass
... '''
>>> print(remove_decorator_code(src))
def func():
    pass
>>> src = '''
... @decorator1
... @decorator2
... def func():
...     pass
... '''
>>> print(remove_decorator_code(src, "decorator1"))
@decorator2
def func():
    pass