Skip to content

Commit

Permalink
pg.191
Browse files Browse the repository at this point in the history
  • Loading branch information
CavalcanteLucas committed Nov 16, 2021
1 parent b1b0950 commit 2356559
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 16 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-fluent
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -11,7 +11,7 @@
"<Order total: 42.00 due: 39.90>"
]
},
"execution_count": 1,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -37,7 +37,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand All @@ -46,7 +46,7 @@
"<Order total: 42.00 due: 39.90>"
]
},
"execution_count": 2,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -57,7 +57,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -66,7 +66,7 @@
"<Order total: 30.00 due: 28.50>"
]
},
"execution_count": 3,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -81,19 +81,18 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'large_order_promo' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\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",
"\u001b[0;31mNameError\u001b[0m: name 'large_order_promo' is not defined"
]
"data": {
"text/plain": [
"<Order total: 10.00 due: 9.30>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
Expand All @@ -104,6 +103,26 @@
"Order(joe, long_order, large_order_promo)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Order total: 42.00 due: 42.00>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Order(joe, cart, large_order_promo)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
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
}
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
}
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
}
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.

0 comments on commit 2356559

Please sign in to comment.