Reference guide¶
GParser¶
- class gparser.GParser(validation: bool = True, lib: Optional[str] = None, patternmatcher: Optional[gpatternmatcher.GPatternMatcher] = None, graphbuilder: Optional[ggraphbuilder.GGraphBuilder] = None, graphtype: Literal['GDGraph', 'GAGraph'] = 'GDGraph', databuilder: Optional[gdatabuilder.GDataBuilder] = None, grammar: Optional[Union[lark.load_grammar.Grammar, str, IO[str]]] = None, parser: Literal['earley', 'lalr', 'cyk', 'auto'] = 'lalr', lexer: Union[Type[lark.lexer.Lexer], Literal['auto', 'standard', 'contextual', 'dynamic', 'dynamic_complete']] = 'contextual', start: Optional[Union[str, List[str]]] = 'sequence', cache: Union[bool, str] = '.lark-cache', debug: bool = False)¶
The GParser class, inherited from
lark.Lark
- Parameters
validation – If True, run indices validation after parsing
lib – The patternlibrary for the Pattern Matching. (see
GPatternMatcher.lib
). Default:None
patternmatcher (GPatternMatcher) – The Pattern Matching class. If
None
, usingGPatternMatcher
. Default:None
graphbuilder (GGraphBuilder) – The graph builder class. If
None
, usingGGraphBuilder
. Default:None
databuilder (GDataBuilder) – The data builder class. If
None
, usingGDataBuilder
. Default:None
grammar – The grammar file. Default:
grammar.lark
parser – The parser algorithm. Default:
lalr
lexer – The lexer. Default:
contextual
start – The start point to the parsing. Default:
sequence
cache – With
cache
parsing is x2~x3 faster! Default:True
debug – The
debug
option. Default:False
- build_data(data: Union[List, Tuple, gtree.GTree, str], **kwargs) Union[List, gdata.GData] ¶
Builds
GData
from thedata
and returns the result- Parameters
data – The data to the graph building. May be
GTree
,list[GTree]
or raw data, from thatGTree
will be parsed.- Returns
The builded
List[GData]
- build_graph(data: Union[List, Tuple, gtree.GTree], pattern_matching: bool = False, **kwargs) Optional[Union[List, Tuple, gagraph.GAGraph, gdgraph.GDGraph, gdgraph.GDGraphHelper]] ¶
Builds
GAGraph
orGDGraph
from thedata
and returns the result- Parameters
data – The data to the graph building. May be
GTree
,list[GTree]
or raw data, from thatGTree
will be parsed.pattern_matching (bool) – If
True
makesPattern Matching
fromlib
orself.lib
(is setted andlib
arg isNone
). Default:False
- Returns
The builded
GAGraph
orGDGraph
- convert(data: lark.tree.Tree) gtree.GTree ¶
- convert(data: lark.lexer.Token) gtree.GToken
- convert(data: Any) Any
Convers data to the
GTree
orGToken
- property databuilder: gdatabuilder.GDataBuilder¶
The
self._databuilder
getter
- property graphbuilder: ggraphbuilder.GGraphBuilder¶
The
self._graphbuilder
getter
- match(data: Union[List, Tuple, gdgraph.GDGraph, gdgraph.GDGraphHelper], lib: Optional[str] = None) Union[List, Tuple, gdgraph.GDGraph, gdgraph.GDGraphHelper] ¶
Makes
Pattern Matching
for thedata
and returns the result- Parameters
data – The data to
Pattern Matching
. May beGTree
,list[GTree]
or raw data, from thatGTree
will be parsed.lib – The pattern patternlibrary. If
None
using fromGParser.__init__
. Default:None
- Returns
The Pattern Matched GTree
- parse(text: str, start: Optional[str] = None, on_error: Optional[Callable] = None, make_identifiers: bool = True, reset_counts: bool = True) Union[gtree.GTree, List, Tuple] ¶
Reimplemented
lark.tree.Tree.parse
method- Parameters
text (str) – Text to be parsed
start (str) – Required if
GParser
was given multiple possible start symbols (using the start option). Default:None
on_error (function) – If provided, will be called on
UnexpectedToken
error. ReturnTrue
to resume parsing. Default:None
make_identifiers (bool) – If
True
makes unique identifiers for all the parts of thisGTree
(seeGTree.make_identifiers
). Default:True
reset_counts (bool) – If
True
resets the counts for the making identifiers (seeGTree.reset_counts
). Default:True
- Returns
Returns parsed
GTree
- pattern_matching(data: Union[List, Tuple, gdgraph.GDGraph, gdgraph.GDGraphHelper], lib: Optional[str] = None) Union[List, Tuple, gdgraph.GDGraph, gdgraph.GDGraphHelper] ¶
Makes
Pattern Matching
for thedata
and returns the result- Parameters
data – The data to
Pattern Matching
. May beGTree
,list[GTree]
or raw data, from thatGTree
will be parsed.lib – The pattern patternlibrary. If
None
using fromGParser.__init__
. Default:None
- Returns
The Pattern Matched GTree
- property patternmatcher: gpatternmatcher.GPatternMatcher¶
The
self._patternmatcher
getter
- tree_to_file(data: Union[gtree.GTree, lark.tree.Tree], filename: str, rankdir: Literal['TB', 'LR', 'BT', 'RL'] = 'LR', **kwargs) None ¶
Saves the data to
filename
- Parameters
data –
GTree
orlark.tree.Tree
to savingfilename (str) – The filename to saving
rankdir (str) – Possible values are “TB”, “LR” (default), “BT”, “RL”, corresponding to directed graphs drawn from top to bottom, from left to right, from bottom to top, and from right to left, respectively.
kwargs –
kwargs
can be any graph attribute (e.g.dpi=200
). For a list of possible attributes, see https://www.graphviz.org/doc/info/attrs.html.
GPatternMatcher¶
- class gpatternmatcher.GPatternMatcher(parsemethod: Optional[Callable] = None, datamethod: Optional[Callable] = None, lib: Optional[str] = None, debug: bool = False, **kwargs)¶
The GPatternMatcher class
Designed for the Pattern Matching, in other words, to search the library for special rules for replacing any constructs in the tree, thereby simplifying the tree.
Use match-method for the Pattern Matching.
- Parameters
parser – The parser class, that provides parse-method
lib – The library with patterns
Note
Now supports only YAML libraries! Example:
patternname: expr: "DSL @ Expression * to + Pattern - Matching" label: "label"
- build_pgraphs() list ¶
Builds the list of the patterns GDGraph`s `GPatternMatcher.pgraphs
- property lib: Optional[str]¶
The library with patterns
- match(graph: Union[gdgraph.GDGraph, gdgraph.GDGraphHelper]) Union[gdgraph.GDGraph, gdgraph.GDGraphHelper] ¶
Find patterns in the lib and match corresponding object in the tree
- Parameters
tree – The graph to pattern matching
- read_patterns(lib: Optional[str] = None) dict ¶
Reads the patterns from lib and returns as dict
- Parameters
lib – The library with patterns
Note
Now supports only YAML libraries!
GGraphBuilder¶
- class ggraphbuilder.GGraphBuilder(databuilder: Optional[gdatabuilder.GDataBuilder] = None, graphtype: Literal['GDGraph', 'GAGraph'] = 'GDGraph', debug: bool = False)¶
The
GGraphBuilder
classDesigned for the building of
GDGraphHelper
orGAGraph
fromGTree
,GData
orSequence[GData]
- Parameters
databuilder – The GData builder class. If
None
, use a new instance ofGDataBuilder
. Default:None
graphtype – The type of the graph:
GDGraph
orGAGraph
debug – The debug option
- abuild(data: Union[gtree.GTree, gdata.GData, Sequence], orig: Optional[gagraph.GAGraph] = None, **kwargs) gagraph.GAGraph ¶
Builds
GAGraph
fromGTree
orGData
- Parameters
data – The data from which the
GAGraph
will be collected. May beGTree
,GData
orSequence
.orig – If
orig
isGAGraph
, starting from the graph and modifying it while the building graph fromdata
. Else create a new instance ofGAGraph
. Default:None
- property databuilder: gdatabuilder.GDataBuilder¶
The class to build GData objects
- dbuild(_data: Union[gtree.GTree, gdata.GData, Sequence], orig: Optional[gdgraph.GDGraph] = None, **kwargs) Optional[gdgraph.GDGraph] ¶
Builds
GDGraph
fromGTree
orGData
- Parameters
data – The data from which the
GDGraph
will be collected. May beGTree
,GData
orSequence
.orig – If
orig
isGDGraph
, starting from the graph and modifying it while the building graph fromdata
. Else create a new instance ofGDGraph
. Default:None
- property debug: bool¶
If
True
do not collapse extra nodes
- property graphtype: str¶
If
True
do not collapse extra nodes
- togagraph(gdgraph: Union[gdgraph.GDGraph, gdgraph.GDGraphHelper], **kwargs) gagraph.GAGraph ¶
Converts GDGraph or GDGraphHelper to GAGraph
GDataBuilder¶
- class gdatabuilder.GDataBuilder(validation: bool = False, debug: bool = False)¶
The
GDataBuilder
classDesigned for the building of
GData
fromGTree
orlist[GTree]
.Use
build
for the buildingGData
fromGTree
.- Parameters
debug – If
True
do not collapse extra nodes. Default:False
- build(tree: Union[gtree.GTree, List, Tuple], assignments: Optional[Dict[str, gdata.GData]] = None, instances: Optional[Dict[str, int]] = None, **kwargs) Union[gdata.GData, List[gdata.GData]] ¶
Building
GData
orList[GData]
fromGTree
orList[GTree]
- property debug: bool¶
If
True
do not collapse extra nodes
- property validation: bool¶
If
True
call the validation procedure after a parsing
GTree¶
- class gagraph.GAGraph(thing=None, filename=None, data=None, string=None, handle=None, name='', strict=True, directed=False, **attr)¶
Inherited class from
pygraphviz.AGraph
Reimplemented to work with
GData
class- add_edge(u: Union[gdata.GData, Tuple[gdata.GData, gdata.GData], Any], v: Optional[gdata.GData] = None, key: Optional[str] = None, **attr) pygraphviz.agraph.Edge ¶
Adding edge between nodes to the graph
Reimplemented for the work with
GData
-class
- add_node(data: Any = 1, **attr) pygraphviz.agraph.Node ¶
Adding node to the graph
Reimplemented for the work with
GData
-class
GData¶
- class gdata.GData(orig: Optional[Dict] = None)¶
The GData class
The class is like a python
dict
:name
(str);sname
(str);type
(str);namespace
(str);label
(dict):default
(str);...
(str);
instance
(int);arguments
(list);indices
(dict):explicit
(list);implicit
(list);reduction
(list).
sname <-> storage name .uname() <-> unique name
- Parameters
orig – Initialization data (
GData
,dict
,str
). Default:None
- argnames() list ¶
Returns the arguments list without instances, for example, Transf_1 -> Transf, Object_1_2_3 -> Object_1_2
- copy() gdata.GData ¶
Returns a copy of the object
- classmethod from_str(data: str) gdata.GData ¶
Build the GData object from string
- set_sname(val: Optional[str] = None) None ¶
Sets the storage name sname
- uname() str ¶
If
type
istransformation
returnsname_instance
-string, else returnsname
GDGraph¶
- class gdgraph.GDGraph(name: str, data: Any, children: List[GDGraph] = [])
The light graph class. The main idea is an absence of nodes and self-similarity, i.e. the children (like nodes, but it would be more correct to say the inner layers) are GDGraph`s as the head layer. Any `GDGraph instance contains the name (usual the same as data[“name”]), the data field (see GData class) and children. Supports some kind of caching (see GDMeta class), contains a pretty print method, a dict representation, shape and depth methods. It is possible to get a graph by the name or list of graphs at the some layer via the get method or using brackets []. The class does not contain any graphical representation, but it is possible to convert GDGraph class to GAGraph (via GGraphBuilder.togagraph method), which has the PyGraphviz features (see ggraphbuilder and gagraph modules).
- Parameters
name – The name of the graph
data – The data of the graph
children – The list of the children
- asdict(include_data: bool = False) dict
Returns the dict representation of the graph
- Parameters
include_data – If True, includes data field, else uses only name and children fields
- depth() int
Returns the depth (maximum layer number) of the graph
- classmethod from_gdata(data: gdata.GData, children: Optional[Sequence[gdgraph.GDGraph]] = None) gdgraph.GDGraph
Build the class from
GData
- Parameters
data – The GData object
children – The children (inputs, nodes) list
- get(key: Union[int, str], default: Optional[Any] = None) Optional[Union[gdgraph.GDGraph, List, Any]]
Returns the graph or list of graphs by layer number or name
- Parameters
key – The layer number or name of the graph
default – If the graph does not contain the desired element(s), returns default value
- getbylayer(layer: int) Union[gdgraph.GDGraph, List]
Returns a graph or list of graphs corresponding to the layer number. It is possible to use negative indexes to get objects from the end
- Parameters
layer – The layer number of the graph to find
- getbyname(name: str) gdgraph.GDGraph
Returns a graph corresponding to the name
- Parameters
name – The name of the graph to find
- isliketo(other: Any, strict_order: bool = True) bool
Checks the name and arguments of the objects
- Parameters
other – The object to compare
strict_order – If False do not check the children (arguments) order
- iter_subgraphs()
Depth-first iteration. Iterates over all the subgraphs, never returning to the same subgraph twice.
- iter_subgraphs_topdown()
Breadth-first iteration. Iterates over all the subgraphs.
- objectscount() int
Returns all the count of the inner graphs, including itself
- print(include_data: bool = False) None
Pretty print of the graph. The dict representation (see GDGraph.asdict) is used
- Parameters
include_data – If True, includes data field, else uses only name and children fields
- shape() tuple
Returns the shape of the graph
- todatalist() list
Returns all the GData objects: the parent data and all the data from the children
- tolist() list
Returns all the GDGraph objects: the parent GDGraph and all the graphs from the children
- class gdgraph.GDGraphHelper(graphs: Optional[Union[Sequence[Any], Any]] = None, strict: bool = True, debug: bool = False, **kwargs)
The helper to work with GDGraph objects. Contains the list of graphs and the dict with the same graphs as values and the names as keys.
It is possible to modify graphs by the list or by the dict, but it is important to update the dict after adding new graph to the graph: using the argument bind of the add_graph and add_graphs methods (by default) or by the direct calling the bind_graphs method.
There is no edges, but every graph has children. The method add_graphs, if bind=True, collecting children by the iteration of the data[“arguments”] of the every graph. If a single graph is added using the method add_graph, and it is necessary to iterate through all the graphs and update the children, then a direct call of the bind_children method is necessary.
- add_graph(data: Any, name: Optional[str] = None, children: Optional[Sequence[gdgraph.GDGraph]] = None, bind: bool = True) gdgraph.GDGraph
Adding the graph to the graph
- Parameters
data – The data of the graph
name – The name of the graph
children – The list of the children
bind – If True, adding the graph to the dict GDGraphHelper.graphsdict
- add_graphs(graphs: Sequence[Any], bind: bool = True) None
Adding the list of the graphs
- Parameters
graphs – The graphs sequence
bind – If True, filling also the dict of the graphs GDGraphHelper.graphsdict and binding children
- asdict(include_data: bool = False) dict
Returns the dict representation of the graph
- Parameters
include_data – If True, includes data field, else uses only name and children fields of a graph
- bind_children() None
Collecting and adding children to all the graphs from the graph.data[“arguments”]
- bind_graphs() dict
Filling the dict of the graphs graphsdict
- depths() tuple
Returns the tuple of the depths of the GDGraphHelper.graphs (see GDGraph.depth)
- main_graph(validate: bool = True) Optional[gdgraph.GDGraph]
Returns the deepest graph (see GDGraph.depth)
- print(include_data: bool = False) None
Pretty print of the graph. The dict representation (see GDGraphHelper.asdict) is used
- Parameters
include_data – If True, includes data field, else uses only name and children fields of a graph
- shapes() tuple
Returns the tuple of the shapes of the GDGraphHelper.graphs (see GDGraph.shape)
- todatalist() List[gdata.GData]
Returns the list of the GData objects
- validate(graph: gdgraph.GDGraph, recursive: bool = True) None
Check for equivalence of the children with the data[“arguments”]
- validateall(recursive: bool = True) None
Validation of all the graphs
GAGraph¶
- class gagraph.GAGraph(thing=None, filename=None, data=None, string=None, handle=None, name='', strict=True, directed=False, **attr)
Inherited class from
pygraphviz.AGraph
Reimplemented to work with
GData
class- add_edge(u: Union[gdata.GData, Tuple[gdata.GData, gdata.GData], Any], v: Optional[gdata.GData] = None, key: Optional[str] = None, **attr) pygraphviz.agraph.Edge
Adding edge between nodes to the graph
Reimplemented for the work with
GData
-class
- add_node(data: Any = 1, **attr) pygraphviz.agraph.Node
Adding node to the graph
Reimplemented for the work with
GData
-class