-
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
e2c3d73
commit 4ec8690
Showing
8 changed files
with
13,689 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ venv/ | |
*.swp | ||
.vscode/ | ||
.ipynb_checkpoints/ | ||
__pycache__/ |
File renamed without changes.
13,301 changes: 13,301 additions & 0 deletions
13,301
Part_6__Metaprogramming/Chap_19__Dynamic_Attributes_and_Properties/data/osconfeed.json
Large diffs are not rendered by default.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
Part_6__Metaprogramming/Chap_19__Dynamic_Attributes_and_Properties/ex_19_2__osconfeed.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,18 @@ | ||
from urllib.request import urlopen | ||
import warnings | ||
import os | ||
import json | ||
|
||
URL = 'http://www.oreilly.com/pub/sc/osconfeed' | ||
JSON = 'data/osconfeed.json' # reference: https://github.com/fluentpython/example-code-2e/blob/master/22-dyn-attr-prop/oscon/data/osconfeed.json | ||
|
||
|
||
def load(): | ||
if not os.path.exists(JSON): | ||
msg = 'downloading {} to {}'.format(URL, JSON) | ||
warnings.warn(msg) | ||
with urlopen(URL) as remote, open(JSON, 'wb') as local: | ||
local.write(remote.read()) | ||
|
||
with open(JSON) as fp: | ||
return json.load(fp) |
160 changes: 160 additions & 0 deletions
160
...aprogramming/Chap_19__Dynamic_Attributes_and_Properties/ex_19_3__osconfeed_doctests.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,160 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"['conferences', 'events', 'speakers', 'venues']" | ||
] | ||
}, | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from ex_19_2__osconfeed import load\n", | ||
"\n", | ||
"feed = load()\n", | ||
"sorted(feed['Schedule'].keys())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
" 1 conferences\n", | ||
"484 events\n", | ||
"357 speakers\n", | ||
" 53 venues\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"for key, value in sorted(feed['Schedule'].items()):\n", | ||
" print('{:3} {}'.format(len(value), key))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'Carina C. Zona'" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"feed['Schedule']['speakers'][-1]['name']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"141590" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"feed['Schedule']['speakers'][-1]['serial']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'There *Will* Be Bugs'" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"feed['Schedule']['events'][40]['name']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[3471, 5199]" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"feed['Schedule']['events'][40]['speakers']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "b1cad6e4136aaf5b8d341537b41e4e06441496f7176c964558b716a0d04682ee" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3.10.2 64-bit ('python-fluent')", | ||
"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.10.2" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
184 changes: 184 additions & 0 deletions
184
..._6__Metaprogramming/Chap_19__Dynamic_Attributes_and_Properties/ex_19_4__frozen_json.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,184 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"357" | ||
] | ||
}, | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from ex_19_2__osconfeed import load\n", | ||
"from ex_19_5__frozen_json import FrozenJSON\n", | ||
"\n", | ||
"raw_feed = load()\n", | ||
"feed = FrozenJSON(raw_feed)\n", | ||
"len(feed.Schedule.speakers)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"['conferences', 'events', 'speakers', 'venues']" | ||
] | ||
}, | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"sorted(feed.Schedule.keys())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'Carina C. Zona'" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"feed.Schedule.speakers[-1].name" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"ex_19_5__frozen_json.FrozenJSON" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"talk = feed.Schedule.events[40]\n", | ||
"type(talk)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'There *Will* Be Bugs'" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"talk.name" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[3471, 5199]" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"talk.speakers" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "KeyError", | ||
"evalue": "'flavor'", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | ||
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", | ||
"\u001b[1;32m/home/lucas/codes/python-fluent/Part_6__Metaprogramming/Chap_19__Dynamic_Attributes_and_Properties/ex_19_4__frozen_json.ipynb Cell 7'\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/home/lucas/codes/python-fluent/Part_6__Metaprogramming/Chap_19__Dynamic_Attributes_and_Properties/ex_19_4__frozen_json.ipynb#ch0000006?line=0'>1</a>\u001b[0m talk\u001b[39m.\u001b[39;49mflavor\n", | ||
"File \u001b[0;32m~/codes/python-fluent/Part_6__Metaprogramming/Chap_19__Dynamic_Attributes_and_Properties/ex_19_5__frozen_json.py:15\u001b[0m, in \u001b[0;36mFrozenJSON.__getattr__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m <a href='file:///home/lucas/codes/python-fluent/Part_6__Metaprogramming/Chap_19__Dynamic_Attributes_and_Properties/ex_19_5__frozen_json.py?line=12'>13</a>\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mgetattr\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m__data, name)\n\u001b[1;32m <a href='file:///home/lucas/codes/python-fluent/Part_6__Metaprogramming/Chap_19__Dynamic_Attributes_and_Properties/ex_19_5__frozen_json.py?line=13'>14</a>\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m---> <a href='file:///home/lucas/codes/python-fluent/Part_6__Metaprogramming/Chap_19__Dynamic_Attributes_and_Properties/ex_19_5__frozen_json.py?line=14'>15</a>\u001b[0m \u001b[39mreturn\u001b[39;00m FrozenJSON\u001b[39m.\u001b[39mbuild(\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m__data[name])\n", | ||
"\u001b[0;31mKeyError\u001b[0m: 'flavor'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"talk.flavor" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "b1cad6e4136aaf5b8d341537b41e4e06441496f7176c964558b716a0d04682ee" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3.10.2 64-bit ('python-fluent')", | ||
"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.10.2" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
25 changes: 25 additions & 0 deletions
25
Part_6__Metaprogramming/Chap_19__Dynamic_Attributes_and_Properties/ex_19_5__frozen_json.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,25 @@ | ||
from collections import abc | ||
|
||
|
||
class FrozenJSON: | ||
"""A read-only facade for navigating a JSON-like object | ||
using attribute notation | ||
""" | ||
|
||
def __init__(self, mapping): | ||
self.__data = dict(mapping) | ||
|
||
def __getattr__(self, name): | ||
if hasattr(self.__data, name): | ||
return getattr(self.__data, name) | ||
else: | ||
return FrozenJSON.build(self.__data[name]) | ||
|
||
@classmethod | ||
def build(cls, obj): | ||
if isinstance(obj, abc.Mapping): | ||
return cls(obj) | ||
elif isinstance(obj, abc.MutableSequence): | ||
return [cls.build(item) for item in obj] | ||
else: | ||
return obj |
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
read - 320 | ||
write - 404 |