From 20dd93c11b2287d20b433721c8b70b872e658562 Mon Sep 17 00:00:00 2001 From: Luciano Ramalho Date: Tue, 18 Feb 2020 23:58:03 -0300 Subject: [PATCH] ch03: update from book draft --- 03-dict-set/dialcodes.py | 8 ++++---- 03-dict-set/index.py | 4 ++-- 03-dict-set/index0.py | 4 ++-- 03-dict-set/index_default.py | 4 ++-- 03-dict-set/strkeydict.py | 4 ++-- 03-dict-set/strkeydict0.py | 8 ++++---- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/03-dict-set/dialcodes.py b/03-dict-set/dialcodes.py index d559d79..07b06a7 100644 --- a/03-dict-set/dialcodes.py +++ b/03-dict-set/dialcodes.py @@ -1,4 +1,4 @@ -# BEGIN DIALCODES +# tag::DIALCODES[] # dial codes of the top 10 most populous countries DIAL_CODES = [ (86, 'China'), @@ -20,11 +20,11 @@ print('d2:', d2.keys()) d3 = dict(sorted(DIAL_CODES, key=lambda x:x[1])) # <3> print('d3:', d3.keys()) 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]) 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]) -# END DIALCODES_OUTPUT +# end::DIALCODES_OUTPUT[] """ diff --git a/03-dict-set/index.py b/03-dict-set/index.py index f8641d2..5da48b1 100644 --- a/03-dict-set/index.py +++ b/03-dict-set/index.py @@ -2,7 +2,7 @@ # http://www.aleax.it/Python/accu04_Relearn_Python_alex.pdf # (slide 41) Ex: lines-by-word file index -# BEGIN INDEX +# tag::INDEX[] """Build an index mapping word -> list of occurrences""" import sys @@ -22,4 +22,4 @@ with open(sys.argv[1], encoding='utf-8') as fp: # print in alphabetical order for word in sorted(index, key=str.upper): print(word, index[word]) -# END INDEX +# end::INDEX[] diff --git a/03-dict-set/index0.py b/03-dict-set/index0.py index b41af0e..8c49915 100644 --- a/03-dict-set/index0.py +++ b/03-dict-set/index0.py @@ -2,7 +2,7 @@ # http://www.aleax.it/Python/accu04_Relearn_Python_alex.pdf # (slide 41) Ex: lines-by-word file index -# BEGIN INDEX0 +# tag::INDEX0[] """Build an index mapping word -> list of occurrences""" import sys @@ -25,4 +25,4 @@ with open(sys.argv[1], encoding='utf-8') as fp: # print in alphabetical order for word in sorted(index, key=str.upper): # <4> print(word, index[word]) -# END INDEX0 +# end::INDEX0[] diff --git a/03-dict-set/index_default.py b/03-dict-set/index_default.py index 8d3ae58..60279a9 100644 --- a/03-dict-set/index_default.py +++ b/03-dict-set/index_default.py @@ -2,7 +2,7 @@ # http://www.aleax.it/Python/accu04_Relearn_Python_alex.pdf # (slide 41) Ex: lines-by-word file index -# BEGIN INDEX_DEFAULT +# tag::INDEX_DEFAULT[] """Build an index mapping word -> list of occurrences""" import sys @@ -23,4 +23,4 @@ with open(sys.argv[1], encoding='utf-8') as fp: # print in alphabetical order for word in sorted(index, key=str.upper): print(word, index[word]) -# END INDEX_DEFAULT +# end::INDEX_DEFAULT[] diff --git a/03-dict-set/strkeydict.py b/03-dict-set/strkeydict.py index b13ea1f..ba82133 100644 --- a/03-dict-set/strkeydict.py +++ b/03-dict-set/strkeydict.py @@ -53,7 +53,7 @@ Tests for update using a `dict` or a sequence of pairs:: TypeError: 'int' object is not iterable """ -# BEGIN STRKEYDICT +# tag::STRKEYDICT[] import collections @@ -71,4 +71,4 @@ class StrKeyDict(collections.UserDict): # <1> def __setitem__(self, key, item): self.data[str(key)] = item # <4> -# END STRKEYDICT +# end::STRKEYDICT[] diff --git a/03-dict-set/strkeydict0.py b/03-dict-set/strkeydict0.py index 75df4c7..7062b5e 100644 --- a/03-dict-set/strkeydict0.py +++ b/03-dict-set/strkeydict0.py @@ -1,6 +1,6 @@ """StrKeyDict0 converts non-string keys to `str` on lookup -# BEGIN STRKEYDICT0_TESTS +# tag::STRKEYDICT0_TESTS[] Tests for item retrieval using `d[key]` notation:: @@ -31,11 +31,11 @@ Tests for the `in` operator:: >>> 1 in d False -# END STRKEYDICT0_TESTS +# end::STRKEYDICT0_TESTS[] """ -# BEGIN STRKEYDICT0 +# tag::STRKEYDICT0[] class StrKeyDict0(dict): # <1> def __missing__(self, key): @@ -52,4 +52,4 @@ class StrKeyDict0(dict): # <1> def __contains__(self, key): return key in self.keys() or str(key) in self.keys() # <6> -# END STRKEYDICT0 +# end::STRKEYDICT0[]