syft.ast¶
Syft’s Abstract Syntax Tree (AST) submodule is responsible for remote call executions.
An AST is a tree that maps function calls to their exact path, and knows what to do with that node in tree.
Example: Suppose we want to append an object to a List. This means that we need to know where we could find the append method, so we need to know the following chain:
- globals <- the global scope or the entry point of execution (hidden)
- syft <- the syft module
- lib <- a submodule of syft
- List <- the class we were looking for
append <- the method we were looking for
When performing remote execution, this lookup path has to be resolved as well. This is where the AST submodule comes in handy. AST is responsible for:
Remote execution.
Local execution.
A. Remote Execution Remote execution can be performed only when an AST has been constructed with a client. Check syft/core/node to be familiar with the roles of clients and nodes. Each valid action on the AST triggers an Action (GetSetStaticAttributeAction, GetSetPropertyAction, etc). This kind of actions requires: The path on resolving the required node (on the above example, the path is syft.lib.List.append), the object on which to perform it (given by the __self attribute and from id_at_location if executed on a pointer) and the attributes if needed.
B. Local Execution After a call has been made, the remote execution starts. For this, we need a local handler for the result of the remote execution through a Pointer. The AST is responsible for generating all the permitted methods and attributes on a Pointer and the return type of the performed action.
The existing types of nodes are: * a Globals, which is the entry point of an execution, from which point on we can only access Modules.
a Callable, which can be a node for a method, a static method, a function, or a constructor.
This node can no longer have any attributes.
a Class, which is a node that represents a Python Class. This node can contain methods - Callable,
static methods - Callable, class methods - Callable, slot attributes - StaticAttribute, properties - Property, enum attributes - EnumAttribute.
a Module, which represents a Python file/module. This node can have attributes such as global
variables - StaticAttribute, global functions - Callable and classes - Klass.
a StaticAttribute - represents attribute of a Class or of a Module. This node cannot have any attributes.
This can be remotely get and set.
a Property - represents a @property object of a class. This node cannot have any attributes, which means
that this node is essentially a “leaf”. This node can perform get and set remotely.
an EnumAttribute - represents the fields generated by an Enum. This node cannot have any attributes,
meaning that this node is essentially a “leaf”. This node can perform get remotely.
Functions
|
Parse a list of classes and register each class to its corresponding parent object in the AST path. |
|
|
|
Parse a list of methods and register each method to its corresponding parent object in the AST path. |
|
Parse a list of modules and register each module to its corresponding parent object in the AST path. |
|
Return the parent of a given path. |
- syft.ast.get_parent(path, root)[source]¶
Return the parent of a given path.
- Parameters
path – The full path to an object.
root – The collection of frameworks held in the global namespace.
- Returns
The parent module or class.
- Raises
ValueError – If parent is not a class or module
Examples
For instance, given the syft project root directory, the parent to the path syft.lib.python.Int is python.
- syft.ast.add_modules(ast, modules)[source]¶
Parse a list of modules and register each module to its corresponding parent object in the AST path.
- Parameters
ast – The global AST.
modules – A list of modules, either a path in string format or a tuple of the path in string and a reference.
- syft.ast.add_classes(ast, paths)[source]¶
Parse a list of classes and register each class to its corresponding parent object in the AST path.
- Parameters
ast – The global AST.
paths – A list of classes, each of which is a tuple of the path, the return type, and its reference.
- syft.ast.add_methods(ast, paths)[source]¶
Parse a list of methods and register each method to its corresponding parent object in the AST path.
- Parameters
ast – The global AST.
paths – A list of methods, each of which is a tuple of the method’s path and its return type.
Modules
This module contains Attribute, an interface of a generic node in the AST. |
|
This module contains Callable, an AST node representing a method (can be static), global function, or constructor which can be directly executed. |
|
This module contains EnumAttribute, an AST node representing the attributes of a Python Enum which are only gettable, not settable. |
|
This module contains Global, an AST node representing the collection of frameworks held in the global namespace. |
|
This module contains Class attribute,an AST node representing a class. |
|
This module contains Module attribute representing a module which contains other models or callables. |
|
This module contains Property attribute representing property objects which implements getter and setter objects. |
|
This module contains StaticAttribute, an AST node representing a method, function, or constructor which can be directly executed. |
|
This module contains utility funtions for Syft's AST submodule. |