Skip to content
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

Solutions to prework #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
50 changes: 42 additions & 8 deletions exercise-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,52 @@
import unittest


EMAIL_PATTERN = re.compile(r'^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$')


class Email:
EMAIL_PATTERN = re.compile(r'^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$')

def __get__(self, instance, owner):
return self.value

def __set__(self, instance, value):
if not isinstance(value, str) or Email.EMAIL_PATTERN.match(value) is None:
raise ValueError('Invalid email')
self.value = value


class Person:
email = Email()

def __init__(self, first_name, last_name, email):
assert isinstance(first_name, str)
assert isinstance(last_name, str)
assert isinstance(email, str)

self.first_name = first_name

self.first_name = first_name
self.last_name = last_name
if EMAIL_PATTERN.match(email) is None:
raise ValueError('Invalid email')
else:
self.email = email
self.email = email


class TestPersonEmailValidation(unittest.TestCase):
def test_should_not_throw_exception_when_correct_mail_address_given(self):
correct_mail = '[email protected]'

p = Person('Jan', 'Kowalski', correct_mail)

self.assertEqual(p.email, correct_mail)

def test_should_throw_exception_when_invalid_mail_given(self):
incorrect_mails = [
'jan.kowalski',
'@gmail.com',
23,
dict(),
]

for incorrect_mail in incorrect_mails:
with self.subTest(email=incorrect_mail), self.assertRaises(ValueError):
p = Person('Jan', 'Kowalski', incorrect_mail)


if __name__ == '__main__':
unittest.main()
53 changes: 31 additions & 22 deletions exercise-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,38 @@ def __init__(self, first_purchase_date, birth_date, is_veteran):
self.is_veteran = is_veteran


def n_years_ago(n_years):
return datetime.datetime.now() - n_years * datetime.timedelta(days=365)


def loyality_discount_factory(min_loyality_years, discount):
def loyality_discount(customer):
return discount if customer.first_purchase_date and customer.first_purchase_date <= n_years_ago(min_loyality_years) else 0
return loyality_discount


def senior_discount(customer):
return 5 if customer.birth_date <= n_years_ago(65) else 0


def first_time_purchase_discount(customer):
return 15 if not customer.first_purchase_date else 0


def veteran_discount(customer):
return 10 if customer.is_veteran else 0


def calculate_discount_percentage(customer):
discount = 0
now = datetime.datetime.now()
year = datetime.timedelta(days=365)
if customer.birth_date <= now - 65*year:
# senior discount
discount = 5
if customer.first_purchase_date is not None:
if customer.first_purchase_date <= now - year:
# after one year, loyal customers get 10%
discount = 10
if customer.first_purchase_date <= now - 5*year:
# after five years, 12%
discount = 12
if customer.first_purchase_date <= now - 10*year:
# after ten years, 20%
discount = 20
else:
# first time purchase ==> 15% discount
discount = 15
if customer.is_veteran:
discount = max(discount, 10)
return discount
discounts = [
senior_discount,
loyality_discount_factory(min_loyality_years=1, discount=10),
loyality_discount_factory(min_loyality_years=5, discount=12),
loyality_discount_factory(min_loyality_years=10, discount=20),
first_time_purchase_discount,
veteran_discount
]
return max(discount(customer) for discount in discounts)


class CalculateDiscountPercentageTests(unittest.TestCase):
Expand Down