meshed.scrap.collapse_and_expand#
Ideas on collapsing and expanding nodes See “Collapse and expand nodes” discussion: i2mint/meshed#54
Functions
|
Contract function calls in a source code string. |
|
Inverse of collapse_function_calls. |
|
|
|
|
|
Remove the code corresponding to decorators from a source code string. |
Classes
|
To collapse a DAG into a single function |
- class meshed.scrap.collapse_and_expand.CollapsedDAG(dag)[source]#
Bases:
objectTo 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)withfunc(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)withcall(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:
- 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:
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