ch03: update from book draft
This commit is contained in:
parent
aa868e8f75
commit
20dd93c11b
@ -1,4 +1,4 @@
|
|||||||
# BEGIN DIALCODES
|
# tag::DIALCODES[]
|
||||||
# dial codes of the top 10 most populous countries
|
# dial codes of the top 10 most populous countries
|
||||||
DIAL_CODES = [
|
DIAL_CODES = [
|
||||||
(86, 'China'),
|
(86, 'China'),
|
||||||
@ -20,11 +20,11 @@ print('d2:', d2.keys())
|
|||||||
d3 = dict(sorted(DIAL_CODES, key=lambda x:x[1])) # <3>
|
d3 = dict(sorted(DIAL_CODES, key=lambda x:x[1])) # <3>
|
||||||
print('d3:', d3.keys())
|
print('d3:', d3.keys())
|
||||||
assert d1 == d2 and d2 == d3 # <4>
|
assert d1 == d2 and d2 == d3 # <4>
|
||||||
# END DIALCODES
|
# end::DIALCODES[]
|
||||||
"""
|
"""
|
||||||
# BEGIN DIALCODES_OUTPUT
|
# tag::DIALCODES_OUTPUT[]
|
||||||
d1: dict_keys([880, 1, 86, 55, 7, 234, 91, 92, 62, 81])
|
d1: dict_keys([880, 1, 86, 55, 7, 234, 91, 92, 62, 81])
|
||||||
d2: dict_keys([880, 1, 91, 86, 81, 55, 234, 7, 92, 62])
|
d2: dict_keys([880, 1, 91, 86, 81, 55, 234, 7, 92, 62])
|
||||||
d3: dict_keys([880, 81, 1, 86, 55, 7, 234, 91, 92, 62])
|
d3: dict_keys([880, 81, 1, 86, 55, 7, 234, 91, 92, 62])
|
||||||
# END DIALCODES_OUTPUT
|
# end::DIALCODES_OUTPUT[]
|
||||||
"""
|
"""
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
# http://www.aleax.it/Python/accu04_Relearn_Python_alex.pdf
|
# http://www.aleax.it/Python/accu04_Relearn_Python_alex.pdf
|
||||||
# (slide 41) Ex: lines-by-word file index
|
# (slide 41) Ex: lines-by-word file index
|
||||||
|
|
||||||
# BEGIN INDEX
|
# tag::INDEX[]
|
||||||
"""Build an index mapping word -> list of occurrences"""
|
"""Build an index mapping word -> list of occurrences"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
@ -22,4 +22,4 @@ with open(sys.argv[1], encoding='utf-8') as fp:
|
|||||||
# print in alphabetical order
|
# print in alphabetical order
|
||||||
for word in sorted(index, key=str.upper):
|
for word in sorted(index, key=str.upper):
|
||||||
print(word, index[word])
|
print(word, index[word])
|
||||||
# END INDEX
|
# end::INDEX[]
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
# http://www.aleax.it/Python/accu04_Relearn_Python_alex.pdf
|
# http://www.aleax.it/Python/accu04_Relearn_Python_alex.pdf
|
||||||
# (slide 41) Ex: lines-by-word file index
|
# (slide 41) Ex: lines-by-word file index
|
||||||
|
|
||||||
# BEGIN INDEX0
|
# tag::INDEX0[]
|
||||||
"""Build an index mapping word -> list of occurrences"""
|
"""Build an index mapping word -> list of occurrences"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
@ -25,4 +25,4 @@ with open(sys.argv[1], encoding='utf-8') as fp:
|
|||||||
# print in alphabetical order
|
# print in alphabetical order
|
||||||
for word in sorted(index, key=str.upper): # <4>
|
for word in sorted(index, key=str.upper): # <4>
|
||||||
print(word, index[word])
|
print(word, index[word])
|
||||||
# END INDEX0
|
# end::INDEX0[]
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
# http://www.aleax.it/Python/accu04_Relearn_Python_alex.pdf
|
# http://www.aleax.it/Python/accu04_Relearn_Python_alex.pdf
|
||||||
# (slide 41) Ex: lines-by-word file index
|
# (slide 41) Ex: lines-by-word file index
|
||||||
|
|
||||||
# BEGIN INDEX_DEFAULT
|
# tag::INDEX_DEFAULT[]
|
||||||
"""Build an index mapping word -> list of occurrences"""
|
"""Build an index mapping word -> list of occurrences"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
@ -23,4 +23,4 @@ with open(sys.argv[1], encoding='utf-8') as fp:
|
|||||||
# print in alphabetical order
|
# print in alphabetical order
|
||||||
for word in sorted(index, key=str.upper):
|
for word in sorted(index, key=str.upper):
|
||||||
print(word, index[word])
|
print(word, index[word])
|
||||||
# END INDEX_DEFAULT
|
# end::INDEX_DEFAULT[]
|
||||||
|
@ -53,7 +53,7 @@ Tests for update using a `dict` or a sequence of pairs::
|
|||||||
TypeError: 'int' object is not iterable
|
TypeError: 'int' object is not iterable
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# BEGIN STRKEYDICT
|
# tag::STRKEYDICT[]
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
|
|
||||||
@ -71,4 +71,4 @@ class StrKeyDict(collections.UserDict): # <1>
|
|||||||
def __setitem__(self, key, item):
|
def __setitem__(self, key, item):
|
||||||
self.data[str(key)] = item # <4>
|
self.data[str(key)] = item # <4>
|
||||||
|
|
||||||
# END STRKEYDICT
|
# end::STRKEYDICT[]
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
"""StrKeyDict0 converts non-string keys to `str` on lookup
|
"""StrKeyDict0 converts non-string keys to `str` on lookup
|
||||||
|
|
||||||
# BEGIN STRKEYDICT0_TESTS
|
# tag::STRKEYDICT0_TESTS[]
|
||||||
|
|
||||||
Tests for item retrieval using `d[key]` notation::
|
Tests for item retrieval using `d[key]` notation::
|
||||||
|
|
||||||
@ -31,11 +31,11 @@ Tests for the `in` operator::
|
|||||||
>>> 1 in d
|
>>> 1 in d
|
||||||
False
|
False
|
||||||
|
|
||||||
# END STRKEYDICT0_TESTS
|
# end::STRKEYDICT0_TESTS[]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
# BEGIN STRKEYDICT0
|
# tag::STRKEYDICT0[]
|
||||||
class StrKeyDict0(dict): # <1>
|
class StrKeyDict0(dict): # <1>
|
||||||
|
|
||||||
def __missing__(self, key):
|
def __missing__(self, key):
|
||||||
@ -52,4 +52,4 @@ class StrKeyDict0(dict): # <1>
|
|||||||
def __contains__(self, key):
|
def __contains__(self, key):
|
||||||
return key in self.keys() or str(key) in self.keys() # <6>
|
return key in self.keys() or str(key) in self.keys() # <6>
|
||||||
|
|
||||||
# END STRKEYDICT0
|
# end::STRKEYDICT0[]
|
||||||
|
Loading…
Reference in New Issue
Block a user