Skip to content

Commit 99af8a9

Browse files
committed
Formatting with black and flake8
1 parent 07434fa commit 99af8a9

File tree

5 files changed

+94
-46
lines changed

5 files changed

+94
-46
lines changed

emacs-the-best-python-editor/PyEval/debug-example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
'''
1+
"""
22
Example code for debugging PyEval
3-
'''
3+
"""
44

55
from pyeval_expression import Expression
66

emacs-the-best-python-editor/PyEval/expr_test.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,39 @@ def test_double_term_expression(self):
3838
)
3939
expr = Expression("-53+2")
4040
self.assertEqual(
41-
"-53 2 + ", expr.result(), "ERROR: Negative/positive term expression"
41+
"-53 2 + ",
42+
expr.result(),
43+
"ERROR: Negative/positive term expression",
4244
)
4345
expr = Expression("53+-2")
4446
self.assertEqual(
45-
"53 -2 + ", expr.result(), "ERROR: Positive/negative term expression"
47+
"53 -2 + ",
48+
expr.result(),
49+
"ERROR: Positive/negative term expression",
4650
)
4751
expr = Expression("-53+-2")
4852
self.assertEqual(
49-
"-53 -2 + ", expr.result(), "ERROR: Double negative term expression"
53+
"-53 -2 + ",
54+
expr.result(),
55+
"ERROR: Double negative term expression",
5056
)
5157

5258
def test_double_term_operands(self):
5359
"""
5460
Tests a set of operands
5561
"""
5662
expr = Expression("53+2")
57-
self.assertEqual("53 2 + ", expr.result(), "ERROR: Additive expression")
63+
self.assertEqual(
64+
"53 2 + ", expr.result(), "ERROR: Additive expression"
65+
)
5866
expr = Expression("53-2")
59-
self.assertEqual("53 2 - ", expr.result(), "ERROR: Subtrative expression")
67+
self.assertEqual(
68+
"53 2 - ", expr.result(), "ERROR: Subtrative expression"
69+
)
6070
expr = Expression("53*2")
61-
self.assertEqual("53 2 * ", expr.result(), "ERROR: Multiplicative expression")
71+
self.assertEqual(
72+
"53 2 * ", expr.result(), "ERROR: Multiplicative expression"
73+
)
6274
expr = Expression("53/2")
6375
self.assertEqual("53 2 / ", expr.result(), "ERROR: Divide expression")
6476

@@ -67,7 +79,9 @@ def test_triple_term_expression(self):
6779
Tests a set of triple term expressions
6880
"""
6981
expr = Expression("53+2+37")
70-
self.assertEqual("53 2 37 + + ", expr.result(), "ERROR: Add/Add expression")
82+
self.assertEqual(
83+
"53 2 37 + + ", expr.result(), "ERROR: Add/Add expression"
84+
)
7185
expr = Expression("53+2*37")
7286
self.assertEqual(
7387
"53 2 37 * + ", expr.result(), "ERROR: Add/Multiply expression"
@@ -78,7 +92,9 @@ def test_triple_term_expression(self):
7892
)
7993
expr = Expression("53*2*37")
8094
self.assertEqual(
81-
"53 2 37 * * ", expr.result(), "ERROR: Multiply/Multiply expression"
95+
"53 2 37 * * ",
96+
expr.result(),
97+
"ERROR: Multiply/Multiply expression",
8298
)
8399

84100
def test_whitespace_expression(self):
@@ -91,11 +107,15 @@ def test_whitespace_expression(self):
91107
)
92108
expr = Expression("53 + 2 + 37")
93109
self.assertEqual(
94-
"53 2 37 + + ", expr.result(), "ERROR: Infixed whitespace expression"
110+
"53 2 37 + + ",
111+
expr.result(),
112+
"ERROR: Infixed whitespace expression",
95113
)
96114
expr = Expression(" 53+2+37 ")
97115
self.assertEqual(
98-
"53 2 37 + + ", expr.result(), "ERROR: Pre/post-fixed whitespace expression"
116+
"53 2 37 + + ",
117+
expr.result(),
118+
"ERROR: Pre/post-fixed whitespace expression",
99119
)
100120
expr = Expression(" 53 + 2 + 37 ")
101121
self.assertEqual(
@@ -105,10 +125,13 @@ def test_whitespace_expression(self):
105125
)
106126
expr = Expression(" 53 + 2 + 37 ")
107127
self.assertEqual(
108-
"53 2 37 + + ", expr.result(), "ERROR: Multiple whitespace expression"
128+
"53 2 37 + + ",
129+
expr.result(),
130+
"ERROR: Multiple whitespace expression",
109131
)
110132

111-
# This test should throw an exception - spaces in between operands should give an error
133+
# This test should throw an exception - spaces in between operands
134+
# should give an error
112135
with self.assertRaises(SyntaxError):
113136
expr = Expression(" 53 + - 2 + 37 ")
114137
expr.parse()

emacs-the-best-python-editor/PyEval/pyeval_expression.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
from pyeval_operator import Operator
1010

11-
class Expression():
11+
12+
class Expression:
1213
"""
1314
Defines and parses an infix expression string, returning
1415
an RPN expression string, or raising an exception if the input string
@@ -20,7 +21,7 @@ class Expression():
2021
# they are appended to the stack. As the input string is processed, the
2122
# grows as needed. In the end, it should be empty.
2223

23-
__operator_stack = [] # Holds the current stack of operators
24+
__operator_stack = [] # Holds the current stack of operators
2425

2526
# Store the string, and where we are in our parsing run.
2627
__expr_string = ""
@@ -54,7 +55,7 @@ def __init__(self, expression_string):
5455
def result(self):
5556
"""
5657
Returns the result of the evaluation.
57-
If the expression is not yet evaluated, we attempt to parse the expression.
58+
If the expression is not yet evaluated, we try to parse the expression.
5859
If this is unsuccessful, we raise a ValueError exception.
5960
Else we return the output string.
6061
"""
@@ -75,20 +76,23 @@ def parse(self):
7576
# Right now, every expression starts with an operand
7677
# This is not universally true for functions and parentheses, but we're
7778
# not supporting them yet
78-
## TODO: Add support for functions and parentheses
79+
# TODO: Add support for functions and parentheses
7980
expecting_operand = True
8081

8182
# Get the current character to inspect
8283
current_char = self.__expr_string[self.__current_position]
8384

8485
# Loop until we're past the end of the string
85-
while self.__current_position < len(self.__expr_string) and current_char != "$":
86+
while (
87+
self.__current_position < len(self.__expr_string)
88+
and current_char != "$"
89+
):
8690

8791
# Skip any leading whitespace characters
8892
while current_char.isspace():
8993
self.__current_position += 1
9094
current_char = self.__expr_string[self.__current_position]
91-
95+
9296
# Store whatever is next in the current_token string
9397
current_token = ""
9498

@@ -101,17 +105,29 @@ def parse(self):
101105
current_char = self.__expr_string[self.__current_position]
102106

103107
# Now we loop for as long as we have numbers
104-
while current_char in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]:
108+
while current_char in [
109+
"0",
110+
"1",
111+
"2",
112+
"3",
113+
"4",
114+
"5",
115+
"6",
116+
"7",
117+
"8",
118+
"9",
119+
]:
105120
current_token += current_char
106121
self.__current_position += 1
107122
current_char = self.__expr_string[self.__current_position]
108-
109-
# We should have a number now - add it to the output string, space delimited
123+
124+
# We should have a number now - add it to the output string,
125+
# space delimited
110126
self.__output_string += current_token + " "
111127

112128
# And after every operand, we need to look for an operator
113129
expecting_operand = False
114-
130+
115131
else:
116132
# Here, we just need a single operator, so
117133
# Get that operator, validate it, then
@@ -132,12 +148,20 @@ def parse(self):
132148
self.__operator_stack.append(current_operator)
133149

134150
else:
135-
top_operator = self.__operator_stack[len(self.__operator_stack)-1]
136-
while len(self.__operator_stack)>0 and top_operator.precedence > current_operator.precedence:
151+
top_operator = self.__operator_stack[
152+
len(self.__operator_stack) - 1
153+
]
154+
while (
155+
len(self.__operator_stack) > 0
156+
and top_operator.precedence
157+
> current_operator.precedence
158+
):
137159
self.__output_string += top_operator.op_string + " "
138160
self.__operator_stack.pop()
139-
if len(self.__operator_stack)>0:
140-
top_operator = self.__operator_stack[len(self.__operator_stack)-1]
161+
if len(self.__operator_stack) > 0:
162+
top_operator = self.__operator_stack[
163+
len(self.__operator_stack) - 1
164+
]
141165

142166
self.__operator_stack.append(current_operator)
143167

@@ -162,4 +186,3 @@ def parse(self):
162186

163187
self.__evaluated = True
164188
return self.__output_string
165-

emacs-the-best-python-editor/PyEval/pyeval_operand.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
AUTHOR: Jon Fincher
55
"""
66

7+
78
class Operand:
89
"""
910
Common operator class used by the evaluator.
1011
"""
1112

12-
op_string = "" # String to hold the operand literal
13-
op_value = 0 # Integer value of the operand
13+
op_string = "" # String to hold the operand literal
14+
op_value = 0 # Integer value of the operand
1415

1516
def __init__(self, operand_string):
1617
""" Create a new operator object. """

emacs-the-best-python-editor/PyEval/pyeval_operator.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,33 @@
77
# Dictionary lookup for precedence
88
# Currently four levels of precedence
99
PRECEDENCE = {
10-
'+' : 2, # Addition
11-
'-' : 2, # Subtraction
12-
'*' : 3, # Multiplication
13-
'/' : 3, # Division
14-
'%' : 3, # Modulo
15-
'^' : 4 # Power
10+
"+": 2, # Addition
11+
"-": 2, # Subtraction
12+
"*": 3, # Multiplication
13+
"/": 3, # Division
14+
"%": 3, # Modulo
15+
"^": 4, # Power
1616
}
1717

1818
# Dictionary lookup for number of operands to use
1919
OPERAND_COUNT = {
20-
'+' : 2, # Addition
21-
'-' : 2, # Subtraction
22-
'*' : 2, # Multiplication
23-
'/' : 2, # Division
24-
'%' : 2, # Modulo
25-
'^' : 2 # Power
20+
"+": 2, # Addition
21+
"-": 2, # Subtraction
22+
"*": 2, # Multiplication
23+
"/": 2, # Division
24+
"%": 2, # Modulo
25+
"^": 2, # Power
2626
}
2727

28+
2829
class Operator:
2930
"""
3031
Common operator class used by the evaluator.
3132
"""
3233

33-
_op_string = "" # String to hold the operator
34-
_op_prec = 0 # Integer to hold the precedence
35-
_op_cnt = 0 # Integer to hold the number of operands to use
34+
_op_string = "" # String to hold the operator
35+
_op_prec = 0 # Integer to hold the precedence
36+
_op_cnt = 0 # Integer to hold the number of operands to use
3637

3738
def __init__(self, operator_string):
3839
""" Create a new operator object. """

0 commit comments

Comments
 (0)