Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b7c6e70

Browse files
committed
class func
1 parent 67c1a0e commit b7c6e70

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

‎05-1class-func/bingocall.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import random
4+
5+
6+
class BingoCage:
7+
8+
def __init__(self, items):
9+
self._items = list(items)
10+
random.shuffle(self._items)
11+
12+
def pick(self):
13+
try:
14+
return self._items.pop()
15+
except IndexError:
16+
raise LookupError('pick from empty BingoCage')
17+
18+
def __call__(self):
19+
return self.pick()

‎05-1class-func/clip.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
def clip(text: str, max_len: 'int > 0'=80) -> str:
5+
"""Return text clipped at the last space before or after max_len"""
6+
end = None
7+
if len(text) > max_len:
8+
space_before = text.rfind(' ', 0, max_len)
9+
if space_before > 0:
10+
end = space_before
11+
else:
12+
space_after = text.rfind(' ', max_len)
13+
if space_after >= 0:
14+
end = space_after
15+
if end is None:
16+
end = len(text)
17+
return text[:end].rstrip()

‎05-1class-func/tagger.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
def tag(tag_name, *content, cls=None, **attrs):
5+
"""Generate one or more HTML tags"""
6+
if cls is not None:
7+
attrs['class'] = cls
8+
if attrs:
9+
attr_str = ''.join(' %s="%s"' % (attr, value)
10+
for attr, value in sorted(attrs.items()))
11+
else:
12+
attr_str = ''
13+
if content:
14+
return '\n'.join('<%s%s>%s<%s>' % (tag_name, attr_str, c, tag_name)
15+
for c in content)
16+
else:
17+
return '<%s%s />' % (tag_name, attr_str)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from abc import ABC, abstractmethod
4+
from collections import namedtuple
5+
6+
Customer = namedtuple('Customer', 'name fidelity')
7+
8+
9+
class LineItem:
10+
11+
def __init__(self, product, quantity, price):
12+
self.product = product
13+
self.quantity = quantity
14+
self.price = price
15+
16+
def total(self):
17+
return self.price * self.quantity
18+
19+
20+
class Order:
21+
22+
def __init__(self, customer, cart, promotion=None):
23+
self.customer = customer
24+
self.cart = list(cart)
25+
self.promotion = promotion
26+
27+
def total(self):
28+
if not hasattr(self, '__total'):
29+
self.__total = sum(item.total() for item in self.cart)
30+
return self.__total
31+
32+
def due(self):
33+
if self.promotion is None:
34+
discount = 0
35+
else:
36+
discount = self.promotion.discount(self)
37+
return self.total() - discount
38+
39+
def __repr__(self):
40+
fmt = '<Order total: {:.2f} due: {:.2f}>'
41+
return fmt.format(self.total(), self.due())
42+
43+
44+
class Promotion(ABC):
45+
46+
@abstractmethod
47+
def discount(self, order):
48+
"Return discount as a positive dollar amount"
49+
50+
51+
class FidelityPromo(Promotion):
52+
"""5% discount for customers with 1000 or more fidelity points"""
53+
54+
def discount(self, order):
55+
return order.total() * 0.05 if order.customer.fidelity >= 1000 else 0
56+
57+
58+
class BulkItemPromo(Promotion):
59+
"""10% discount for each LineItem with 20 or more units"""
60+
61+
def discount(self, order):
62+
discount = 0
63+
for item in order.cart:
64+
if item.quantity >= 20:
65+
discount += item.total() * 0.1
66+
return discount
67+
68+
69+
class LargeOrderPromo(Promotion):
70+
"""7% discount for orders with 10 or more distinct items"""
71+
72+
def discount(self, order):
73+
distinct_items = {item.product for item in order.cart}
74+
if len(distinct_items) >= 10:
75+
return order.total() * 0.07
76+
return 0

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /