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
interface User:
firstName: str
lastName: str
age: int
class User:
def __init__(self, firstName: str, lastName: str, age: int):
if (len(firstName) == 0 or len(lastName) == 0):
raise Exception("The strings is empty")
self.firstName = firstName
self.lastName = lastName
self.age = age
def createUser(userDto: User) -> str:
return f"{user.firstName} {user.lastName} {user.age}"
print(createUser(User(firstName="firstName", lastName="", age=0)))
print(createUser(firstName="firstName", lastName="lastName", age=20))
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js -d ormconfig.ts",
"migration:generate": "npm run build && npm run typeorm migration:generate ./database/migrations/%npm_config_name%",
"migration:create": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli migration:create ./database/migrations/%npm_config_name%",
"migration:run": "npm run build && npm run typeorm migration:run",
"migration:show": "npm run build && npm run typeorm migration:show",
"migration:revert": "npm run build && npm run typeorm migration:revert",
"migration:drop": "npm run build && npm run typeorm schema:drop",
"migration:fresh": "npm run build && npm run typeorm schema:drop && npm run typeorm migration:run"
npm run migration:generate --name=table
npm run migration:create --name=table
npm run migration:run
npm run migration:revert
npm run migration:show -f
npm run migration:drop
npm run migration:fresh
//ormconfig
import { DataSource } from 'typeorm';
const dataSource = new DataSource({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'app1',
entities: ['dist/**/*.entity{.ts,.js}'],
synchronize: false,
migrations: ['dist/database/migrations/**/*{.ts,.js}'],
migrationsTableName: 'migrations',
});
export default dataSource;