module translation.node_visitor_translator
¶
Short summary¶
module pysqllike.translation.node_visitor_translator
One class which visits a syntax tree.
Classes¶
class |
truncated documentation |
---|---|
Defines a visitor which walks though the syntax tree of the code. |
Properties¶
property |
truncated documentation |
---|---|
returns a list of dictionaries with all the elements of the code |
Static Methods¶
staticmethod |
truncated documentation |
---|---|
Debugging purpose. |
Methods¶
method |
truncated documentation |
---|---|
constructor |
|
Overrides |
|
Overrides |
|
Displays the tree of instructions. |
|
Pushes an element into a list. |
|
Visits a node, a method must exist for every object class. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Documentation¶
One class which visits a syntax tree.
- class pysqllike.translation.node_visitor_translator.CodeNodeVisitor¶
Bases:
NodeVisitor
Defines a visitor which walks though the syntax tree of the code.
Get the tree of a simple function
The following code uses Python syntax but follows a SQL logic.
<<<
import ast import inspect import textwrap from pysqllike.translation.node_visitor_translator import CodeNodeVisitor def myjob(input): iter = input.select(input.age, input.nom, age2=input.age2 * input.age2) wher = iter.where((iter.age > 60).Or(iter.age < 25)) return wher code = textwrap.dedent(inspect.getsource(myjob)) node = ast.parse(code) v = CodeNodeVisitor() v.visit(node) for r in v.Rows: print("{0}{1}: {2}".format(" " * r["indent"], r["type"], r["str"]))
>>>
[runpythonerror] Traceback (most recent call last): File "<stdin>", line 17, in <module> File "/usr/local/lib/python3.9/inspect.py", line 1024, in getsource lines, lnum = getsourcelines(object) File "/usr/local/lib/python3.9/inspect.py", line 1006, in getsourcelines lines, lnum = findsource(object) File "/usr/local/lib/python3.9/inspect.py", line 835, in findsource raise OSError('could not get source code') OSError: could not get source code
constructor
- property Rows¶
returns a list of dictionaries with all the elements of the code
- __init__()¶
constructor
- generic_visit(node)¶
Overrides
generic_visit
to check it is not used.
- generic_visit_args(node, row)¶
Overrides
generic_visit
to keep track of the indentation and the node parent. The function will add fieldrow["children"] = visited
nodes from here.- Parameters:
node – node which needs to be visited
row – row (a dictionary)
- Returns:
See
ast.NodeVisitor.generic_visit
- static print_node(node)¶
Debugging purpose.
- print_tree()¶
Displays the tree of instructions.
- Returns:
string
- push(row)¶
Pushes an element into a list.
- visit(node)¶
Visits a node, a method must exist for every object class.