Skip to content

Calculator Exercise - #146

Open
yonatanevantal wants to merge 16 commits into
osherdp:masterfrom
yonatanevantal:yonatan/calculator
Open

Calculator Exercise#146
yonatanevantal wants to merge 16 commits into
osherdp:masterfrom
yonatanevantal:yonatan/calculator

Conversation

@yonatanevantal

Copy link
Copy Markdown
Collaborator

No description provided.

@yonatanevantalyonatanevantal self-assigned this Apr 21, 2020
Comment threadcalculator/calculator.py
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/test_unittest_calculator.py Outdated
Comment threadcalculator/test_unittest_calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/test_unittest_calculator.py
Comment threadcalculator/test_unittest_calculator.py
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment on lines +6 to +10
import math
import operator
import re
from collections import namedtuple
from sys import maxsize

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reorder

Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment on lines +15 to +16
# higher precedence means the operator stronger (will be done first)
Operator = namedtuple("Operator", ["operation", "precedence", "regex"])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Comment threadcalculator/calculator.py Outdated
Comment on lines +18 to +46
OPERATORS = {
'+': Operator(precedence=1, operation=operator.add,
regex=fr"({ANY_NUMBER})\+({ANY_NUMBER})"),
'-': Operator(precedence=1, operation=operator.sub,
regex=fr"({ANY_NUMBER})-({ANY_NUMBER})"),

'*': Operator(precedence=2, operation=operator.mul,
regex=fr"({ANY_NUMBER})\*({ANY_NUMBER})"),
'/': Operator(precedence=2, operation=operator.truediv,
regex=fr"({ANY_NUMBER})/({ANY_NUMBER})"),

'^': Operator(precedence=3, operation=math.pow,
regex=fr"({ANY_NUMBER})\^({ANY_NUMBER})"),
'%': Operator(precedence=4, operation=math.fmod,
regex=fr"({ANY_NUMBER})%({ANY_NUMBER})"),

'@': Operator(precedence=5, operation=lambda x, y: (x + y) / 2.0,
regex=fr"({ANY_NUMBER})@({ANY_NUMBER})"),
'$': Operator(precedence=5, operation=max,
regex=fr"({ANY_NUMBER})\$({ANY_NUMBER})"),
'&': Operator(precedence=5, operation=min,
regex=fr"({ANY_NUMBER})&({ANY_NUMBER})"),

'~': Operator(precedence=6, operation=operator.neg,
regex=fr"~({ANY_NUMBER})"),
'!': Operator(precedence=7, operation=math.factorial,
regex=fr"({ANY_NUMBER})!"),

'(': Operator(precedence=maxsize, operation=lambda expr: _evaluate(expr),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a dict?

Comment threadcalculator/calculator.py Outdated
Comment on lines +51 to +74
def operators_info_by_precedence() -> list:
"""Sort the value of each item in OPERATORS by its priority.

Returns:
List of Operator named tuples by priority, strong to weak.
"""
return sorted(OPERATORS.values(), key=operator.attrgetter("precedence"),
reverse=True)


def replace_in_expression(operator_regex: str, expression: str,
new_expr: str) -> str:
"""Change the old sub expression to a new one according to pattern.

Args:
operator_regex: pattern to find a sub expression of the operator.
expression: the entire expression.
new_expr: the expression to put instead of the old one.

Returns:
A new whole expression, changed according to pattern.
"""
old_expression = re.search(operator_regex, expression).group()
return expression.replace(old_expression, new_expr)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of this functions seems redundant.
Just inline those lines, its simple enough and clear.

Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
if operator_info.precedence != maxsize:
operands = list(map(float, operands))

result = operator_info.operation(*operands)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! thats more like it 👍

operator_regex, expression, str(result))

return _evaluate(expression)
return float(expression)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline (conventions)

Comment threadcalculator/calculator.py Outdated
Comment on lines +104 to +108
raw_expression = input(USER_MSG)

while raw_expression.lower() != "quit":
print(_evaluate(raw_expression))
raw_expression = input(USER_MSG)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read about iter

Comment threadcalculator/test_calculator.py Outdated


class TestEdgeCases(unittest.TestCase):
"""Test rare cases of inputs to see function behavior"""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dot in the end of the line.

Comment on lines +55 to +57
with self.assertRaises(ValueError):
_evaluate('1+a*76')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the output of the expression : -1-2*-2

Comment threadcalculator/calculator.py
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/test_calculator.py Outdated
Comment threadcalculator/test_calculator.py
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Comment threadcalculator/calculator.py Outdated
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@yonatanevantal@rfire01@yairigal@NivosMagnivos@jacksonpc2