module translation.node_visitor_translator

Inheritance diagram of pysqllike.translation.node_visitor_translator

Short summary

module pysqllike.translation.node_visitor_translator

One class which visits a syntax tree.

source on GitHub

Classes

class

truncated documentation

CodeNodeVisitor

Defines a visitor which walks though the syntax tree of the code.

Properties

property

truncated documentation

Rows

returns a list of dictionaries with all the elements of the code

Static Methods

staticmethod

truncated documentation

print_node

Debugging purpose.

Methods

method

truncated documentation

__init__

constructor

generic_visit

Overrides generic_visit to check it is not used.

generic_visit_args

Overrides generic_visit to keep track of the indentation and the node parent. The function will add field …

print_tree

Displays the tree of instructions.

push

Pushes an element into a list.

visit

Visits a node, a method must exist for every object class.

visit_

visit_arg

visit_arguments

visit_Assign

visit_Attribute

visit_BinOp

visit_Call

visit_Compare

visit_FunctionDef

visit_Gt

visit_keyword

visit_Load

visit_Lt

visit_Module

visit_Mult

visit_Name

visit_Num

visit_Return

visit_Store

visit_Str

Documentation

One class which visits a syntax tree.

source on GitHub

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

source on GitHub

constructor

source on GitHub

property Rows

returns a list of dictionaries with all the elements of the code

source on GitHub

__init__()

constructor

source on GitHub

generic_visit(node)

Overrides generic_visit to check it is not used.

source on GitHub

generic_visit_args(node, row)

Overrides generic_visit to keep track of the indentation and the node parent. The function will add field row["children"] = visited nodes from here.

Parameters:
  • node – node which needs to be visited

  • row – row (a dictionary)

Returns:

See ast.NodeVisitor.generic_visit

source on GitHub

static print_node(node)

Debugging purpose.

source on GitHub

print_tree()

Displays the tree of instructions.

Returns:

string

source on GitHub

push(row)

Pushes an element into a list.

source on GitHub

visit(node)

Visits a node, a method must exist for every object class.

source on GitHub