Skip to content

Create dependency-inversion-after-comp.py #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions 9 - solid/dependency-inversion-after-comp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env python3


class Order:

def __init__(self):
self.items = []
self.quantities = []
self.prices = []
self.status = "open"

def add_item(self, name, quantity, price):
self.items.append(name)
self.quantities.append(quantity)
self.prices.append(price)

def total_price(self):
total = 0
for i in range(len(self.prices)):
total += self.quantities[i] * self.prices[i]
return total
Comment on lines +18 to +21
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
total = 0
for i in range(len(self.prices)):
total += self.quantities[i] * self.prices[i]
return total
return sum(quantity * price for quantity, price in zip(self.quantities, self.prices))



class SMSAuth:

def __init__(self):
self.authorized = False

def verify_code(self, code):
print(f'Verifying SMS code {code!r}')
self.authorized = True


class GoogleAuth:

def __init__(self):
self.authorized = False

def verify_code(self, code):
print(f'Verifying GoogleAuth code {code!r}')
self.authorized = True


class NotARobotAuth:

def __init__(self):
self.authorized = False

def not_a_robot(self):
self.authorized = True


class DebitPaymentProcessor:

def __init__(self, security_code, authorizer):
self.security_code = security_code
self.authorizer = authorizer

def pay(self, order):
if not self.authorizer.authorized:
raise Exception("Not authorized")
print("Processing debit payment type")
print(f"Verifying security code: {self.security_code}")
order.status = "paid"


class CreditPaymentProcessor:

def __init__(self, security_code):
self.security_code = security_code

def pay(self, order):
print("Processing credit payment type")
print(f"Verifying security code: {self.security_code}")
order.status = "paid"


class PaypalPaymentProcessor:

def __init__(self, email_address, authorizer):
self.email_address = email_address
self.authorizer = authorizer

def pay(self, order):
if not self.authorizer.authorized:
raise Exception("Not authorized")
print("Processing paypal payment type")
print(f"Using email address: {self.email_address}")
order.status = "paid"


order = Order()
order.add_item("Keyboard", 1, 50)
order.add_item("SSD", 1, 150)
order.add_item("USB cable", 2, 5)

print(order.total_price())
authorizer = GoogleAuth()
authorizer.verify_code(9445)
processor = PaypalPaymentProcessor("hi@arjancodes.com", authorizer)
processor.pay(order)