Skip to content

Commit

Permalink
Update code for class on March 10, 2020
Browse files Browse the repository at this point in the history
  • Loading branch information
ariannedee committed Mar 9, 2020
1 parent 543fed0 commit 42183b8
Show file tree
Hide file tree
Showing 18 changed files with 251 additions and 260 deletions.
23 changes: 23 additions & 0 deletions Examples/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from enum import Enum


class Weekday(Enum):
MONDAY = 1
TUESDAY = 2
WEDNESDAY = 3
THURSDAY = 4
FRIDAY = 5
SATURDAY = 6
SUNDAY = 7


weekdays = (Weekday.MONDAY, Weekday.TUESDAY, Weekday.WEDNESDAY, Weekday.THURSDAY, Weekday.FRIDAY)
weekends = (Weekday.SATURDAY, Weekday.SUNDAY)

weekday_wake_up = "7am"
weekend_wake_up = "9am"

for day in Weekday:
print(f'Today is {day.name.capitalize()}')
wake_time = weekday_wake_up if day in weekdays else weekend_wake_up
print(f'Wake up at {wake_time}\n')
7 changes: 4 additions & 3 deletions Examples/example_10_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def __init__(self, length):
super().__init__(length, length)


shapes = [Square(10), Circle(20), Rectangle(3.4, 1.5)]
if __name__ == '__main__':
shapes = [Square(10), Circle(20), Rectangle(3.4, 1.5)]

for shape in shapes:
print(f'{shape} area is {shape.area()}')
for shape in shapes:
print(f'{shape} area is {shape.area()}')
7 changes: 4 additions & 3 deletions Examples/example_11_args_kwargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def __init__(self, length, *args, **kwargs):
super().__init__(length, length, *args, **kwargs)


shapes = [Square(10, x=0, y=0), Circle(20, -1, 1), Rectangle(3.4, 1.5, 20, y=5)]
if __name__ == '__main__':
shapes = [Square(10, x=0, y=0), Circle(20, -1, 1), Rectangle(3.4, 1.5, 20, y=5)]

for shape in shapes:
print(f'{shape} area is {shape.area()}')
for shape in shapes:
print(f'{shape} area is {shape.area()}')
50 changes: 50 additions & 0 deletions Examples/example_12_inheritance_with_abc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import abc # Abstract Base Class
from math import pi


class Shape(abc.ABC):
@abc.abstractmethod
def area(self):
pass

@abc.abstractmethod
def circumference(self):
pass

def __str__(self):
return type(self).__name__


class Circle(Shape):
def __init__(self, r):
self.r = r

def area(self):
return pi * self.r ** 2

def circumference(self):
return 2 * pi * self.r


class Rectangle(Shape):
def __init__(self, length, width):
self.l = length
self.w = width

def area(self):
return self.l * self.w

def circumference(self):
return 2 * self.l + 2 * self.w


class Square(Rectangle):
def __init__(self, length):
super().__init__(length, length)


if __name__ == '__main__':
shapes = [Square(10), Circle(20), Rectangle(3.4, 1.5)]

for shape in shapes:
print(f'{shape} area is {shape.area()}')
42 changes: 15 additions & 27 deletions Examples/example_1_bike_without_classes.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,38 @@
from datetime import datetime
def update_sale_price(bike, sale_price):
if bike['sold'] is True:
raise Exception("Action not allowed. Bike has already been sold")
bike['sale_price'] = sale_price


def lookup_msrp_value(make, model):
"""
Determine original sale price of a bike when new
"""
return 1000


def set_sale_price(bike):
original_value = lookup_msrp_value(bike['make'], bike['model'])
current_year = datetime.now().year
current_value = original_value * (1 - (current_year - bike['year']) * 0.015)
current_value = current_value * bike['condition']
bike['sale_price'] = current_value


def create_bike(cost, make, model, year, condition):
def create_bike(description, cost, sale_price, condition):
return {
'description': description,
'cost': cost,
'make': make,
'model': model,
'year': year,
'sale_price': sale_price,
'condition': condition,
'sold': False,
}


def sell(bike):
def sell(bike, sold_for=None):
if sold_for:
update_sale_price(bike, sold_for)
bike['sold'] = True
profit = bike['sale_price'] - bike['cost']
return profit


bike1 = create_bike(100, 'Univega', 'Alpina', 1999, 0.5)
bike1 = create_bike('Univega Alpina, orange', cost=100, sale_price=500, condition=0.5)
# bike1 = {
# 'cost': 100,
# 'make': 'Univega',
# 'model': 'Alpina',
# 'year': 1999,
# 'condition': 0.5,
# 'description': 'Univega Alpina, orange',
# 'sale_price': 500,
# 'sold': False,
# }

set_sale_price(bike1)
update_sale_price(bike1, 350)
# bike1['sale_price'] = 350.00

sell(bike1)
print(sell(bike1))
# bike1['sold'] = True
10 changes: 4 additions & 6 deletions Examples/example_2_using_classes.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from Examples.example_5_methods import Bike, Condition

bike = Bike(100, 'Univega', 'Alpina', 1999, Condition.OKAY)
bike = Bike('Univega Alpina, orange', Condition.OKAY, sale_price=500, cost=100)

bike.update_sale_price() # sale price = $350
bike.service(spent=30, sale_price=600) # cost=$130, sale_price=$600

bike.service(30, Condition.GOOD) # cost = $130
print(bike.sale_price) # 600

print(bike.sale_price) # sale price = $560

bike.sell() # profit = $430
print(bike.sell()) # sold=True
14 changes: 6 additions & 8 deletions Examples/example_4_attributes.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
class Bike(object):
def __init__(self, cost, make, model, year, condition):
def __init__(self, description, condition, sale_price, cost=0):
# Different initial values for every new instance
self.cost = cost
self.make = make
self.model = model
self.year = year
self.description = description
self.condition = condition
self.sale_price = sale_price
self.cost = cost

# Same initial values for every new instance
self.sale_price = None
# Same initial value for every new instance
self.sold = False

def update_sale_price(self):
Expand All @@ -17,5 +15,5 @@ def update_sale_price(self):
def sell(self):
pass

def service(self, cost, new_condition):
def service(self):
pass
49 changes: 19 additions & 30 deletions Examples/example_5_methods.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from datetime import datetime
from enum import Enum


Expand All @@ -9,27 +8,23 @@ class Condition(Enum):
BAD = 0.2


class MethodNotAllowed(Exception):
pass


class Bike(object):
def __init__(self, cost, make, model, year, condition):
self.cost = cost
self.make = make
self.model = model
self.year = year
def __init__(self, description, condition, sale_price, cost=0):
self.description = description
self.condition = condition
self.sale_price = sale_price
self.cost = cost

self.sale_price = self.update_sale_price()
self.sold = False

def update_sale_price(self):
"""
Set the current sale price based on the make, model, age, and condition
"""
original_value = lookup_msrp_value(self.make, self.model)
current_year = datetime.now().year
current_value = original_value * (1 - (current_year - self.year) * 0.015)
current_value = current_value * self.condition.value
self.sale_price = current_value
return self.sale_price
def update_sale_price(self, sale_price):
if self.sold:
raise MethodNotAllowed('Bike has already been sold')
self.sale_price = sale_price

def sell(self):
"""
Expand All @@ -39,18 +34,12 @@ def sell(self):
profit = self.sale_price - self.cost
return profit

def service(self, cost, new_condition):
def service(self, spent, sale_price=None, condition=None):
"""
Service the bike and update sale price
Service the bike and update attributes
"""
self.cost += cost
self.condition = new_condition
self.update_sale_price()
return self.sale_price


def lookup_msrp_value(make, model):
"""
Determine original sale price of a bike when new
"""
return 1000
self.cost += spent
if sale_price:
self.update_sale_price(sale_price)
if self.condition:
self.condition = condition
64 changes: 25 additions & 39 deletions Examples/example_6_dunder_methods.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from datetime import datetime
from enum import Enum


Expand All @@ -10,32 +9,22 @@ class Condition(Enum):


class Bike(object):

def __init__(self, cost, make, model, year, condition):
self.cost = cost
self.make = make
self.model = model
self.year = year
def __init__(self, description, condition, sale_price, cost=0):
self.description = description
self.condition = condition
self.sale_price = sale_price
self.cost = cost

self.sale_price = self.update_sale_price()
self.sold = False

print(f'New bike: {self}')

def __del__(self):
print(f'Deleting bike: {self}')

def update_sale_price(self):
"""
Set the current sale price based on the make, model, age, and condition
"""
original_value = lookup_msrp_value(self.make, self.model)
current_year = datetime.now().year
current_value = original_value * (1 - (current_year - self.year) * 0.015)
current_value = current_value * self.condition.value
self.sale_price = current_value
return self.sale_price
def update_sale_price(self, sale_price):
if self.sold:
return Exception('Action not allowed. Bike has already been sold')
self.sale_price = sale_price

def sell(self):
"""
Expand All @@ -45,36 +34,33 @@ def sell(self):
profit = self.sale_price - self.cost
return profit

def service(self, cost, new_condition):
def service(self, spent, sale_price=None, condition=None):
"""
Service the bike and update sale price
Service the bike and update attributes
"""
self.cost += cost
self.condition = new_condition
self.update_sale_price()
return self.sale_price
self.cost += spent
if sale_price:
self.update_sale_price(sale_price)
if self.condition:
self.condition = condition

def __repr__(self):
return f'Bike: {self.year} {self.make} {self.model} ({"sold" if self.sold else self.sale_price})'
sold_or_price = "sold" if self.sold else f"${self.sale_price}"
return f'Bike: {self.description} ({sold_or_price})'

def __str__(self):
return f'{self.year} {self.make} {self.model}'


def lookup_msrp_value(make, model):
"""
Determine original sale price of a bike when new
"""
return 1000
return self.description


if __name__ == '__main__':
bike = Bike(100, 'Univega', 'Alpina', 1999, Condition.OKAY) # __init__ called
bike = Bike('Univega Alpina, orange', Condition.OKAY, sale_price=500, cost=100)

print(bike) # __str__ called
print(bike) # __str__ called
print(str(bike)) # __str__ called

print([bike]) # __repr__ called
print([bike]) # __repr__ called
print(repr(bike)) # __repr__ called

del bike # __del__ called
del bike # __del__ called

Bike(0, 'Raleigh', 'Talus 2', 2013, Condition.BAD) # __init__ and __del__ called
bike = Bike('Raleigh Talus 2', Condition.BAD, sale_price=20) # __init__ and __del__ called
Loading

0 comments on commit 42183b8

Please sign in to comment.