Skip to content

Commit 612b862

Browse files
committed
Renamed expression classes and prepared docs for them
1 parent f65b1a0 commit 612b862

9 files changed

Lines changed: 156 additions & 36 deletions

File tree

dice_notation/parser/dice.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from operator import add, sub
55

66
from dice_notation.parser.common import PlyParser
7-
from dice_notation.parser.notation import BinaryOperationExpression, ConstantExpression, \
8-
DiceExpression
7+
from dice_notation.parser.notation import BinaryOperation, ConstantOperand, \
8+
DiceOperand
99

1010
"""
1111
Dice notation parsers.
@@ -60,7 +60,7 @@ def p_statement_expr(self, p):
6060

6161
def p_expression_dice(self, p):
6262
'expression : DIGIT DSEPARATOR DIGIT'
63-
p[0] = DiceExpression(p[1], p[3])
63+
p[0] = DiceOperand(p[1], p[3])
6464
self._logger.debug("Dice %s", p[0])
6565

6666
def p_expression_binop(self, p):
@@ -70,17 +70,17 @@ def p_expression_binop(self, p):
7070
"""
7171
# print [repr(p[i]) for i in range(0,4)]
7272
if p[2] == '+':
73-
p[0] = BinaryOperationExpression(add, p[1], p[3])
73+
p[0] = BinaryOperation(add, p[1], p[3])
7474
elif p[2] == '-':
75-
p[0] = BinaryOperationExpression(sub, p[1], p[3])
75+
p[0] = BinaryOperation(sub, p[1], p[3])
7676
self._logger.debug("Binary operation %s", p[0])
7777

7878
def p_expression_digit(self, p):
7979
'expression : DIGIT'
80-
if isinstance(p[1], ConstantExpression):
80+
if isinstance(p[1], ConstantOperand):
8181
p[0] = p[1]
8282
else:
83-
p[0] = ConstantExpression(p[1])
83+
p[0] = ConstantOperand(p[1])
8484
self._logger.debug("Constant %s", p[0])
8585

8686
def p_error(self, p):

dice_notation/parser/notation.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""
1616

1717

18-
class ConstantExpression(Rollable):
18+
class ConstantOperand(Rollable):
1919
"""
2020
Expression for a constant value.
2121
@@ -26,30 +26,30 @@ class ConstantExpression(Rollable):
2626
"""
2727

2828
def __init__(self, constant):
29-
super(ConstantExpression, self).__init__()
29+
super(ConstantOperand, self).__init__()
3030
self._constant = constant
3131
# If it received a constant node this is unwrapped
3232
# TODO: Maybe this problem should be fixed somewhere else
33-
while isinstance(self._constant, ConstantExpression):
33+
while isinstance(self._constant, ConstantOperand):
3434
self._constant = constant.constant
3535

3636
def __add__(self, other):
37-
return ConstantExpression(other + self.constant)
37+
return ConstantOperand(other + self.constant)
3838

3939
def __sub__(self, other):
40-
return ConstantExpression(self.constant - other)
40+
return ConstantOperand(self.constant - other)
4141

4242
def __radd__(self, other):
43-
return ConstantExpression(self.constant + other)
43+
return ConstantOperand(self.constant + other)
4444

4545
def __rsub__(self, other):
46-
return ConstantExpression(other - self.constant)
46+
return ConstantOperand(other - self.constant)
4747

4848
def __lt__(self, other):
4949
return self.constant < other
5050

5151
def __le__(self, other):
52-
if isinstance(other, ConstantExpression):
52+
if isinstance(other, ConstantOperand):
5353
return self.constant <= other.constant
5454
elif isinstance(other, (int, float)):
5555
return self.constant <= other
@@ -66,7 +66,7 @@ def __gt__(self, other):
6666
return self.constant > other
6767

6868
def __ge__(self, other):
69-
if isinstance(other, ConstantExpression):
69+
if isinstance(other, ConstantOperand):
7070
return self.constant >= other.constant
7171
elif isinstance(other, (int, float)):
7272
return self.constant >= other
@@ -102,27 +102,27 @@ def roll(self):
102102
return self.constant
103103

104104

105-
class DiceExpression(RollableDice):
105+
class DiceOperand(RollableDice):
106106
"""
107107
Expression for a dice.
108108
109109
It is mostly just its parent class, only adding comparison methods.
110110
"""
111111

112112
def __init__(self, quantity, sides):
113-
super(DiceExpression, self).__init__(quantity=quantity, sides=sides)
113+
super(DiceOperand, self).__init__(quantity=quantity, sides=sides)
114114

115115
def __add__(self, other):
116-
return ConstantExpression(other + self.roll())
116+
return ConstantOperand(other + self.roll())
117117

118118
def __sub__(self, other):
119-
return ConstantExpression(self.roll() - other)
119+
return ConstantOperand(self.roll() - other)
120120

121121
def __radd__(self, other):
122-
return ConstantExpression(self.roll() + other)
122+
return ConstantOperand(self.roll() + other)
123123

124124
def __rsub__(self, other):
125-
return ConstantExpression(other - self.roll())
125+
return ConstantOperand(other - self.roll())
126126

127127
def __str__(self):
128128
return '%sd%s' % (self.quantity, self.sides)
@@ -132,15 +132,15 @@ def __repr__(self):
132132
(self.__class__.__name__, self.quantity, self.sides)
133133

134134

135-
class BinaryOperationExpression(Rollable):
135+
class BinaryOperation(Rollable):
136136
"""
137137
Exprssion for a binary operation.
138138
139139
Acquiring its value will execute a function with two parameters.
140140
"""
141141

142142
def __init__(self, function, left, right):
143-
super(BinaryOperationExpression, self).__init__()
143+
super(BinaryOperation, self).__init__()
144144
self._logger = logging.getLogger(self.__class__.__name__)
145145
self._function = function
146146
self._left = left
@@ -149,28 +149,28 @@ def __init__(self, function, left, right):
149149
def __add__(self, other):
150150
value = self.operate()
151151
self._logger.debug("%s + %s", value, other)
152-
return ConstantExpression(other + value)
152+
return ConstantOperand(other + value)
153153

154154
def __sub__(self, other):
155155
value = self.operate()
156156
self._logger.debug("%s - %s", other, value)
157-
return ConstantExpression(value - other)
157+
return ConstantOperand(value - other)
158158

159159
def __radd__(self, other):
160160
value = self.operate()
161161
self._logger.debug("(rear) %s + %s", value, other)
162-
return ConstantExpression(value + other)
162+
return ConstantOperand(value + other)
163163

164164
def __rsub__(self, other):
165165
value = self.operate()
166166
self._logger.debug("(rear) %s - %s", other, value)
167-
return ConstantExpression(other - value)
167+
return ConstantOperand(other - value)
168168

169169
def __lt__(self, other):
170170
return self.operate() < other
171171

172172
def __le__(self, other):
173-
if isinstance(other, ConstantExpression):
173+
if isinstance(other, ConstantOperand):
174174
return self.operate() <= other.constant
175175
elif isinstance(other, (int, float)):
176176
return self.operate() <= other
@@ -187,7 +187,7 @@ def __gt__(self, other):
187187
return self.operate() > other
188188

189189
def __ge__(self, other):
190-
if isinstance(other, ConstantExpression):
190+
if isinstance(other, ConstantOperand):
191191
return self.operate() >= other.constant
192192
elif isinstance(other, (int, float)):
193193
return self.operate() >= other
3.28 KB
Loading
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<diagram program="umlet" version="14.2">
3+
<zoom_level>10</zoom_level>
4+
<element>
5+
<id>UMLClass</id>
6+
<coordinates>
7+
<x>390</x>
8+
<y>200</y>
9+
<w>100</w>
10+
<h>30</h>
11+
</coordinates>
12+
<panel_attributes>+</panel_attributes>
13+
<additional_attributes/>
14+
</element>
15+
<element>
16+
<id>UMLClass</id>
17+
<coordinates>
18+
<x>330</x>
19+
<y>270</y>
20+
<w>100</w>
21+
<h>30</h>
22+
</coordinates>
23+
<panel_attributes>2d6</panel_attributes>
24+
<additional_attributes/>
25+
</element>
26+
<element>
27+
<id>UMLClass</id>
28+
<coordinates>
29+
<x>450</x>
30+
<y>270</y>
31+
<w>100</w>
32+
<h>30</h>
33+
</coordinates>
34+
<panel_attributes>+</panel_attributes>
35+
<additional_attributes/>
36+
</element>
37+
<element>
38+
<id>UMLClass</id>
39+
<coordinates>
40+
<x>390</x>
41+
<y>340</y>
42+
<w>100</w>
43+
<h>30</h>
44+
</coordinates>
45+
<panel_attributes>1d20</panel_attributes>
46+
<additional_attributes/>
47+
</element>
48+
<element>
49+
<id>UMLClass</id>
50+
<coordinates>
51+
<x>510</x>
52+
<y>340</y>
53+
<w>100</w>
54+
<h>30</h>
55+
</coordinates>
56+
<panel_attributes>5</panel_attributes>
57+
<additional_attributes/>
58+
</element>
59+
<element>
60+
<id>Relation</id>
61+
<coordinates>
62+
<x>370</x>
63+
<y>220</y>
64+
<w>90</w>
65+
<h>70</h>
66+
</coordinates>
67+
<panel_attributes>lt=-</panel_attributes>
68+
<additional_attributes>10.0;50.0;70.0;10.0</additional_attributes>
69+
</element>
70+
<element>
71+
<id>Relation</id>
72+
<coordinates>
73+
<x>430</x>
74+
<y>220</y>
75+
<w>90</w>
76+
<h>70</h>
77+
</coordinates>
78+
<panel_attributes>lt=-</panel_attributes>
79+
<additional_attributes>70.0;50.0;10.0;10.0</additional_attributes>
80+
</element>
81+
<element>
82+
<id>Relation</id>
83+
<coordinates>
84+
<x>430</x>
85+
<y>290</y>
86+
<w>90</w>
87+
<h>70</h>
88+
</coordinates>
89+
<panel_attributes>lt=-</panel_attributes>
90+
<additional_attributes>10.0;50.0;70.0;10.0</additional_attributes>
91+
</element>
92+
<element>
93+
<id>Relation</id>
94+
<coordinates>
95+
<x>490</x>
96+
<y>290</y>
97+
<w>90</w>
98+
<h>70</h>
99+
</coordinates>
100+
<panel_attributes>lt=-</panel_attributes>
101+
<additional_attributes>70.0;50.0;10.0;10.0</additional_attributes>
102+
</element>
103+
</diagram>

docs/source/code/notation.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
==============
2-
Notation model
3-
==============
1+
===============
2+
Notation module
3+
===============
44

55
.. automodule:: dice_notation.parser.notation
66
:members:

docs/source/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@
117117
'navbar_links': [('Documentation', [('Acquire', './acquire.html'),
118118
('Usage', './usage.html'),
119119
('Dice model', './docs/dice.html'),
120-
('Grammar', './docs/grammar.html')]),
120+
('Grammar', './docs/grammar.html'),
121+
('Notation', './docs/notation.html')]),
121122
('Info and Reports', [('Reports', './reports.html'),
122123
('Code docs', './code/index.html')])],
123124
}

docs/source/docs/notation.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
==============
2+
Notation model
3+
==============
4+
5+
The dice notation grammar is meant to allow working with dice notation
6+
expressions, which will be transformed into an equivalent structure by using
7+
a custom model.
8+
9+
As the model user is very simple the structure generated will be a simple
10+
tree, where nodes are connected by the use of operations.
11+
12+
For example, the expression “2d6+1d20+5” would become something like this:
13+
14+
.. image:: ../_static/diagram/dice_notation_tree_example.png
15+
:alt: Dice notation tree example

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ Features
2626
code/index
2727
docs/dice
2828
docs/grammar
29+
docs/notation

tests/parser/test_dice_parser_notation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44

55
from dice_notation.parser.dice import DiceParser
6-
from dice_notation.parser.notation import ConstantExpression
6+
from dice_notation.parser.notation import ConstantOperand
77

88
"""
99
Dice parser tests for purely numeric expressions.
@@ -30,4 +30,4 @@ def test_longSub_singleWrap(self):
3030
"""
3131
result = self.parser.parse("1-2-3-4-5").operate()
3232

33-
self.assertFalse(isinstance(result.constant, ConstantExpression))
33+
self.assertFalse(isinstance(result.constant, ConstantOperand))

0 commit comments

Comments
 (0)