-
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
7543422
commit f89bc1a
Showing
7 changed files
with
360 additions
and
0 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,3 @@ | ||
{ | ||
"python.formatting.provider": "black" | ||
} |
4 changes: 4 additions & 0 deletions
4
..._as_Objects/Chap_5-First_Class_Functions/ex_5_21__factorial_with_reduced_and_anonymous.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,4 @@ | ||
from functools import reduce | ||
|
||
def fact(n): | ||
return reduce(lambda a, b: a*b, range(1, n+1)) |
5 changes: 5 additions & 0 deletions
5
...s_as_Objects/Chap_5-First_Class_Functions/ex_5_22__factorial_with_reduced_and_operator.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,5 @@ | ||
from functools import reduce | ||
from operator import mul | ||
|
||
def fact(n): | ||
return reduce(mul, range(1, n+1)) |
7 changes: 7 additions & 0 deletions
7
Part_3-Fuctions_as_Objects/Chap_5-First_Class_Functions/ex_5_23a__metro_data.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,7 @@ | ||
metro_data = [ | ||
('Tokyo', 'JP', 36.933, (35.689722, 139.691667)), | ||
('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)), | ||
('Mexico City', 'MX', 20.142, (19.433333, -99.133333)), | ||
('New York-Newark', 'US', 20.104, (40.808611, -74.020386)), | ||
('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833)), | ||
] |
82 changes: 82 additions & 0 deletions
82
Part_3-Fuctions_as_Objects/Chap_5-First_Class_Functions/ex_5_23b__demo_of_itemgetter.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,82 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"source": [ | ||
"from operator import itemgetter\n", | ||
"from ex_5_23a__metro_data import metro_data\n", | ||
"for city in sorted(metro_data, key=itemgetter(1)):\n", | ||
" print(city)" | ||
], | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833))\n", | ||
"('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889))\n", | ||
"('Tokyo', 'JP', 36.933, (35.689722, 139.691667))\n", | ||
"('Mexico City', 'MX', 20.142, (19.433333, -99.133333))\n", | ||
"('New York-Newark', 'US', 20.104, (40.808611, -74.020386))\n" | ||
] | ||
} | ||
], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"source": [ | ||
"cc_name = itemgetter(1, 0)\n", | ||
"for city in metro_data:\n", | ||
" print(cc_name(city))" | ||
], | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"('JP', 'Tokyo')\n", | ||
"('IN', 'Delhi NCR')\n", | ||
"('MX', 'Mexico City')\n", | ||
"('US', 'New York-Newark')\n", | ||
"('BR', 'Sao Paulo')\n" | ||
] | ||
} | ||
], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"source": [], | ||
"outputs": [], | ||
"metadata": {} | ||
} | ||
], | ||
"metadata": { | ||
"orig_nbformat": 4, | ||
"language_info": { | ||
"name": "python", | ||
"version": "3.8.10", | ||
"mimetype": "text/x-python", | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"pygments_lexer": "ipython3", | ||
"nbconvert_exporter": "python", | ||
"file_extension": ".py" | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3.8.10 64-bit ('python-fluent': pyenv)" | ||
}, | ||
"interpreter": { | ||
"hash": "37f590f6de04d9a834de345867b5912e86fd172250c2636cb0473472171768e6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
180 changes: 180 additions & 0 deletions
180
...-Fuctions_as_Objects/Chap_5-First_Class_Functions/ex_5_24__attrgetter_on_namedtuple.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,180 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"source": [ | ||
"from collections import namedtuple\n", | ||
"from ex_5_23a__metro_data import metro_data\n", | ||
"LatLong = namedtuple('LatLong', 'lat long')\n", | ||
"Metropolis = namedtuple('Metropolis', 'name cc pop coord')\n", | ||
"metro_areas = [Metropolis(name, cc, pop, LatLong(lat, long))\n", | ||
" for name, cc, pop, (lat, long) in metro_data]\n", | ||
"metro_areas[0]" | ||
], | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"Metropolis(name='Tokyo', cc='JP', pop=36.933, coord=LatLong(lat=35.689722, long=139.691667))" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 3 | ||
} | ||
], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"source": [ | ||
"metro_areas[0].coord.lat" | ||
], | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"35.689722" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 4 | ||
} | ||
], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"source": [ | ||
"from operator import attrgetter\n", | ||
"name_lat = attrgetter('name', 'coord.lat')\n", | ||
"\n", | ||
"for city in sorted(metro_areas, key=attrgetter('coord.lat')):\n", | ||
" print(name_lat(city))" | ||
], | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"('Sao Paulo', -23.547778)\n", | ||
"('Mexico City', 19.433333)\n", | ||
"('Delhi NCR', 28.613889)\n", | ||
"('Tokyo', 35.689722)\n", | ||
"('New York-Newark', 40.808611)\n" | ||
] | ||
} | ||
], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"source": [ | ||
"import operator\n", | ||
"[name for name in dir(operator) if not name.startswith('_')]" | ||
], | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"['abs',\n", | ||
" 'add',\n", | ||
" 'and_',\n", | ||
" 'attrgetter',\n", | ||
" 'concat',\n", | ||
" 'contains',\n", | ||
" 'countOf',\n", | ||
" 'delitem',\n", | ||
" 'eq',\n", | ||
" 'floordiv',\n", | ||
" 'ge',\n", | ||
" 'getitem',\n", | ||
" 'gt',\n", | ||
" 'iadd',\n", | ||
" 'iand',\n", | ||
" 'iconcat',\n", | ||
" 'ifloordiv',\n", | ||
" 'ilshift',\n", | ||
" 'imatmul',\n", | ||
" 'imod',\n", | ||
" 'imul',\n", | ||
" 'index',\n", | ||
" 'indexOf',\n", | ||
" 'inv',\n", | ||
" 'invert',\n", | ||
" 'ior',\n", | ||
" 'ipow',\n", | ||
" 'irshift',\n", | ||
" 'is_',\n", | ||
" 'is_not',\n", | ||
" 'isub',\n", | ||
" 'itemgetter',\n", | ||
" 'itruediv',\n", | ||
" 'ixor',\n", | ||
" 'le',\n", | ||
" 'length_hint',\n", | ||
" 'lshift',\n", | ||
" 'lt',\n", | ||
" 'matmul',\n", | ||
" 'methodcaller',\n", | ||
" 'mod',\n", | ||
" 'mul',\n", | ||
" 'ne',\n", | ||
" 'neg',\n", | ||
" 'not_',\n", | ||
" 'or_',\n", | ||
" 'pos',\n", | ||
" 'pow',\n", | ||
" 'rshift',\n", | ||
" 'setitem',\n", | ||
" 'sub',\n", | ||
" 'truediv',\n", | ||
" 'truth',\n", | ||
" 'xor']" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 8 | ||
} | ||
], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"source": [], | ||
"outputs": [], | ||
"metadata": {} | ||
} | ||
], | ||
"metadata": { | ||
"orig_nbformat": 4, | ||
"language_info": { | ||
"name": "python", | ||
"version": "3.8.10", | ||
"mimetype": "text/x-python", | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"pygments_lexer": "ipython3", | ||
"nbconvert_exporter": "python", | ||
"file_extension": ".py" | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3.8.10 64-bit ('python-fluent': pyenv)" | ||
}, | ||
"interpreter": { | ||
"hash": "37f590f6de04d9a834de345867b5912e86fd172250c2636cb0473472171768e6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
79 changes: 79 additions & 0 deletions
79
Part_3-Fuctions_as_Objects/Chap_5-First_Class_Functions/ex_5_25__demo_of_methodcaller.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,79 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"source": [ | ||
"from operator import methodcaller\n", | ||
"s = 'The time has come'\n", | ||
"upcase = methodcaller('upper')\n", | ||
"upcase(s)" | ||
], | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"'THE TIME HAS COME'" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 1 | ||
} | ||
], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"source": [ | ||
"hiphanate = methodcaller('replace', ' ', '-')\n", | ||
"hiphanate(s)" | ||
], | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"'The-time-has-come'" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 2 | ||
} | ||
], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"source": [], | ||
"outputs": [], | ||
"metadata": {} | ||
} | ||
], | ||
"metadata": { | ||
"orig_nbformat": 4, | ||
"language_info": { | ||
"name": "python", | ||
"version": "3.8.10", | ||
"mimetype": "text/x-python", | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"pygments_lexer": "ipython3", | ||
"nbconvert_exporter": "python", | ||
"file_extension": ".py" | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3.8.10 64-bit ('python-fluent': pyenv)" | ||
}, | ||
"interpreter": { | ||
"hash": "37f590f6de04d9a834de345867b5912e86fd172250c2636cb0473472171768e6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |