refactoring metaprogramming examples

This commit is contained in:
Luciano Ramalho
2015-01-01 12:11:47 -02:00
parent 8e2b8d90e5
commit 1edb0cd05b
11 changed files with 61 additions and 64 deletions

View File

@@ -1,7 +1,8 @@
"""
explore2.py: Script to download and explore the OSCON schedule feed
explore2.py: Script to explore the OSCON schedule feed
>>> raw_feed = load_json()
>>> from osconfeed import load
>>> raw_feed = load()
>>> feed = FrozenJSON(raw_feed)
>>> sorted(feed.Schedule.keys())
['conferences', 'events', 'speakers', 'venues']
@@ -24,26 +25,8 @@ explore2.py: Script to download and explore the OSCON schedule feed
"""
from urllib.request import urlopen
import warnings
import os
import json
from collections import abc
URL = 'http://www.oreilly.com/pub/sc/osconfeed'
JSON_NAME = 'osconfeed.json'
def load_json():
if not os.path.exists(JSON_NAME):
msg = 'downloading {} to {}'.format(URL, JSON_NAME)
warnings.warn(msg)
with urlopen(URL) as remote, open(JSON_NAME, 'wb') as local:
local.write(remote.read())
with open(JSON_NAME) as fp:
return json.load(fp)
class FrozenJSON:
"""A read-only façade for navigating a JSON-like object