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
2527c7d9
Commit
2527c7d9
authored
13 years ago
by
Sander Mathijs van Veen
Browse files
Options
Downloads
Patches
Plain Diff
Code cleanup and added interactie parser mode (for shells).
parent
93a5449d
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/__init__.py
+0
-0
0 additions, 0 deletions
src/__init__.py
src/calc.py
+19
-4
19 additions, 4 deletions
src/calc.py
tests/rules.mk
+2
-2
2 additions, 2 deletions
tests/rules.mk
tests/test_calc.py
+31
-2
31 additions, 2 deletions
tests/test_calc.py
with
52 additions
and
8 deletions
src/__init__.py
0 → 100644
+
0
−
0
View file @
2527c7d9
This diff is collapsed.
Click to expand it.
src/calc.py
+
19
−
4
View file @
2527c7d9
...
@@ -40,6 +40,12 @@ class Parser(BisonParser):
...
@@ -40,6 +40,12 @@ class Parser(BisonParser):
(
'
right
'
,
(
'
POW
'
,
)),
(
'
right
'
,
(
'
POW
'
,
)),
)
)
interactive
=
0
def
__init__
(
self
,
**
kwargs
):
BisonParser
.
__init__
(
self
,
**
kwargs
)
self
.
interactive
=
kwargs
.
get
(
'
interactive
'
,
0
)
# ------------------------------------------------------------------
# ------------------------------------------------------------------
# override default read method with a version that prompts for input
# override default read method with a version that prompts for input
# ------------------------------------------------------------------
# ------------------------------------------------------------------
...
@@ -67,7 +73,11 @@ class Parser(BisonParser):
...
@@ -67,7 +73,11 @@ class Parser(BisonParser):
input :
input :
| input line
| input line
"""
"""
return
if
option
==
1
:
if
self
.
interactive
:
print
values
[
1
]
return
values
[
1
]
def
on_line
(
self
,
target
,
option
,
names
,
values
):
def
on_line
(
self
,
target
,
option
,
names
,
values
):
"""
"""
...
@@ -75,7 +85,10 @@ class Parser(BisonParser):
...
@@ -75,7 +85,10 @@ class Parser(BisonParser):
| exp NEWLINE
| exp NEWLINE
"""
"""
if
option
==
1
:
if
option
==
1
:
print
'
on_line: exp =
'
,
values
[
0
]
if
self
.
verbose
:
print
'
on_line: exp =
'
,
values
[
0
]
return
values
[
0
]
def
on_exp
(
self
,
target
,
option
,
names
,
values
):
def
on_exp
(
self
,
target
,
option
,
names
,
values
):
"""
"""
...
@@ -88,7 +101,9 @@ class Parser(BisonParser):
...
@@ -88,7 +101,9 @@ class Parser(BisonParser):
| exp POW exp
| exp POW exp
| LPAREN exp RPAREN
| LPAREN exp RPAREN
"""
"""
print
'
on_exp: got %s %s %s %s
'
%
(
target
,
option
,
names
,
values
)
if
self
.
verbose
:
print
'
on_exp: got %s %s %s %s
'
%
(
target
,
option
,
names
,
values
)
if
option
==
0
:
if
option
==
0
:
return
float
(
values
[
0
])
return
float
(
values
[
0
])
...
@@ -146,5 +161,5 @@ class Parser(BisonParser):
...
@@ -146,5 +161,5 @@ class Parser(BisonParser):
"""
"""
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
p
=
Parser
(
verbose
=
0
,
keepfiles
=
1
)
p
=
Parser
(
verbose
=
0
,
keepfiles
=
1
,
interactive
=
1
)
p
.
run
(
debug
=
0
)
p
.
run
(
debug
=
0
)
This diff is collapsed.
Click to expand it.
tests/rules.mk
+
2
−
2
View file @
2527c7d9
...
@@ -12,7 +12,7 @@ endif
...
@@ -12,7 +12,7 @@ endif
test
:
$(TESTS) build
test
:
$(TESTS) build
coverage
:
${COVERAGE}
coverage
:
${COVERAGE}
build
mkdir
${
COVERAGE_OUTPUT_DIR
}
2>/dev/null
||
true
mkdir
${
COVERAGE_OUTPUT_DIR
}
2>/dev/null
||
true
${
COVERAGE
}
erase
${
COVERAGE
}
erase
for
t
in
${
TESTS
};
do
\
for
t
in
${
TESTS
};
do
\
...
@@ -26,4 +26,4 @@ ${COVERAGE}:
...
@@ -26,4 +26,4 @@ ${COVERAGE}:
@
echo
"Install package 'python-coverage' to generate a coverage report."
@
echo
"Install package 'python-coverage' to generate a coverage report."
@
echo
"On Debian/Ubuntu use: sudo apt-get install python-coverage"
;
false
@
echo
"On Debian/Ubuntu use: sudo apt-get install python-coverage"
;
false
$(TESTS)
:
; @python -m testrunner $@
$(TESTS)
:
build
; @python -m testrunner $@
This diff is collapsed.
Click to expand it.
tests/test_calc.py
+
31
−
2
View file @
2527c7d9
import
unittest
import
unittest
from
src.calc
import
Parser
class
TestParser
(
Parser
):
def
__init__
(
self
,
input_buffer
,
**
kwargs
):
Parser
.
__init__
(
self
,
**
kwargs
)
self
.
input_buffer
=
[]
self
.
input_position
=
0
map
(
self
.
append
,
input_buffer
)
def
append
(
self
,
input
):
self
.
input_buffer
.
append
(
input
+
'
\n
'
)
def
read
(
self
,
nbytes
):
buffer
=
''
try
:
buffer
=
self
.
input_buffer
[
self
.
input_position
]
except
IndexError
:
return
''
self
.
input_position
+=
1
return
buffer
class
TestCalc
(
unittest
.
TestCase
):
class
TestCalc
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
...
@@ -9,5 +38,5 @@ class TestCalc(unittest.TestCase):
...
@@ -9,5 +38,5 @@ class TestCalc(unittest.TestCase):
def
tearDown
(
self
):
def
tearDown
(
self
):
pass
pass
def
test_
true
(
self
):
def
test_
constructor
(
self
):
assert
T
rue
assert
T
estParser
([
'
1+4
'
],
keepfiles
=
1
).
run
()
==
5.0
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