Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
trs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
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
Taddeüs Kroes
trs
Commits
5383845b
Commit
5383845b
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added Scope class for easy scope-level operations.
parent
47f55b7f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/scope.py
+34
-0
34 additions, 0 deletions
src/scope.py
tests/test_scope.py
+36
-0
36 additions, 0 deletions
tests/test_scope.py
with
70 additions
and
0 deletions
src/scope.py
0 → 100644
+
34
−
0
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
)
This diff is collapsed.
Click to expand it.
tests/test_scope.py
0 → 100644
+
36
−
0
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
)
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