You may want to add a pipe operator to have things like
square = lambda x: x ** 2
2 | square | (lambda x: x+1) | print
(this will output 5)
A naive implementation could be:
class PipeWrapper(ast.NodeTransformer):
def visit_BinOp(self, node):
if isinstance(node.op, ast.BitOr):
node.left = self.visit(node.left)
node.right = self.visit(node.right)
return ast.Call(
func=node.right,
args=[node.left],
keywords=[],
)
return self.generic_visit(node)
def transform_ast(tree, **_kwargs):
"""Transforms the Abstract Syntax Tree or a single node"""
tree_or_node = PipeWrapper().visit(tree)
ast.fix_missing_locations(tree_or_node)
return tree_or_node
def add_hook(**_kwargs):
"""Creates and automatically adds the import hook in sys.meta_path"""
hook = import_hook.create_hook(
# transform_source=transform_source,
transform_ast=transform_ast,
hook_name=__name__, # optional
)
return hook
This needs improvement to identify if the pipe operator or the binary or operator is needed, right now 1 | 2 throws error.
You may want to add a pipe operator to have things like
(this will output 5)
A naive implementation could be:
This needs improvement to identify if the pipe operator or the binary or operator is needed, right now
1 | 2throws error.