Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
GNA Parser
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Redmine
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
gna
GNA Parser
Commits
f4eabec8
Commit
f4eabec8
authored
3 years ago
by
Tsegelnik Nikita
Browse files
Options
Downloads
Patches
Plain Diff
renaming to more logical names
parent
2dc74d38
No related branches found
Branches containing commit
No related tags found
1 merge request
!19
New version
Pipeline
#16935
failed
3 years ago
Stage: tests
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
source/gdgraph.py
+143
-143
143 additions, 143 deletions
source/gdgraph.py
source/ggraphbuilder.py
+20
-20
20 additions, 20 deletions
source/ggraphbuilder.py
source/gparser.py
+9
-9
9 additions, 9 deletions
source/gparser.py
source/gpatternmatcher.py
+6
-9
6 additions, 9 deletions
source/gpatternmatcher.py
with
178 additions
and
181 deletions
source/gdgraph.py
+
143
−
143
View file @
f4eabec8
...
...
@@ -25,75 +25,75 @@ class GDMeta:
@attr.s
(
repr
=
False
)
class
GD
Node
:
class
GD
Graph
:
"""
The data class for the ``GDGraph
`` node
s
The data class for the ``GDGraph
Helper`` graph
s
:param name: The name of the
node
:param data: The data of the
node
:param
inputs
: The list of the
inputs
:param name: The name of the
graph
:param data: The data of the
graph
:param
children
: The list of the
children
"""
name
:
str
=
attr
.
ib
(
validator
=
attr
.
validators
.
instance_of
(
str
))
data
:
Any
=
attr
.
ib
()
inputs
:
List
[
GD
Node
]
=
attr
.
ib
(
children
:
List
[
GD
Graph
]
=
attr
.
ib
(
validator
=
attr
.
validators
.
instance_of
(
list
),
default
=
[]
)
meta
:
GDMeta
=
GDMeta
()
@classmethod
def
from_gdata
(
cls
,
data
:
GData
,
inputs
:
Optional
[
Sequence
[
GD
Node
]]
=
None
)
->
GD
Node
:
cls
,
data
:
GData
,
children
:
Optional
[
Sequence
[
GD
Graph
]]
=
None
)
->
GD
Graph
:
"""
Build the class from ``GData``
"""
if
not
inputs
:
inputs
=
[]
elif
not
isinstance
(
inputs
,
List
):
inputs
=
list
(
inputs
)
return
cls
(
data
.
uname
(),
data
,
inputs
)
if
not
children
:
children
=
[]
elif
not
isinstance
(
children
,
List
):
children
=
list
(
children
)
return
cls
(
data
.
uname
(),
data
,
children
)
def
__copy__
(
self
)
->
GD
Node
:
def
__copy__
(
self
)
->
GD
Graph
:
obj
=
type
(
self
).
__new__
(
self
.
__class__
)
obj
.
__dict__
.
update
(
self
.
__dict__
)
return
obj
def
copy
(
self
)
->
GD
Node
:
def
copy
(
self
)
->
GD
Graph
:
return
self
.
__copy__
()
def
__repr__
(
self
)
->
str
:
return
f
"
GD
Node
(name=
{
self
.
name
}
,
inputs=
{
self
.
inputs
}
)
"
return
f
"
GD
Graph
(name=
{
self
.
name
}
,
children=
{
self
.
children
}
)
"
def
__str__
(
self
)
->
str
:
return
self
.
__repr__
()
def
asdict
(
self
,
include_data
:
bool
=
False
)
->
dict
:
"""
Returns the dict representation of the
node
Returns the dict representation of the
graph
:param include_data: If `True`, includes `data` field,
else uses only `name` and `
inputs
` fields
else uses only `name` and `
children
` fields
"""
if
not
include_data
:
return
{
"
name
"
:
self
.
name
,
"
inputs
"
:
[
obj
.
asdict
(
include_data
)
for
obj
in
self
.
inputs
],
"
children
"
:
[
obj
.
asdict
(
include_data
)
for
obj
in
self
.
children
],
}
else
:
return
{
"
name
"
:
self
.
name
,
"
data
"
:
self
.
data
,
"
inputs
"
:
[
obj
.
asdict
(
include_data
)
for
obj
in
self
.
inputs
],
"
children
"
:
[
obj
.
asdict
(
include_data
)
for
obj
in
self
.
children
],
}
def
print
(
self
,
include_data
:
bool
=
False
)
->
None
:
"""
Pretty print of the
node
.
The dict representation (see `GD
Node
.asdict`) is used
Pretty print of the
graph
.
The dict representation (see `GD
Graph
.asdict`) is used
:param include_data: If `True`, includes `data` field,
else uses only `name` and `
inputs
` fields
else uses only `name` and `
children
` fields
"""
pprint
(
self
.
asdict
(
include_data
))
...
...
@@ -101,7 +101,7 @@ class GDNode:
self
,
force
:
bool
=
False
,
strict
:
bool
=
True
,
debug
:
bool
=
False
)
->
bool
:
"""
Check for equivalence of the `
inputs
` with the `data[
"
arguments
"
]`
Check for equivalence of the `
children
` with the `data[
"
arguments
"
]`
If `strict=True` an exception will be raised on the validation failure,
else if `debug=True` a message will be printed instead of the exception
...
...
@@ -117,18 +117,18 @@ class GDNode:
return
self
.
meta
.
validity
self
.
meta
.
is_validated
=
True
self
.
meta
.
validity
=
False
if
len
(
self
.
inputs
)
!=
len
(
self
.
data
[
"
arguments
"
]):
msg
=
"
The lenghts of
inputs
and data[
'
arguments
'
] are not equal!
"
"
Seems like the binding of
inputs
is not called!
"
if
len
(
self
.
children
)
!=
len
(
self
.
data
[
"
arguments
"
]):
msg
=
"
The lenghts of
children
and data[
'
arguments
'
] are not equal!
"
"
Seems like the binding of
children
is not called!
"
if
strict
:
raise
ValueError
(
msg
)
elif
debug
:
print
(
f
"
DebugInfo:
{
msg
}
"
)
self
.
meta
.
validity
=
False
return
self
.
meta
.
validity
for
node
in
self
.
inputs
:
if
node
.
name
not
in
self
.
data
[
"
arguments
"
]:
msg
=
f
"
The
node
'
{
node
.
name
}
'
not in the arguments
"
for
graph
in
self
.
children
:
if
graph
.
name
not
in
self
.
data
[
"
arguments
"
]:
msg
=
f
"
The
graph
'
{
graph
.
name
}
'
not in the arguments
"
f
"
{
self
.
data
[
'
arguments
'
]
}
!
"
if
strict
:
raise
ValueError
(
msg
)
...
...
@@ -143,37 +143,37 @@ class GDNode:
self
,
force
:
bool
=
False
,
strict
:
bool
=
True
,
debug
:
bool
=
False
)
->
bool
:
"""
Recursive validation of all the
node
s. See (`GD
Node
.validate`)
Recursive validation of all the
graph
s. See (`GD
Graph
.validate`)
:param force: If `False` uses the last cached validation result
"""
statuslist
=
[
self
.
validate
(
force
,
strict
,
debug
)]
statuslist
.
extend
(
node
.
validate
(
force
,
strict
,
debug
)
for
node
in
self
.
inputs
graph
.
validate
(
force
,
strict
,
debug
)
for
graph
in
self
.
children
)
return
all
(
statuslist
)
def
shape
(
self
)
->
tuple
:
"""
Returns the shape of the
node
Returns the shape of the
graph
"""
leninp
=
len
(
self
.
inputs
)
leninp
=
len
(
self
.
children
)
self
.
meta
.
shape
=
(
(
0
,)
if
leninp
==
0
else
(
leninp
,
tuple
(
node
.
shape
()
for
node
in
self
.
inputs
))
else
(
leninp
,
tuple
(
graph
.
shape
()
for
graph
in
self
.
children
))
)
return
self
.
meta
.
shape
def
objectscount
(
self
)
->
int
:
"""
Returns the number of the inner
node
s, including itself
Returns the number of the inner
graph
s, including itself
"""
return
1
+
sum
(
node
.
objectscount
()
for
node
in
self
.
inputs
)
return
1
+
sum
(
graph
.
objectscount
()
for
graph
in
self
.
children
)
def
depth
(
self
)
->
int
:
if
len
(
self
.
inputs
)
!=
0
:
return
1
+
max
(
tuple
(
node
.
depth
()
for
node
in
self
.
inputs
))
if
len
(
self
.
children
)
!=
0
:
return
1
+
max
(
tuple
(
graph
.
depth
()
for
graph
in
self
.
children
))
else
:
return
1
...
...
@@ -197,74 +197,74 @@ class GDNode:
def
todatalist
(
self
)
->
list
:
"""
Returns all the `GData` objects: the parent `data` and
all the `data` from the `
inputs
`
all the `data` from the `
children
`
"""
return
[
self
.
data
,
*
[
node
.
data
for
node
in
self
.
inputs
]]
return
[
self
.
data
,
*
[
graph
.
data
for
graph
in
self
.
children
]]
class
GDGraph
:
class
GDGraph
Helper
:
"""
The simple directed graph, containing the list of
node
s and
the dict with the same
node
s as values and the names as keys.
It is possible to modify
node
s by the list or by the dict,
but it is important to update the dict after adding new
node
to the graph:
using the argument `bind` of the `add_
node
` and `add_
node
s` methods
(by default) or by the direct calling the `bind_
node
s` method.
There is no edges, but every
node has inputs
. The method `add_
node
s`,
if `bind=True`, collecting
inputs
by the iteration of the
`data[
"
arguments
"
]` of the every
node
. If a single
node
is added using
the method `add_
node
`, and it is necessary to iterate through
all the
node
s and update the
inputs
, then a direct call of the
`bind_
inputs
` method is necessary.
The simple directed graph, containing the list of
graph
s and
the dict with the same
graph
s as values and the names as keys.
It is possible to modify
graph
s 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_
graph
s` methods
(by default) or by the direct calling the `bind_
graph
s` method.
There is no edges, but every
graph has children
. The method `add_
graph
s`,
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
graph
s and update the
children
, then a direct call of the
`bind_
children
` method is necessary.
"""
def
__init__
(
self
,
node
s
:
Union
[
Sequence
[
Any
],
Any
]
=
None
,
graph
s
:
Union
[
Sequence
[
Any
],
Any
]
=
None
,
strict
:
bool
=
True
,
debug
:
bool
=
False
,
**
kwargs
,
)
->
None
:
self
.
debug
=
debug
self
.
strict
=
strict
self
.
node
sdict
=
{}
self
.
node
s
=
[]
self
.
node
scount
=
self
.
__len__
self
.
graph
sdict
=
{}
self
.
graph
s
=
[]
self
.
graph
scount
=
self
.
__len__
self
.
name
=
kwargs
.
pop
(
"
name
"
,
type
(
self
).
__name__
)
if
isinstance
(
node
s
,
(
List
,
Tuple
)):
self
.
add_
nodes
(
node
s
,
kwargs
.
pop
(
"
bind
"
,
True
))
if
isinstance
(
graph
s
,
(
List
,
Tuple
)):
self
.
add_
graphs
(
graph
s
,
kwargs
.
pop
(
"
bind
"
,
True
))
else
:
raise
ValueError
(
"
The
node
s must be `List` or `Tuple`,
"
f
"
but given
'
{
type
(
node
s
)
}
'
!
"
"
The
graph
s must be `List` or `Tuple`,
"
f
"
but given
'
{
type
(
graph
s
)
}
'
!
"
)
def
__len__
(
self
)
->
int
:
return
len
(
self
.
node
s
)
return
len
(
self
.
graph
s
)
def
__copy__
(
self
)
->
GDGraph
:
def
__copy__
(
self
)
->
GDGraph
Helper
:
obj
=
type
(
self
).
__new__
(
self
.
__class__
)
obj
.
__dict__
.
update
(
self
.
__dict__
)
return
obj
def
copy
(
self
)
->
GDGraph
:
def
copy
(
self
)
->
GDGraph
Helper
:
return
self
.
__copy__
()
def
__repr__
(
self
)
->
str
:
return
f
"
GDGraph(name=
{
self
.
name
}
,
node
s=
{
self
.
node
s
}
)
"
return
f
"
GDGraph
Helper
(name=
{
self
.
name
}
,
graph
s=
{
self
.
graph
s
}
)
"
def
__str__
(
self
)
->
str
:
return
self
.
__repr__
()
def
__iter__
(
self
)
->
Iterator
[
GD
Node
]:
return
self
.
node
s
.
__iter__
()
def
__iter__
(
self
)
->
Iterator
[
GD
Graph
]:
return
self
.
graph
s
.
__iter__
()
def
__eq__
(
self
,
other
:
Any
)
->
bool
:
try
:
return
(
self
.
node
s
==
other
.
node
s
and
self
.
node
sdict
==
other
.
node
sdict
self
.
graph
s
==
other
.
graph
s
and
self
.
graph
sdict
==
other
.
graph
sdict
)
except
AttributeError
:
return
False
...
...
@@ -274,151 +274,151 @@ class GDGraph:
Returns the dict representation of the graph
:param include_data: If `True`, includes `data` field,
else uses only `name` and `
inputs
` fields of a
node
else uses only `name` and `
children
` fields of a
graph
"""
return
{
"
name
"
:
self
.
name
,
"
nodes
"
:
[
node
.
asdict
(
include_data
)
for
node
in
self
.
node
s
],
"
graphs
"
:
[
graph
.
asdict
(
include_data
)
for
graph
in
self
.
graph
s
],
}
def
print
(
self
,
include_data
:
bool
=
False
)
->
None
:
"""
Pretty print of the graph.
The dict representation (see `GDGraph.asdict`) is used
The dict representation (see `GDGraph
Helper
.asdict`) is used
:param include_data: If `True`, includes `data` field,
else uses only `name` and `
inputs
` fields of a
node
else uses only `name` and `
children
` fields of a
graph
"""
pprint
(
self
.
asdict
(
include_data
))
def
add_
node
s
(
self
,
node
s
:
Sequence
[
Any
],
bind
:
bool
=
True
)
->
None
:
def
add_
graph
s
(
self
,
graph
s
:
Sequence
[
Any
],
bind
:
bool
=
True
)
->
None
:
"""
Adding the list of the
node
s
Adding the list of the
graph
s
:param
node
s: The
node
s sequence
:param bind: If `True`, filling also the dict of the
node
s
`GDGraph
.node
sdict` and binding
inputs
:param
graph
s: The
graph
s sequence
:param bind: If `True`, filling also the dict of the
graph
s
`GDGraph
Helper.graph
sdict` and binding
children
"""
self
.
_add_
nodes
(
node
s
)
self
.
_add_
graphs
(
graph
s
)
if
bind
:
self
.
bind_
node
s
()
self
.
bind_
inputs
()
def
_add_
node
s
(
self
,
node
s
:
Sequence
[
Any
])
->
None
:
for
node
in
node
s
:
if
isinstance
(
node
,
GData
):
self
.
node
s
.
append
(
GD
Node
.
from_gdata
(
node
))
elif
isinstance
(
node
,
GDNode
):
self
.
node
s
.
append
(
node
)
elif
isinstance
(
node
,
(
List
,
Tuple
)):
self
.
_add_
nodes
(
node
)
self
.
bind_
graph
s
()
self
.
bind_
children
()
def
_add_
graph
s
(
self
,
graph
s
:
Sequence
[
Any
])
->
None
:
for
graph
in
graph
s
:
if
isinstance
(
graph
,
GData
):
self
.
graph
s
.
append
(
GD
Graph
.
from_gdata
(
graph
))
elif
isinstance
(
graph
,
GDGraph
):
self
.
graph
s
.
append
(
graph
)
elif
isinstance
(
graph
,
(
List
,
Tuple
)):
self
.
_add_
graphs
(
graph
)
else
:
if
self
.
strict
:
raise
ValueError
(
"
In the strict mode the allowed types for
node
s are
"
f
"
`GData` and `GD
Node
`, but given
'
{
type
(
node
)
}
'
!
"
"
In the strict mode the allowed types for
graph
s are
"
f
"
`GData` and `GD
Graph
`, but given
'
{
type
(
graph
)
}
'
!
"
)
elif
self
.
debug
:
warn
(
"
The name of the
node
cannot be determined,
"
"
so the string representation of the
node
is used
"
"
The name of the
graph
cannot be determined,
"
"
so the string representation of the
graph
is used
"
"
as the name.
"
,
RuntimeWarning
,
)
self
.
add_
node
(
node
)
self
.
add_
graph
(
graph
)
def
add_
node
(
def
add_
graph
(
self
,
data
:
Any
,
name
:
Optional
[
str
]
=
None
,
inputs
:
Optional
[
Sequence
[
GD
Node
]]
=
None
,
children
:
Optional
[
Sequence
[
GD
Graph
]]
=
None
,
bind
:
bool
=
True
,
)
->
GD
Node
:
)
->
GD
Graph
:
"""
Adding the
node
to the graph
Adding the
graph
to the graph
:param data: The data of the
node
:param name: The name of the
node
:param
inputs
: The list of the
inputs
:param bind: If `True`, adding the
node
to the `GDGraph
.node
sdict`
:param data: The data of the
graph
:param name: The name of the
graph
:param
children
: The list of the
children
:param bind: If `True`, adding the
graph
to the `GDGraph
Helper.graph
sdict`
"""
if
isinstance
(
data
,
GData
):
node
=
GDNode
.
from_gdata
(
data
,
inputs
)
elif
isinstance
(
data
,
GD
Node
):
node
=
data
graph
=
GDGraph
.
from_gdata
(
data
,
children
)
elif
isinstance
(
data
,
GD
Graph
):
graph
=
data
else
:
if
self
.
strict
:
raise
ValueError
(
"
In the strict mode the allowed types for
node
s are
"
f
"
`GData` and `GD
Node
`, but given
'
{
type
(
data
)
}
'
!
"
"
In the strict mode the allowed types for
graph
s are
"
f
"
`GData` and `GD
Graph
`, but given
'
{
type
(
data
)
}
'
!
"
)
elif
not
inputs
:
inputs
=
[]
elif
not
isinstance
(
inputs
,
List
):
elif
not
children
:
children
=
[]
elif
not
isinstance
(
children
,
List
):
try
:
inputs
=
list
(
inputs
)
children
=
list
(
children
)
except
Exception
as
exc
:
raise
TypeError
(
"
The *
inputs
* must be `list`, but given
"
f
"'
{
type
(
inputs
)
}
!
'"
"
The *
children
* must be `list`, but given
"
f
"'
{
type
(
children
)
}
!
'"
)
from
exc
node
=
GDNode
(
name
or
str
(
data
),
data
,
inputs
)
self
.
node
s
.
append
(
node
)
graph
=
GDGraph
(
name
or
str
(
data
),
data
,
children
)
self
.
graph
s
.
append
(
graph
)
if
bind
:
self
.
node
sdict
[
node
.
name
]
=
node
return
node
self
.
graph
sdict
[
graph
.
name
]
=
graph
return
graph
def
bind_
node
s
(
self
)
->
dict
:
def
bind_
graph
s
(
self
)
->
dict
:
"""
Filling the dict of the
nodes `node
sdict`
Filling the dict of the
graphs `graph
sdict`
"""
self
.
node
sdict
=
{
node
.
name
:
node
for
node
in
self
.
node
s
}
return
self
.
node
sdict
self
.
graph
sdict
=
{
graph
.
name
:
graph
for
graph
in
self
.
graph
s
}
return
self
.
graph
sdict
def
bind_
inputs
(
self
)
->
None
:
def
bind_
children
(
self
)
->
None
:
"""
Collecting and adding
inputs
to all the
node
s from
the `
node
.data[
"
arguments
"
]`
Collecting and adding
children
to all the
graph
s from
the `
graph
.data[
"
arguments
"
]`
"""
for
node
in
self
.
node
s
:
for
arg
in
node
.
data
[
"
arguments
"
]:
if
arg
in
self
.
node
sdict
:
node
.
inputs
.
append
(
self
.
node
sdict
[
arg
])
for
graph
in
self
.
graph
s
:
for
arg
in
graph
.
data
[
"
arguments
"
]:
if
arg
in
self
.
graph
sdict
:
graph
.
children
.
append
(
self
.
graph
sdict
[
arg
])
def
validate
(
self
,
force
:
bool
=
False
)
->
bool
:
"""
Validates all the
node
s by running `GD
Node
.validate`
Validates all the
graph
s by running `GD
Graph
.validate`
:param force: If `False` uses the last cached validation result
"""
statuslist
=
tuple
(
node
.
validate
(
force
=
force
,
strict
=
self
.
strict
,
debug
=
self
.
debug
)
for
node
in
self
.
node
s
graph
.
validate
(
force
=
force
,
strict
=
self
.
strict
,
debug
=
self
.
debug
)
for
graph
in
self
.
graph
s
)
return
all
(
statuslist
)
def
shapes
(
self
)
->
tuple
:
"""
Returns the tuple of the shapes of the `GDGraph
.node
s`
(see `GD
Node
.shape`)
Returns the tuple of the shapes of the `GDGraph
Helper.graph
s`
(see `GD
Graph
.shape`)
"""
return
tuple
(
node
.
shape
()
for
node
in
self
.
node
s
)
return
tuple
(
graph
.
shape
()
for
graph
in
self
.
graph
s
)
def
main_
node
(
self
)
->
Optional
[
GD
Node
]:
def
main_
graph
(
self
)
->
Optional
[
GD
Graph
]:
"""
Returns the deepest
node
(see `GD
Node
.depth`)
Returns the deepest
graph
(see `GD
Graph
.depth`)
"""
ind
=
-
1
maxdepth
=
0
for
i
,
node
in
enumerate
(
self
.
node
s
):
depth
=
node
.
depth
()
for
i
,
graph
in
enumerate
(
self
.
graph
s
):
depth
=
graph
.
depth
()
if
maxdepth
<
depth
:
maxdepth
=
depth
ind
=
i
return
self
.
node
s
[
ind
]
if
ind
!=
-
1
else
None
return
self
.
graph
s
[
ind
]
if
ind
!=
-
1
else
None
def
todatalist
(
self
)
->
List
[
GData
]:
"""
Returns the list of the `GData` objects
"""
return
[
node
.
data
for
node
in
self
.
node
s
]
return
[
graph
.
data
for
graph
in
self
.
graph
s
]
This diff is collapsed.
Click to expand it.
source/ggraphbuilder.py
+
20
−
20
View file @
f4eabec8
...
...
@@ -11,14 +11,14 @@ if __package__ is None or __package__ == "":
from
gagraph
import
GAGraph
from
gdata
import
GData
from
gdatabuilder
import
GDataBuilder
from
gdgraph
import
GDGraph
from
gdgraph
import
GDGraph
Helper
from
gtree
import
GTree
else
:
# uses current package visibility
from
.gagraph
import
GAGraph
from
.gdata
import
GData
from
.gdatabuilder
import
GDataBuilder
from
.gdgraph
import
GDGraph
from
.gdgraph
import
GDGraph
Helper
from
.gtree
import
GTree
...
...
@@ -26,19 +26,19 @@ class GGraphBuilder:
"""
The ``GGraphBuilder`` class
Designed for the building of ``GDGraph`` or ``GAGraph``
Designed for the building of ``GDGraph
Helper
`` or ``GAGraph``
from ``GTree``, ``GData`` or ``Sequence[GData]``
:param databuilder: The GData builder class. If ``None``,
use a new instance of ``GDataBuilder``. **Default:** ``None``
:param graphtype: The type of the graph: ``GDGraph`` or ``GAGraph``
:param graphtype: The type of the graph: ``GDGraph
Helper
`` or ``GAGraph``
:param debug: The debug option
"""
def
__init__
(
self
,
databuilder
:
Optional
[
GDataBuilder
]
=
None
,
graphtype
:
Literal
[
"
GDGraph
"
,
"
GAGraph
"
]
=
"
GDGraph
"
,
graphtype
:
Literal
[
"
GDGraph
Helper
"
,
"
GAGraph
"
]
=
"
GDGraph
Helper
"
,
debug
:
bool
=
False
,
)
->
None
:
if
not
databuilder
:
...
...
@@ -79,14 +79,14 @@ class GGraphBuilder:
@graphtype.setter
def
graphtype
(
self
,
value
:
str
)
->
None
:
if
value
not
in
(
"
GDGraph
"
,
"
GAGraph
"
):
if
value
not
in
(
"
GDGraph
Helper
"
,
"
GAGraph
"
):
raise
TypeError
(
"
The *graphtype* option must be
'
GDGraph
'
or
'
GAGraph
'
,
"
"
The *graphtype* option must be
'
GDGraph
Helper
'
or
'
GAGraph
'
,
"
f
"
but given:
'
{
value
}
'"
)
self
.
_graphtype
=
value
self
.
build
=
(
self
.
dbuild
if
self
.
graphtype
==
"
GDGraph
"
else
self
.
abuild
self
.
dbuild
if
self
.
graphtype
==
"
GDGraph
Helper
"
else
self
.
abuild
)
@property
...
...
@@ -109,18 +109,18 @@ class GGraphBuilder:
def
dbuild
(
self
,
_data
:
Union
[
GTree
,
GData
,
Sequence
],
orig
:
Optional
[
GDGraph
]
=
None
,
orig
:
Optional
[
GDGraph
Helper
]
=
None
,
**
kwargs
,
)
->
GDGraph
:
)
->
GDGraph
Helper
:
"""
Builds ``GDGraph`` from ``GTree`` or ``GData``
Builds ``GDGraph
Helper
`` from ``GTree`` or ``GData``
:param data: The data from which the ``GDGraph`` will be collected.
:param data: The data from which the ``GDGraph
Helper
`` will be collected.
May be ``GTree``, ``GData`` or ``Sequence``.
:param orig: If ``orig`` is ``GDGraph``,
:param orig: If ``orig`` is ``GDGraph
Helper
``,
starting from the graph and modifying it while
the building graph from ``data``.
Else create a new instance of ``GDGraph``. **Default:** ``None``
Else create a new instance of ``GDGraph
Helper
``. **Default:** ``None``
"""
data
=
(
_data
...
...
@@ -131,13 +131,13 @@ class GGraphBuilder:
data
=
list
(
data
)
if
not
orig
:
graph
=
GDGraph
(
nodes
=
data
,
debug
=
self
.
debug
,
**
kwargs
)
elif
isinstance
(
orig
,
GDGraph
):
graph
=
GDGraph
Helper
(
nodes
=
data
,
debug
=
self
.
debug
,
**
kwargs
)
elif
isinstance
(
orig
,
GDGraph
Helper
):
graph
=
orig
.
copy
()
graph
.
add_nodes
(
nodes
=
data
,
bind
=
kwargs
.
pop
(
"
bind
"
,
True
))
else
:
raise
TypeError
(
f
"
The *graph* must be
'
GDGraph
'
, but given
'
(
{
orig
}
,
"
f
"
The *graph* must be
'
GDGraph
Helper
'
, but given
'
(
{
orig
}
,
"
f
"
{
type
(
orig
)
}
)
'"
)
return
graph
...
...
@@ -228,10 +228,10 @@ class GGraphBuilder:
graph
.
add_edge
(
data2
[
2
],
data1
[
2
])
return
graph
def
togagraph
(
self
,
gdgraph
:
GDGraph
,
**
kwargs
)
->
GAGraph
:
def
togagraph
(
self
,
gdgraph
:
GDGraph
Helper
,
**
kwargs
)
->
GAGraph
:
"""
Converts `GDGraph` to `GAGraph`
Converts `GDGraph
Helper
` to `GAGraph`
"""
if
not
isinstance
(
gdgraph
,
GDGraph
):
if
not
isinstance
(
gdgraph
,
GDGraph
Helper
):
raise
TypeError
(
f
"
Cannot convert
'
{
type
(
gdgraph
)
}
to `GAGraph`
'
!
"
)
return
self
.
abuild
(
gdgraph
.
todatalist
(),
**
kwargs
)
This diff is collapsed.
Click to expand it.
source/gparser.py
+
9
−
9
View file @
f4eabec8
...
...
@@ -26,7 +26,7 @@ if __package__ is None or __package__ == "":
from
gagraph
import
GAGraph
from
gdata
import
GData
from
gdatabuilder
import
GDataBuilder
from
gdgraph
import
GDGraph
from
gdgraph
import
GDGraph
Helper
from
ggraphbuilder
import
GGraphBuilder
from
gpatternmatcher
import
GPatternMatcher
from
gtree
import
GToken
,
GTree
...
...
@@ -35,7 +35,7 @@ else:
from
.gagraph
import
GAGraph
from
.gdata
import
GData
from
.gdatabuilder
import
GDataBuilder
from
.gdgraph
import
GDGraph
from
.gdgraph
import
GDGraph
Helper
from
.ggraphbuilder
import
GGraphBuilder
from
.gpatternmatcher
import
GPatternMatcher
from
.gtree
import
GToken
,
GTree
...
...
@@ -69,7 +69,7 @@ class GParser(Lark):
lib
:
Optional
[
str
]
=
None
,
patternmatcher
:
Optional
[
GPatternMatcher
]
=
None
,
graphbuilder
:
Optional
[
GGraphBuilder
]
=
None
,
graphtype
:
Literal
[
"
GDGraph
"
,
"
GAGraph
"
]
=
"
GDGraph
"
,
graphtype
:
Literal
[
"
GDGraph
Helper
"
,
"
GAGraph
"
]
=
"
GDGraph
Helper
"
,
databuilder
:
Optional
[
GDataBuilder
]
=
None
,
grammar
:
Optional
[
Union
[
Grammar
,
str
,
IO
[
str
]]]
=
None
,
parser
:
Literal
[
"
earley
"
,
"
lalr
"
,
"
cyk
"
,
"
auto
"
]
=
"
lalr
"
,
...
...
@@ -284,10 +284,10 @@ class GParser(Lark):
def
pattern_matching
(
self
,
data
:
Union
[
Sequence
,
GDGraph
],
data
:
Union
[
Sequence
,
GDGraph
Helper
],
lib
:
Optional
[
str
]
=
None
,
**
kwargs
,
)
->
Union
[
Sequence
,
GDGraph
]:
)
->
Union
[
Sequence
,
GDGraph
Helper
]:
"""
Makes ``Pattern Matching`` for the ``data`` and returns the result
...
...
@@ -303,7 +303,7 @@ class GParser(Lark):
try
:
if
isinstance
(
data
,
Sequence
):
return
[
self
.
patternmatcher
.
match
(
elem
)
for
elem
in
data
]
if
not
isinstance
(
data
,
GDGraph
):
if
not
isinstance
(
data
,
GDGraph
Helper
):
data
=
self
.
build_graph
(
data
,
pattern_matching
=
False
,
**
kwargs
)
if
lib
:
self
.
patternmatcher
.
lib
=
lib
...
...
@@ -326,9 +326,9 @@ class GParser(Lark):
data
:
Union
[
Sequence
,
GTree
],
pattern_matching
:
bool
=
False
,
**
kwargs
,
)
->
Union
[
List
,
GAGraph
,
GDGraph
]:
)
->
Union
[
List
,
GAGraph
,
GDGraph
Helper
]:
"""
Builds ``GAGraph`` or ``GDGraph`` from the ``data``
Builds ``GAGraph`` or ``GDGraph
Helper
`` from the ``data``
and returns the result
:param data: The data to the graph building.
...
...
@@ -339,7 +339,7 @@ class GParser(Lark):
(is setted and ``lib`` arg is ``None``).
**Default:** ``False``
:return: The builded ``GAGraph`` or ``GDGraph``
:return: The builded ``GAGraph`` or ``GDGraph
Helper
``
"""
if
isinstance
(
data
,
Sequence
):
return
[
...
...
This diff is collapsed.
Click to expand it.
source/gpatternmatcher.py
+
6
−
9
View file @
f4eabec8
...
...
@@ -5,7 +5,6 @@ from typing import (
Callable
,
List
,
Optional
,
Sequence
,
Tuple
,
)
from
yaml
import
safe_load
,
YAMLError
...
...
@@ -14,13 +13,11 @@ from yaml import safe_load, YAMLError
if
__package__
is
None
or
__package__
==
""
:
# uses current directory visibility
# fixed import error when building documentation
from
gtree
import
GToken
,
GTree
from
gdgraph
import
GDGraph
from
gdgraph
import
GDGraphHelper
from
gdata
import
GData
else
:
# uses current package visibility
from
.gtree
import
GToken
,
GTree
from
.gdgraph
import
GDGraph
from
.gdgraph
import
GDGraphHelper
from
.gdata
import
GData
...
...
@@ -174,10 +171,10 @@ class GPatternMatcher:
f
"
key:
{
key
}
!
"
)
data
[
key
]
=
pattern
[
key
]
self
.
pgraphs
.
append
(
GDGraph
(
node
s
=
datalist
,
strict
=
True
))
self
.
pgraphs
.
append
(
GDGraph
Helper
(
graph
s
=
datalist
,
strict
=
True
))
return
self
.
pgraphs
def
match
(
self
,
graph
:
GDGraph
)
->
GDGraph
:
def
match
(
self
,
graph
:
GDGraph
Helper
)
->
GDGraph
Helper
:
"""
Find patterns in the `lib` and match corresponding object in
the `tree`
...
...
@@ -197,9 +194,9 @@ class GPatternMatcher:
"
arguments
"
,
"
type
"
,
)
for
node
in
graph
.
node
s
:
for
node
in
graph
.
graph
s
:
for
pgraph
in
self
.
pgraphs
:
mainnode
=
pgraph
.
main_
node
()
mainnode
=
pgraph
.
main_
graph
()
if
not
mainnode
.
isliketo
(
node
):
continue
for
key
in
mainnode
.
data
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment