forked from CodeMouse92/DeadSimplePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoffee_order_decorator.py
More file actions
40 lines (27 loc) · 1018 Bytes
/
coffee_order_decorator.py
File metadata and controls
40 lines (27 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class CoffeeOrder:
def __init__(self, recipe, to_go=False):
self.recipe = recipe
self.to_go = to_go
def brew(self):
vessel = "in a paper cup" if self.to_go else "in a mug"
print("Brewing", *self.recipe.parts, vessel)
class CoffeeRecipe:
def __init__(self, parts):
self.parts = parts
special = CoffeeRecipe(["double-shot", "grande", "no-whip", "mocha"])
order = CoffeeOrder(special, to_go=False)
order.brew() # prints "Brewing double-shot grande no-whip mocha in a mug"
import functools
def auto_order(to_go):
def decorator(cls):
@functools.wraps(cls)
def wrapper(*args, **kwargs):
recipe = cls(*args, **kwargs)
return (CoffeeOrder(recipe, to_go), recipe)
return wrapper
return decorator
@auto_order(to_go=True)
class CoffeeShackRecipe(CoffeeRecipe):
pass
order, recipe = CoffeeShackRecipe(["tall", "decaf", "cappuccino"])
order.brew() # prints "Brewing tall decaf cappuccino in a paper cup"