Skip to content

Commit 2356559

Browse files
pg.191
1 parent b1b0950 commit 2356559

File tree

7 files changed

+310
-16
lines changed

7 files changed

+310
-16
lines changed

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-fluent

Part_3-Fuctions_as_Objects/Chap_6-Design_Patterns_With_First_Class_Funcions/ex_6_4__example_function_strategy.ipynb

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": 5,
66
"metadata": {},
77
"outputs": [
88
{
@@ -11,7 +11,7 @@
1111
"<Order total: 42.00 due: 39.90>"
1212
]
1313
},
14-
"execution_count": 1,
14+
"execution_count": 5,
1515
"metadata": {},
1616
"output_type": "execute_result"
1717
}
@@ -37,7 +37,7 @@
3737
},
3838
{
3939
"cell_type": "code",
40-
"execution_count": 2,
40+
"execution_count": 6,
4141
"metadata": {},
4242
"outputs": [
4343
{
@@ -46,7 +46,7 @@
4646
"<Order total: 42.00 due: 39.90>"
4747
]
4848
},
49-
"execution_count": 2,
49+
"execution_count": 6,
5050
"metadata": {},
5151
"output_type": "execute_result"
5252
}
@@ -57,7 +57,7 @@
5757
},
5858
{
5959
"cell_type": "code",
60-
"execution_count": 3,
60+
"execution_count": 7,
6161
"metadata": {},
6262
"outputs": [
6363
{
@@ -66,7 +66,7 @@
6666
"<Order total: 30.00 due: 28.50>"
6767
]
6868
},
69-
"execution_count": 3,
69+
"execution_count": 7,
7070
"metadata": {},
7171
"output_type": "execute_result"
7272
}
@@ -81,19 +81,18 @@
8181
},
8282
{
8383
"cell_type": "code",
84-
"execution_count": 4,
84+
"execution_count": 8,
8585
"metadata": {},
8686
"outputs": [
8787
{
88-
"ename": "NameError",
89-
"evalue": "name 'large_order_promo' is not defined",
90-
"output_type": "error",
91-
"traceback": [
92-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
93-
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
94-
"\u001b[0;32m<ipython-input-4-80e58dad44d7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mitem_code\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m ]\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mOrder\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjoe\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlong_order\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlarge_order_promo\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
95-
"\u001b[0;31mNameError\u001b[0m: name 'large_order_promo' is not defined"
96-
]
88+
"data": {
89+
"text/plain": [
90+
"<Order total: 10.00 due: 9.30>"
91+
]
92+
},
93+
"execution_count": 8,
94+
"metadata": {},
95+
"output_type": "execute_result"
9796
}
9897
],
9998
"source": [
@@ -104,6 +103,26 @@
104103
"Order(joe, long_order, large_order_promo)"
105104
]
106105
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": 9,
109+
"metadata": {},
110+
"outputs": [
111+
{
112+
"data": {
113+
"text/plain": [
114+
"<Order total: 42.00 due: 42.00>"
115+
]
116+
},
117+
"execution_count": 9,
118+
"metadata": {},
119+
"output_type": "execute_result"
120+
}
121+
],
122+
"source": [
123+
"Order(joe, cart, large_order_promo)"
124+
]
125+
},
107126
{
108127
"cell_type": "code",
109128
"execution_count": null,
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"text/plain": [
11+
"<Order total: 10.00 due: 9.30>"
12+
]
13+
},
14+
"execution_count": 1,
15+
"metadata": {},
16+
"output_type": "execute_result"
17+
}
18+
],
19+
"source": [
20+
"from ex_6_3__functional_strategy import (\n",
21+
" Customer,\n",
22+
" LineItem,\n",
23+
" Order,\n",
24+
" fidelity_promo,\n",
25+
" bulk_item_promo,\n",
26+
" large_order_promo,\n",
27+
")\n",
28+
"\n",
29+
"joe = Customer('John Doe', 0)\n",
30+
"ann = Customer('Ann Smith', 1100)\n",
31+
"\n",
32+
"promos = [fidelity_promo, bulk_item_promo, large_order_promo]\n",
33+
"\n",
34+
"def best_promo(order):\n",
35+
" \"\"\"Select best discount available\n",
36+
" \"\"\"\n",
37+
" return max(promo(order) for promo in promos)\n",
38+
"\n",
39+
"long_order = [\n",
40+
" LineItem(str(item_code), 1, 1.0)\n",
41+
" for item_code in range(10)\n",
42+
"]\n",
43+
"\n",
44+
"banana_cart = [\n",
45+
" LineItem('banana', 30, .5),\n",
46+
" LineItem('apple', 10, 1.5),\n",
47+
"]\n",
48+
"\n",
49+
"cart = [\n",
50+
" LineItem('banana', 4, .5),\n",
51+
" LineItem('apple', 10, 1.5),\n",
52+
" LineItem('watermellon', 5, 5.0),\n",
53+
"]\n",
54+
"\n",
55+
"Order(joe, long_order, best_promo)"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 2,
61+
"metadata": {},
62+
"outputs": [
63+
{
64+
"data": {
65+
"text/plain": [
66+
"<Order total: 30.00 due: 28.50>"
67+
]
68+
},
69+
"execution_count": 2,
70+
"metadata": {},
71+
"output_type": "execute_result"
72+
}
73+
],
74+
"source": [
75+
"Order(joe, banana_cart, best_promo)"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 3,
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"data": {
85+
"text/plain": [
86+
"<Order total: 42.00 due: 39.90>"
87+
]
88+
},
89+
"execution_count": 3,
90+
"metadata": {},
91+
"output_type": "execute_result"
92+
}
93+
],
94+
"source": [
95+
"Order(ann, cart, best_promo)"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"metadata": {},
102+
"outputs": [],
103+
"source": []
104+
}
105+
],
106+
"metadata": {
107+
"interpreter": {
108+
"hash": "91801c9ce6957ebd0e9246650a12ff0ec9065db97224ddc8ea854cc44b68487c"
109+
},
110+
"kernelspec": {
111+
"display_name": "Python 3.8.10 64-bit ('python-fluent': pyenv)",
112+
"language": "python",
113+
"name": "python3"
114+
},
115+
"language_info": {
116+
"codemirror_mode": {
117+
"name": "ipython",
118+
"version": 3
119+
},
120+
"file_extension": ".py",
121+
"mimetype": "text/x-python",
122+
"name": "python",
123+
"nbconvert_exporter": "python",
124+
"pygments_lexer": "ipython3",
125+
"version": "3.8.10"
126+
},
127+
"orig_nbformat": 4
128+
},
129+
"nbformat": 4,
130+
"nbformat_minor": 2
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 3,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"text/plain": [
11+
"[<function ex_6_3__functional_strategy.fidelity_promo(order)>,\n",
12+
" <function ex_6_3__functional_strategy.bulk_item_promo(order)>,\n",
13+
" <function ex_6_3__functional_strategy.large_order_promo(order)>]"
14+
]
15+
},
16+
"execution_count": 3,
17+
"metadata": {},
18+
"output_type": "execute_result"
19+
}
20+
],
21+
"source": [
22+
"promos = [globals()[name] for name in globals()\n",
23+
" if name.endswith('_promo')\n",
24+
" and name != 'best_promo']\n",
25+
"\n",
26+
"def best_promo(order):\n",
27+
" \"\"\"Select best discount available\n",
28+
" \"\"\"\n",
29+
" return max(promo(order) for promo in promos)\n",
30+
"\n",
31+
"promos"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"metadata": {},
38+
"outputs": [],
39+
"source": []
40+
}
41+
],
42+
"metadata": {
43+
"interpreter": {
44+
"hash": "91801c9ce6957ebd0e9246650a12ff0ec9065db97224ddc8ea854cc44b68487c"
45+
},
46+
"kernelspec": {
47+
"display_name": "Python 3.8.10 64-bit ('python-fluent': pyenv)",
48+
"language": "python",
49+
"name": "python3"
50+
},
51+
"language_info": {
52+
"codemirror_mode": {
53+
"name": "ipython",
54+
"version": 3
55+
},
56+
"file_extension": ".py",
57+
"mimetype": "text/x-python",
58+
"name": "python",
59+
"nbconvert_exporter": "python",
60+
"pygments_lexer": "ipython3",
61+
"version": "3.8.10"
62+
},
63+
"orig_nbformat": 4
64+
},
65+
"nbformat": 4,
66+
"nbformat_minor": 2
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"text/plain": [
11+
"[<function ex_6_3__functional_strategy.bulk_item_promo(order)>,\n",
12+
" <function ex_6_3__functional_strategy.fidelity_promo(order)>,\n",
13+
" <function ex_6_3__functional_strategy.large_order_promo(order)>,\n",
14+
" <function collections.namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)>]"
15+
]
16+
},
17+
"execution_count": 1,
18+
"metadata": {},
19+
"output_type": "execute_result"
20+
}
21+
],
22+
"source": [
23+
"import inspect\n",
24+
"import ex_6_3__functional_strategy\n",
25+
"\n",
26+
"promos = [\n",
27+
" func for name, func in\n",
28+
" inspect.getmembers(ex_6_3__functional_strategy, inspect.isfunction)\n",
29+
"]\n",
30+
"\n",
31+
"promos"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"metadata": {},
38+
"outputs": [],
39+
"source": []
40+
}
41+
],
42+
"metadata": {
43+
"interpreter": {
44+
"hash": "91801c9ce6957ebd0e9246650a12ff0ec9065db97224ddc8ea854cc44b68487c"
45+
},
46+
"kernelspec": {
47+
"display_name": "Python 3.8.10 64-bit ('python-fluent': pyenv)",
48+
"language": "python",
49+
"name": "python3"
50+
},
51+
"language_info": {
52+
"codemirror_mode": {
53+
"name": "ipython",
54+
"version": 3
55+
},
56+
"file_extension": ".py",
57+
"mimetype": "text/x-python",
58+
"name": "python",
59+
"nbconvert_exporter": "python",
60+
"pygments_lexer": "ipython3",
61+
"version": "3.8.10"
62+
},
63+
"orig_nbformat": 4
64+
},
65+
"nbformat": 4,
66+
"nbformat_minor": 2
67+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class MacroCommand:
2+
"""A command that executes a list of commands"""
3+
4+
def __init__(self, commands):
5+
self.commands = list(commands)
6+
7+
def __call__(self):
8+
for command in self.commands:
9+
command()

Part_3-Fuctions_as_Objects/Chap_6-Design_Patterns_With_First_Class_Funcions/promotions.py

Whitespace-only changes.

0 commit comments

Comments
 (0)