Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
trs
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
trs
Commits
5383845b
Commit
5383845b
authored
Jan 23, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Scope class for easy scope-level operations.
parent
47f55b7f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
0 deletions
+70
-0
src/scope.py
src/scope.py
+34
-0
tests/test_scope.py
tests/test_scope.py
+36
-0
No files found.
src/scope.py
0 → 100644
View file @
5383845b
from
rules.utils
import
nary_node
class
Scope
(
object
):
def
__init__
(
self
,
node
):
self
.
node
=
node
self
.
nodes
=
node
.
get_scope
()
def
remove
(
self
,
node
,
replacement
=
None
):
if
node
.
is_leaf
():
node_cmp
=
hash
(
node
)
else
:
node_cmp
=
node
for
i
,
n
in
enumerate
(
self
.
nodes
):
if
n
.
is_leaf
():
n_cmp
=
hash
(
n
)
else
:
n_cmp
=
n
if
n_cmp
==
node_cmp
:
if
replacement
!=
None
:
self
.
nodes
[
i
]
=
replacement
else
:
del
self
.
nodes
[
i
]
return
raise
ValueError
(
'Node "%s" is not in the scope of "%s".'
%
(
node
,
self
.
node
))
def
as_nary_node
(
self
):
return
nary_node
(
self
.
node
.
value
,
self
.
nodes
)
tests/test_scope.py
0 → 100644
View file @
5383845b
import
unittest
from
src.scope
import
Scope
from
tests.rulestestcase
import
RulesTestCase
,
tree
class
TestScope
(
RulesTestCase
):
def
setUp
(
self
):
self
.
n
,
self
.
f
=
tree
(
'a + b + cd,f'
)
(
self
.
a
,
self
.
b
),
self
.
cd
=
self
.
n
self
.
c
,
self
.
d
=
self
.
cd
self
.
scope
=
Scope
(
self
.
n
)
def
test___init__
(
self
):
self
.
assertEqual
(
self
.
scope
.
node
,
self
.
n
)
self
.
assertEqual
(
self
.
scope
.
nodes
,
[
self
.
a
,
self
.
b
,
self
.
cd
])
def
test_remove_leaf
(
self
):
self
.
scope
.
remove
(
self
.
b
)
self
.
assertEqual
(
self
.
scope
.
nodes
,
[
self
.
a
,
self
.
cd
])
def
test_remove_node
(
self
):
self
.
scope
.
remove
(
self
.
cd
)
self
.
assertEqual
(
self
.
scope
.
nodes
,
[
self
.
a
,
self
.
b
])
def
test_remove_replace
(
self
):
self
.
scope
.
remove
(
self
.
cd
,
self
.
f
)
self
.
assertEqual
(
self
.
scope
.
nodes
,
[
self
.
a
,
self
.
b
,
self
.
f
])
def
test_remove_error
(
self
):
with
self
.
assertRaises
(
ValueError
):
self
.
scope
.
remove
(
self
.
f
)
def
test_as_nary_node
(
self
):
self
.
assertEqualNodes
(
self
.
scope
.
as_nary_node
(),
self
.
n
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment