module onnx_tools.onnx_grammar.node_visitor_translator
#
Short summary#
module mlprodict.onnx_tools.onnx_grammar.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 |
---|---|
Calls method export from the translator class. |
|
Overrides |
|
Overrides |
|
Displays line and column information into a string. |
|
Displays the tree of instructions. |
|
Pushes an element into a list. |
|
Visits a node, a method must exist for every object class. |
|
If an element is not found… |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
A name. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Documentation#
One class which visits a syntax tree.
- class mlprodict.onnx_tools.onnx_grammar.node_visitor_translator.CodeNodeVisitor(translator=None)#
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 from textwrap import dedent from mlprodict.onnx_tools.onnx_grammar import CodeNodeVisitor def norm2(x, y): delta = x - y n = delta ** 2 return n code = dedent(inspect.getsource(norm2)) 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"]))
>>>
Module: FunctionDef: norm2 arguments: arg: x arg: y Assign: Name: delta BinOp: Name: x Sub: Name: y Assign: Name: n BinOp: Name: delta Pow: Num: 2 Return: Name: n
- Parameters:
translator –
CodeTranslator
By default the translator is
OnnxTranslator
.- property Rows#
returns a list of dictionaries with all the elements of the code
- __init__(translator=None)#
- Parameters:
translator –
CodeTranslator
By default the translator is
OnnxTranslator
.
- export(context=None, **kwargs)#
Calls method export from the translator class.
- Parameters:
context – known python needed to run the translated function
kwargs – whatever the method export from the translator class ingests
- Returns:
whatever the method export from the translator class returns
- 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
- make_msg(node)#
Displays line and column information into a string.
- 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.
- visit_(node)#
If an element is not found…
- visit_NameConstant(node)#
A name.