Skip to content

Commit 2450724

Browse files
implementation 10 - corrections for pip8
1 parent 5f5faff commit 2450724

File tree

9 files changed

+73
-78
lines changed

9 files changed

+73
-78
lines changed

final_task/pycalc/UnitTests.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# Tests of 'Tokenizer' class from 'tokenizer' module
1515
class TokenizerTestCase(unittest.TestCase):
1616
"""Tests for Tokenizer class"""
17-
17+
1818
def test_extract_operators_and_pos_int_numbers(self):
1919
"""Are operators and positive int numbers extracted properly?"""
2020
user_expr = '1+2-3*4/5^6**7//8%9'
@@ -23,7 +23,7 @@ def test_extract_operators_and_pos_int_numbers(self):
2323
self.assertEqual(tokens, ['1', '+', '2', '-', '3', '*', '4', '/',
2424
'5', '^', '6', '**', '7', '//', '8', '%', '9'])
2525
self.assertEqual(error_msg, None)
26-
26+
2727
def test_extract_operators_and_neg_int_numbers(self):
2828
"""Are operators and negative int numbers extracted properly?"""
2929
user_expr = '-1+-2--3*-4/-5^-6**-7//-8%-9'
@@ -32,63 +32,63 @@ def test_extract_operators_and_neg_int_numbers(self):
3232
self.assertEqual(tokens, ['-1.0', '-', '2', '+', '3', '*', '-4', '/', '-5',
3333
'^', '-6', '**', '-7', '//', '-8', '%', '-9'])
3434
self.assertEqual(error_msg, None)
35-
35+
3636
def test_extract_pos_float_numbers(self):
3737
"""Are positive float numbers extracted properly?"""
3838
user_expr = '0.1+1.55-112.12'
3939
tokenizer = Tokenizer(user_expr)
4040
tokens, error_msg = tokenizer.extract_tokens()
4141
self.assertEqual(tokens, ['0.1', '+', '1.55', '-', '112.12'])
4242
self.assertEqual(error_msg, None)
43-
43+
4444
def test_extract_neg_float_numbers(self):
4545
"""Are negative float numbers extracted properly?"""
4646
user_expr = '-0.1+-1.55--112.12'
4747
tokenizer = Tokenizer(user_expr)
4848
tokens, error_msg = tokenizer.extract_tokens()
4949
self.assertEqual(tokens, ['-0.1', '-', '1.55', '+', '112.12'])
5050
self.assertEqual(error_msg, None)
51-
51+
5252
def test_extract_comparison_operators(self):
5353
"""Are comparison operators extracted properly?"""
5454
user_expr = '><>=<=!==='
5555
tokenizer = Tokenizer(user_expr)
5656
tokens, error_msg = tokenizer.extract_tokens()
5757
self.assertEqual(tokens, ['>', '<', '>=', '<=', '!=', '=='])
5858
self.assertEqual(error_msg, None)
59-
59+
6060
def test_extract_pos_constants(self):
6161
"""Are positive constants extracted properly?"""
6262
user_expr = 'e+pi-tau/inf*nan'
6363
tokenizer = Tokenizer(user_expr)
6464
tokens, error_msg = tokenizer.extract_tokens()
6565
self.assertEqual(tokens, ['e', '+', 'pi', '-', 'tau', '/', 'inf', '*', 'nan'])
6666
self.assertEqual(error_msg, None)
67-
67+
6868
def test_extract_neg_constants(self):
6969
"""Are negative constants extracted properly?"""
7070
user_expr = '-e+-pi--tau/-inf*-nan'
7171
tokenizer = Tokenizer(user_expr)
7272
tokens, error_msg = tokenizer.extract_tokens()
7373
self.assertEqual(tokens, ['-e', '-', 'pi', '+', 'tau', '/', '-inf', '*', '-nan'])
7474
self.assertEqual(error_msg, None)
75-
75+
7676
def test_extract_brackets(self):
7777
"""Are brackets extracted properly?"""
7878
user_expr = '()'
7979
tokenizer = Tokenizer(user_expr)
8080
tokens, error_msg = tokenizer.extract_tokens()
8181
self.assertEqual(tokens, ['(', ')'])
8282
self.assertEqual(error_msg, None)
83-
83+
8484
def test_extract_comma(self):
8585
"""Is comma extracted?"""
8686
user_expr = 'pow(2,3)'
8787
tokenizer = Tokenizer(user_expr)
8888
tokens, error_msg = tokenizer.extract_tokens()
8989
self.assertEqual(tokens, ['pow', '(', '2', ',', '3', ')'])
9090
self.assertEqual(error_msg, None)
91-
91+
9292
def test_extract_functions(self):
9393
"""Are functions extracted properly?"""
9494
user_expr = "round(sin(2)-asin(1))-abs(exp(3))"
@@ -114,7 +114,7 @@ def test_is_number_method(self):
114114
for token in tokens:
115115
is_numbers.append(tokenizer.is_number(token))
116116
self.assertEqual(is_numbers, [True, True, True, False])
117-
117+
118118
def test_extract_tokens_error_msg(self):
119119
"""Is error_message created?"""
120120
user_expr = "2+shikaka(3)"
@@ -126,7 +126,7 @@ def test_extract_tokens_error_msg(self):
126126
# Tests of 'Multsignsadder' class from 'addmultsigns' module
127127
class MultsignsadderTestCase(unittest.TestCase):
128128
"""Tests for Multsignsadder class"""
129-
129+
130130
def test_is_number_method(self):
131131
"""Does 'is_number' method distinguish tokens which are numbers from ones which are not?"""
132132
tokens = ['2.3', '-0.6', '5', 'sin', 'exp']
@@ -135,15 +135,15 @@ def test_is_number_method(self):
135135
for token in tokens:
136136
is_numbers.append(mult_signs_adder.is_number(token))
137137
self.assertEqual(is_numbers, [True, True, True, False, False])
138-
138+
139139
def test_addmultsigns_add_mult_signs(self):
140140
"""Are multiplication signs added to where they implicit were to be in expression?"""
141141
tokens = ['5', 'tau', '-', '4', 'sin', '(', '7', ')', '-', '9', '(', '1', '+', '10', ')']
142142
mult_signs_adder = Multsignsadder(tokens)
143143
extd_tokens = mult_signs_adder.addmultsigns()
144144
self.assertEqual(extd_tokens, ['5', '*', 'tau', '-', '4', '*', 'sin', '(', '7', ')', '-',
145145
'9', '*', '(', '1', '+', '10', ')'])
146-
146+
147147
def test_addmultsigns_dont_add_mult_signs(self):
148148
"""Aren't multiplication signs added if it's not needed?"""
149149
tokens = ['2', '+', '3', '*', '5']
@@ -169,7 +169,7 @@ def test_consider_log_args_method(self):
169169
# Tests of 'RPN class' from 'rpn' module
170170
class RPNTestCase(unittest.TestCase):
171171
"""Tests for RPN class"""
172-
172+
173173
def test_is_left_associative_method(self):
174174
"""Are left associative operators recognized?"""
175175
tokens = ['^', '**', '+', '/']
@@ -178,7 +178,7 @@ def test_is_left_associative_method(self):
178178
for token in tokens:
179179
is_left_associative.append(rpn.is_left_associative(token))
180180
self.assertEqual(is_left_associative, [False, False, True, True])
181-
181+
182182
def test_is_number_method(self):
183183
"""Does 'is_number' method distinguish tokens which are numbers from ones which are not?"""
184184
tokens = ['1.3', '-0.5', '/', '%', '9']
@@ -187,15 +187,15 @@ def test_is_number_method(self):
187187
for token in tokens:
188188
is_numbers.append(rpn.is_number(token))
189189
self.assertEqual(is_numbers, [True, True, False, False, True])
190-
190+
191191
def test_convert2rpn_method(self):
192192
"""Does 'convert2rpn' method work correctly?"""
193193
tokens = ['-pi', '*', 'round', '(', '2.23', ')', '//', '5', '*', 'pow', '(', '2', '3', ')']
194194
rpn = RPN(tokens)
195195
result, error_msg = rpn.convert2rpn()
196196
self.assertEqual(result, ['-pi', '2.23', 'round', '*', '5', '//', '2', '3', 'pow', '*'])
197197
self.assertEqual(error_msg, None)
198-
198+
199199
def test_convert2rpn_method_error_msg(self):
200200
"""Is error_message created?"""
201201
tokens = ['(', '2', '+', '3', ')', ')']
@@ -207,7 +207,7 @@ def test_convert2rpn_method_error_msg(self):
207207
# Tests of 'Constsreplacer' class from 'constsreplacer' module
208208
class ConstsreplacerTestCase(unittest.TestCase):
209209
"""Tests for Constsreplacer class"""
210-
210+
211211
def test_replace_constants_method(self):
212212
"""Are constants replaced and not constants aren't replaced?"""
213213
tokens = ['e', '-e', 'pi', '-pi', 'tau', '-tau', '2', 'cos', 'inf', '-nan', '+']
@@ -222,22 +222,22 @@ def test_replace_constants_method(self):
222222
# Tests of 'RPNcalculator' class from 'rpncalculator' module
223223
class RPNcalculatorTestCase(unittest.TestCase):
224224
"""Tests for RPNcalculator class"""
225-
225+
226226
def test_evaluate_method_result(self):
227227
"""Does 'evaluate' method actually evaluate RPN math expression and give out correct result?"""
228228
rpn_tokens = ['2', 'sqrt', '3', '/', '3.14', '*', 'tan']
229229
rpncalculator = RPNcalculator(rpn_tokens)
230230
result, error_msg = rpncalculator.evaluate()
231231
self.assertEqual(result, 11.009005500434151)
232232
self.assertEqual(error_msg, None)
233-
233+
234234
def test_evaluate_method_error_msg_zero_division(self):
235235
"""Is 'division by zero' error message created?"""
236236
rpn_tokens = ['2', '0', '/']
237237
rpncalculator = RPNcalculator(rpn_tokens)
238238
result, error_msg = rpncalculator.evaluate()
239239
self.assertEqual(error_msg, 'ERROR: float division by zero')
240-
240+
241241
def test_evaluate_method_error_msg_neg_num_in_fract_pow(self):
242242
"""Is 'negative number cannot be raised to a fractional power' error message created?"""
243243
rpn_tokens = [['-2', '0.5', '**'], ['-2', '0.5', '^']]
@@ -247,21 +247,21 @@ def test_evaluate_method_error_msg_neg_num_in_fract_pow(self):
247247
error_msgs.append(rpncalculator.evaluate()[1])
248248
for error_msg in error_msgs:
249249
self.assertEqual(error_msg, 'ERROR: negative number cannot be raised to a fractional power')
250-
250+
251251
def test_evaluate_method_error_msg_neg_num_sqrt(self):
252252
"""Is 'root can't be extracted from a negative number' error message created?"""
253253
rpn_tokens = ['-2', 'sqrt']
254254
rpncalculator = RPNcalculator(rpn_tokens)
255255
result, error_msg = rpncalculator.evaluate()
256256
self.assertEqual(error_msg, "ERROR: a root can't be extracted from a negative number")
257-
257+
258258
def test_evaluate_method_error_msg_invalid_syntax(self):
259259
"""Is 'invalid syntax' error message created?"""
260260
rpn_tokens = ['2', '+']
261261
rpncalculator = RPNcalculator(rpn_tokens)
262262
result, error_msg = rpncalculator.evaluate()
263263
self.assertEqual(error_msg, "ERROR: invalid syntax")
264-
264+
265265

266266
if __name__ == '__main__':
267267
unittest.main()

final_task/pycalc/__main__.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,17 @@ def createparser():
1717
parser = argparse.ArgumentParser(prog='pycalc', description='pure Python command line calculator',
1818
epilog="""Anton Charnichenka for EPAM: Introduction to Python
1919
and Golang programming, 2018.""")
20-
parser.add_argument('expression', help="""mathematical expression string to evaluate;
21-
implicit multiplication is supported""")
22-
20+
parser.add_argument('expression', help="""mathematical expression string to evaluate;
21+
implicit multiplication is supported""")
22+
2323
return parser
2424

25+
2526
def main():
26-
"""calculation chain"""
27+
"""Calculate user's expression"""
2728
parser = createparser()
2829
namespace = parser.parse_args(sys.argv[1:])
2930
user_expr = namespace.expression
30-
#main_input, spare_input = parser.parse_known_args(sys.argv[1:])
31-
# get user's expression
32-
#if main_input.expression:
33-
#user_expr = main_input.expression[0]
34-
#else:
35-
#user_expr = spare_input[0]
3631

3732
# calculation chain
3833
# tokenize user's expression string
@@ -65,7 +60,8 @@ def main():
6560
else:
6661
print(result)
6762
sys.exit(0)
68-
63+
64+
6965
# main
7066
if __name__ == "__main__":
7167
main()

final_task/pycalc/addmultsigns.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Multsignsadder():
88
"""A model of mult_signs_adder capable of adding implicit multiplications signs in list of tokens"""
9-
9+
1010
def __init__(self, tokens):
1111
"""Initialize mult_signs_adder"""
1212
self.tokens = tokens
@@ -52,7 +52,7 @@ def addmultsigns(self):
5252
for index in range(len(self.tokens)-1):
5353
self.extended_tokens.append(self.tokens[index])
5454
if (self.is_number(self.tokens[index]) and ((self.tokens[index+1] in self.constants)
55-
or (self.tokens[index+1] in self.functions)
55+
or (self.tokens[index+1] in self.functions)
5656
or (self.tokens[index+1] == '('))):
5757
self.extended_tokens.append('*')
5858
continue
@@ -68,7 +68,7 @@ def addmultsigns(self):
6868

6969

7070
if __name__ == '__main__':
71-
print("""This module contains class that allows to insert multiplications signs to where they where supposed
71+
print("""This module contains class that allows to insert multiplications signs to where they where supposed
7272
to be in a list with math tokens. For example: \n""")
7373
test_tokens = ['-0.1', 'tan', '+', '23', '*', '-sin', '(', '3', ')', '/', '.12', 'e']
7474
mult_signs_adder = Multsignsadder(test_tokens)

final_task/pycalc/constsreplacer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66

77
class Constsreplacer():
88
"""A model of constants replacer capable of replacing constants (from math module) by their numeric equivalents"""
9-
9+
1010
def __init__(self, rpn_tokens):
1111
"""Initialize constsreplacer"""
1212
self.rpn_tokens = rpn_tokens
1313
self.constants_numeric_equivalents = constants_numeric_equivalents
14-
14+
1515
def replace_constants(self):
1616
"""Replaces tokens which are math module constants by their numeric equivalent"""
1717
for index in range(len(self.rpn_tokens)):
1818
if self.rpn_tokens[index] in self.constants_numeric_equivalents.keys():
1919
self.rpn_tokens[index] = str(self.constants_numeric_equivalents[self.rpn_tokens[index]])
20-
20+
2121
return self.rpn_tokens
2222

2323

2424
if __name__ == '__main__':
25-
print("""This module contains class that allows to replace tokens which are math module constants by their
25+
print("""This module contains class that allows to replace tokens which are math module constants by their
2626
numeric equivalents. For example: \n""")
2727
test_tokens = ['2', '*', 'nan', '-', '-inf', '+', '-tau', '*', '-pi', '+', 'e']
2828
print('RPN tokens with constants: ', test_tokens)

final_task/pycalc/pycalclib.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
# acceptable constants and functions
3939
constants = ['e', 'pi', 'tau', 'inf', 'nan']
4040
negative_constants = ['-e', '-pi', '-tau', '-inf', '-nan']
41-
functions = ['acosh', 'acos', 'asinh', 'asin', 'atan2', 'atanh', 'atan', 'ceil', 'copysign', 'cosh', 'cos',
42-
'degrees', 'erfc', 'erf', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum',
43-
'gamma', 'gcd', 'hypot', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log10',
41+
functions = ['acosh', 'acos', 'asinh', 'asin', 'atan2', 'atanh', 'atan', 'ceil', 'copysign', 'cosh', 'cos',
42+
'degrees', 'erfc', 'erf', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum',
43+
'gamma', 'gcd', 'hypot', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log10',
4444
'log1p', 'log2', 'log', 'modf', 'pow', 'radians', 'sinh', 'sin', 'sqrt', 'tanh', 'tan', 'trunc',
4545
'round', 'abs']
4646

@@ -51,7 +51,7 @@
5151
'-pow', '-radians', '-sinh', '-sin', '-sqrt', '-tanh', '-tan', '-trunc', '-round', '-abs']
5252

5353

54-
# acceptable operators
54+
# acceptable operators
5555
operators = ['+', '-', '*', '/', '//', '%', '^', '**']
5656

5757

@@ -65,7 +65,7 @@
6565

6666

6767
# numeric equivalents of constants
68-
constants_numeric_equivalents = {'e': math.e, '-e': -math.e, 'pi': math.pi, '-pi': -math.pi, 'tau': math.tau,
68+
constants_numeric_equivalents = {'e': math.e, '-e': -math.e, 'pi': math.pi, '-pi': -math.pi, 'tau': math.tau,
6969
'-tau': -math.tau}
7070

7171

@@ -77,17 +77,17 @@
7777

7878
# function's actions
7979
# first element in a tuple associated with each key is a number of arguments for corresponding function
80-
functions_dict = {'acos': (1, math.acos), 'acosh': (1, math.acosh), 'asin': (1, math.asin), 'asinh': (1, math.asinh),
81-
'atan': (1, math.atan), 'atan2': (2, math.atan2), 'atanh': (1, math.atanh), 'ceil': (1, math.ceil),
82-
'copysign': (2, math.copysign), 'cos': (1, math.cos), 'cosh': (1, math.cosh),
83-
'degrees': (1, math.degrees), 'erf': (1, math.erf), 'erfc': (1, math.erfc), 'exp': (1, math.exp),
80+
functions_dict = {'acos': (1, math.acos), 'acosh': (1, math.acosh), 'asin': (1, math.asin), 'asinh': (1, math.asinh),
81+
'atan': (1, math.atan), 'atan2': (2, math.atan2), 'atanh': (1, math.atanh), 'ceil': (1, math.ceil),
82+
'copysign': (2, math.copysign), 'cos': (1, math.cos), 'cosh': (1, math.cosh),
83+
'degrees': (1, math.degrees), 'erf': (1, math.erf), 'erfc': (1, math.erfc), 'exp': (1, math.exp),
8484
'expm1': (1, math.expm1), 'fabs': (1, math.fabs), 'factorial': (1, math. factorial),
85-
'floor': (1, math.floor), 'fmod': (2, math.fmod), 'gamma': (1, math.gamma), 'gcd': (2, math.gcd),
86-
'hypot': (2, math.hypot), 'isfinite': (1, math.isfinite), 'isinf': (1, math.isinf),
85+
'floor': (1, math.floor), 'fmod': (2, math.fmod), 'gamma': (1, math.gamma), 'gcd': (2, math.gcd),
86+
'hypot': (2, math.hypot), 'isfinite': (1, math.isfinite), 'isinf': (1, math.isinf),
8787
'isnan': (1, math.isnan), 'ldexp': (2, math.ldexp), 'lgamma': (1, math.lgamma), 'log': (2, math.log),
8888
'log10': (1, math.log10), 'log1p': (1, math.log1p), 'log2': (1, math.log2), 'pow': (2, math.pow),
89-
'radians': (1, math.radians), 'sin': (1, math.sin), 'sinh': (1, math.sinh), 'sqrt': (1, math.sqrt),
90-
'tan': (1, math.tan), 'tanh': (1, math.tanh), 'trunc': (1, math.trunc), 'abs': (1, lambda x: abs(x)),
89+
'radians': (1, math.radians), 'sin': (1, math.sin), 'sinh': (1, math.sinh), 'sqrt': (1, math.sqrt),
90+
'tan': (1, math.tan), 'tanh': (1, math.tanh), 'trunc': (1, math.trunc), 'abs': (1, lambda x: abs(x)),
9191
'round': (1, lambda x: round(x)), '-abs': (1, lambda x: -abs(x))}
9292

9393
if __name__ == '__main__':

0 commit comments

Comments
 (0)