import dataclasses
from enum import Enum
"""
def calculate(x, y) -> int:
return {"+": x + y, "-": x - y, "*": x * y, "/": x / y, "%": x % y}
print(calculate(1, 2)["+"])
print(calculate(1, 2)["-"])
print(calculate(1, 2)["/"])
print(calculate(1, 2)["*"])
print(calculate(1, 2)["%"])
"""
"""
@dataclasses.dataclass
class Operands:
x: int
y: int
class Operators(Enum):
PLUS = "+"
MINUS = "-"
MULTIPLY = "*"
DIVISION = "/"
MODULO = "%"
def calculate(operands: Operands) -> int:
return {Operators.PLUS: adding(operands), Operators.MINUS: subtracting(operands), Operators.MULTIPLY: multiplying(operands), Operators.DIVISION: dividing(operands), Operators.MODULO: modularDivision(operands)}
def adding(operands: Operands) -> int:
return operands.x + operands.y
def subtracting(operands: Operands) -> int:
return operands.x - operands.y
def multiplying(operands: Operands) -> int:
return operands.x * operands.y
def dividing(operands: Operands) -> int:
return operands.x / operands.y
def modularDivision(operands: Operands) -> int:
return operands.x % operands.y
print(calculate(Operands(x=1, y=2))[Operators.PLUS])
print(calculate(Operands(x=1, y=2))[Operators.MINUS])
print(calculate(Operands(x=1, y=2))[Operators.DIVISION])
print(calculate(Operands(x=1, y=2))[Operators.MULTIPLY])
print(calculate(Operands(x=1, y=2))[Operators.MODULO])
"""
@dataclasses.dataclass
class Operands:
x: int
y: int
class Operators(Enum):
PLUS = "+"
MINUS = "-"
MULTIPLY = "*"
DIVISION = "/"
MODULO = "%"
All = "all"
def calculate(operands: Operands) -> int:
return {Operators.PLUS: adding(operands), Operators.MINUS: subtracting(operands), Operators.MULTIPLY: multiplying(operands), Operators.DIVISION: dividing(operands), Operators.MODULO: modularDivision(operands)}
def adding(operands: Operands) -> int:
return operands.x + operands.y
def subtracting(operands: Operands) -> int:
return operands.x - operands.y
def multiplying(operands: Operands) -> int:
return operands.x * operands.y
def dividing(operands: Operands) -> int:
return operands.x / operands.y
def modularDivision(operands: Operands) -> int:
return operands.x % operands.y
def calculate(operands: Operands, operators: Operators) -> int:
match operators:
case operators.PLUS:
print(adding(operands))
case operators.MINUS:
print(subtracting(operands))
case operators.MULTIPLY:
print(multiplying(operands))
case operators.DIVISION:
print(dividing(operands))
case operators.MODULO:
print(modularDivision(operands))
case operators.All:
print(f'Adding (+) = {adding(operands)}')
print(f'Subtracting (-) = {subtracting(operands)}')
print(f'Multiplying (*) = {multiplying(operands)}')
print(f'Dividing (/) = {dividing(operands)}')
print(f'Modular division (%) = {modularDivision(operands)}')
case _:
print("There is no such operator!")
calculate(Operands(x=1, y=2), Operators.PLUS)
calculate(Operands(x=1, y=2), Operators.MINUS)
calculate(Operands(x=1, y=2), Operators.MULTIPLY)
calculate(Operands(x=1, y=2), Operators.DIVISION)
calculate(Operands(x=1, y=2), Operators.MODULO)
calculate(Operands(x=1, y=2), Operators.All)
interface supports Python 2.7 and Python 3.4+.
To install interface you have to
pip install python-interface
Example Code:
from interface import implements, Interface
class MyInterface(Interface):
def method1(self, x):
pass
def method2(self, x, y):
pass
class MyClass(implements(MyInterface)):
def method1(self, x):
return x * 2
def method2(self, x, y):
return x + y
JS. Просто о сложном: filter, map, reduce