-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1b0950
commit 2356559
Showing
7 changed files
with
310 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python-fluent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
...ns_as_Objects/Chap_6-Design_Patterns_With_First_Class_Funcions/ex_6_5,6__best_promo.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<Order total: 10.00 due: 9.30>" | ||
] | ||
}, | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from ex_6_3__functional_strategy import (\n", | ||
" Customer,\n", | ||
" LineItem,\n", | ||
" Order,\n", | ||
" fidelity_promo,\n", | ||
" bulk_item_promo,\n", | ||
" large_order_promo,\n", | ||
")\n", | ||
"\n", | ||
"joe = Customer('John Doe', 0)\n", | ||
"ann = Customer('Ann Smith', 1100)\n", | ||
"\n", | ||
"promos = [fidelity_promo, bulk_item_promo, large_order_promo]\n", | ||
"\n", | ||
"def best_promo(order):\n", | ||
" \"\"\"Select best discount available\n", | ||
" \"\"\"\n", | ||
" return max(promo(order) for promo in promos)\n", | ||
"\n", | ||
"long_order = [\n", | ||
" LineItem(str(item_code), 1, 1.0)\n", | ||
" for item_code in range(10)\n", | ||
"]\n", | ||
"\n", | ||
"banana_cart = [\n", | ||
" LineItem('banana', 30, .5),\n", | ||
" LineItem('apple', 10, 1.5),\n", | ||
"]\n", | ||
"\n", | ||
"cart = [\n", | ||
" LineItem('banana', 4, .5),\n", | ||
" LineItem('apple', 10, 1.5),\n", | ||
" LineItem('watermellon', 5, 5.0),\n", | ||
"]\n", | ||
"\n", | ||
"Order(joe, long_order, best_promo)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<Order total: 30.00 due: 28.50>" | ||
] | ||
}, | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"Order(joe, banana_cart, best_promo)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<Order total: 42.00 due: 39.90>" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"Order(ann, cart, best_promo)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "91801c9ce6957ebd0e9246650a12ff0ec9065db97224ddc8ea854cc44b68487c" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3.8.10 64-bit ('python-fluent': pyenv)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.10" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
67 changes: 67 additions & 0 deletions
67
...bjects/Chap_6-Design_Patterns_With_First_Class_Funcions/ex_6_7__promos_with_globals.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[<function ex_6_3__functional_strategy.fidelity_promo(order)>,\n", | ||
" <function ex_6_3__functional_strategy.bulk_item_promo(order)>,\n", | ||
" <function ex_6_3__functional_strategy.large_order_promo(order)>]" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"promos = [globals()[name] for name in globals()\n", | ||
" if name.endswith('_promo')\n", | ||
" and name != 'best_promo']\n", | ||
"\n", | ||
"def best_promo(order):\n", | ||
" \"\"\"Select best discount available\n", | ||
" \"\"\"\n", | ||
" return max(promo(order) for promo in promos)\n", | ||
"\n", | ||
"promos" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "91801c9ce6957ebd0e9246650a12ff0ec9065db97224ddc8ea854cc44b68487c" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3.8.10 64-bit ('python-fluent': pyenv)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.10" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
67 changes: 67 additions & 0 deletions
67
.../Chap_6-Design_Patterns_With_First_Class_Funcions/ex_6_8__promos_with_introspection.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[<function ex_6_3__functional_strategy.bulk_item_promo(order)>,\n", | ||
" <function ex_6_3__functional_strategy.fidelity_promo(order)>,\n", | ||
" <function ex_6_3__functional_strategy.large_order_promo(order)>,\n", | ||
" <function collections.namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)>]" | ||
] | ||
}, | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"import inspect\n", | ||
"import ex_6_3__functional_strategy\n", | ||
"\n", | ||
"promos = [\n", | ||
" func for name, func in\n", | ||
" inspect.getmembers(ex_6_3__functional_strategy, inspect.isfunction)\n", | ||
"]\n", | ||
"\n", | ||
"promos" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "91801c9ce6957ebd0e9246650a12ff0ec9065db97224ddc8ea854cc44b68487c" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3.8.10 64-bit ('python-fluent': pyenv)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.10" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
9 changes: 9 additions & 0 deletions
9
...tions_as_Objects/Chap_6-Design_Patterns_With_First_Class_Funcions/ex_6_9__MacroCommand.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class MacroCommand: | ||
"""A command that executes a list of commands""" | ||
|
||
def __init__(self, commands): | ||
self.commands = list(commands) | ||
|
||
def __call__(self): | ||
for command in self.commands: | ||
command() |
Empty file.