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
2527c7d9
Commit
2527c7d9
authored
Nov 19, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code cleanup and added interactie parser mode (for shells).
parent
93a5449d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
8 deletions
+52
-8
src/__init__.py
src/__init__.py
+0
-0
src/calc.py
src/calc.py
+19
-4
tests/rules.mk
tests/rules.mk
+2
-2
tests/test_calc.py
tests/test_calc.py
+31
-2
No files found.
src/__init__.py
0 → 100644
View file @
2527c7d9
src/calc.py
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
)
tests/rules.mk
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 $@
tests/test_calc.py
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
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