From 17dac42139a0688f62108b88c216c6b48f5acba5 Mon Sep 17 00:00:00 2001 From: andela-cdike Date: Sun, 27 Aug 2017 20:30:49 +0100 Subject: [PATCH 01/11] Tests should use separate shelve test database - Switch from depreccated yield_fixture to fixture decorator --- 19-dyn-attr-prop/oscon/test_schedule1.py | 9 +++++++-- 19-dyn-attr-prop/oscon/test_schedule2.py | 7 +++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/19-dyn-attr-prop/oscon/test_schedule1.py b/19-dyn-attr-prop/oscon/test_schedule1.py index dbaacc9..ba5dfd1 100644 --- a/19-dyn-attr-prop/oscon/test_schedule1.py +++ b/19-dyn-attr-prop/oscon/test_schedule1.py @@ -1,15 +1,20 @@ +import os import shelve + import pytest import schedule1 as schedule +DB_NAME = 'data/test_db' + -@pytest.yield_fixture +@pytest.fixture(scope='module') def db(): - with shelve.open(schedule.DB_NAME) as the_db: + with shelve.open(DB_NAME) as the_db: if schedule.CONFERENCE not in the_db: schedule.load_db(the_db) yield the_db + os.remove(DB_NAME) def test_record_class(): diff --git a/19-dyn-attr-prop/oscon/test_schedule2.py b/19-dyn-attr-prop/oscon/test_schedule2.py index de09d32..ab1c79c 100644 --- a/19-dyn-attr-prop/oscon/test_schedule2.py +++ b/19-dyn-attr-prop/oscon/test_schedule2.py @@ -1,15 +1,18 @@ +import os import shelve + import pytest import schedule2 as schedule -@pytest.yield_fixture +@pytest.fixture(scope='module') def db(): - with shelve.open(schedule.DB_NAME) as the_db: + with shelve.open(DB_NAME) as the_db: if schedule.CONFERENCE not in the_db: schedule.load_db(the_db) yield the_db + os.remove(DB_NAME) def test_record_attr_access(): From 9fb76efc35b374835a5a61aaf7cdbb5bdba01ef9 Mon Sep 17 00:00:00 2001 From: anancds Date: Wed, 20 Mar 2019 22:05:34 +0800 Subject: [PATCH 02/11] fixed anomalous backslash in string --- 03-dict-set/index.py | 2 +- 03-dict-set/index0.py | 2 +- 03-dict-set/index_default.py | 2 +- 14-it-generator/sentence.py | 2 +- 14-it-generator/sentence_gen.py | 2 +- 14-it-generator/sentence_gen2.py | 2 +- 14-it-generator/sentence_genexp.py | 2 +- 14-it-generator/sentence_iter.py | 2 +- 14-it-generator/sentence_iter2.py | 2 +- 18-asyncio-py3.7/charfinder/charfinder.py | 4 ++-- 18-asyncio/charfinder/charfinder.py | 2 +- attic/concurrency/charfinder/charfinder.py | 4 ++-- attic/dicts/index_alex.py | 2 +- attic/sequences/sentence_slice.py | 4 ++-- 14 files changed, 17 insertions(+), 17 deletions(-) diff --git a/03-dict-set/index.py b/03-dict-set/index.py index 3eac1fb..f8641d2 100644 --- a/03-dict-set/index.py +++ b/03-dict-set/index.py @@ -8,7 +8,7 @@ import sys import re -WORD_RE = re.compile('\w+') +WORD_RE = re.compile(r'\w+') index = {} with open(sys.argv[1], encoding='utf-8') as fp: diff --git a/03-dict-set/index0.py b/03-dict-set/index0.py index e1fa28f..b41af0e 100644 --- a/03-dict-set/index0.py +++ b/03-dict-set/index0.py @@ -8,7 +8,7 @@ import sys import re -WORD_RE = re.compile('\w+') +WORD_RE = re.compile(r'\w+') index = {} with open(sys.argv[1], encoding='utf-8') as fp: diff --git a/03-dict-set/index_default.py b/03-dict-set/index_default.py index 521b2d5..8d3ae58 100644 --- a/03-dict-set/index_default.py +++ b/03-dict-set/index_default.py @@ -9,7 +9,7 @@ import re import collections -WORD_RE = re.compile('\w+') +WORD_RE = re.compile(r'\w+') index = collections.defaultdict(list) # <1> with open(sys.argv[1], encoding='utf-8') as fp: diff --git a/14-it-generator/sentence.py b/14-it-generator/sentence.py index fb866c4..6a20c15 100644 --- a/14-it-generator/sentence.py +++ b/14-it-generator/sentence.py @@ -5,7 +5,7 @@ import re import reprlib -RE_WORD = re.compile('\w+') +RE_WORD = re.compile(r'\w+') class Sentence: diff --git a/14-it-generator/sentence_gen.py b/14-it-generator/sentence_gen.py index a17c48f..32a8225 100644 --- a/14-it-generator/sentence_gen.py +++ b/14-it-generator/sentence_gen.py @@ -5,7 +5,7 @@ import re import reprlib -RE_WORD = re.compile('\w+') +RE_WORD = re.compile(r'\w+') class Sentence: diff --git a/14-it-generator/sentence_gen2.py b/14-it-generator/sentence_gen2.py index 8b0f355..b308100 100644 --- a/14-it-generator/sentence_gen2.py +++ b/14-it-generator/sentence_gen2.py @@ -5,7 +5,7 @@ import re import reprlib -RE_WORD = re.compile('\w+') +RE_WORD = re.compile(r'\w+') class Sentence: diff --git a/14-it-generator/sentence_genexp.py b/14-it-generator/sentence_genexp.py index 2919c29..52228de 100644 --- a/14-it-generator/sentence_genexp.py +++ b/14-it-generator/sentence_genexp.py @@ -6,7 +6,7 @@ import re import reprlib -RE_WORD = re.compile('\w+') +RE_WORD = re.compile(r'\w+') class Sentence: diff --git a/14-it-generator/sentence_iter.py b/14-it-generator/sentence_iter.py index 938d5b4..11b8179 100644 --- a/14-it-generator/sentence_iter.py +++ b/14-it-generator/sentence_iter.py @@ -9,7 +9,7 @@ import re import reprlib -RE_WORD = re.compile('\w+') +RE_WORD = re.compile(r'\w+') class Sentence: diff --git a/14-it-generator/sentence_iter2.py b/14-it-generator/sentence_iter2.py index 8597b32..2663f3f 100644 --- a/14-it-generator/sentence_iter2.py +++ b/14-it-generator/sentence_iter2.py @@ -8,7 +8,7 @@ import re import reprlib -RE_WORD = re.compile('\w+') +RE_WORD = re.compile(r'\w+') class Sentence: diff --git a/18-asyncio-py3.7/charfinder/charfinder.py b/18-asyncio-py3.7/charfinder/charfinder.py index 7e06792..64e4949 100755 --- a/18-asyncio-py3.7/charfinder/charfinder.py +++ b/18-asyncio-py3.7/charfinder/charfinder.py @@ -64,9 +64,9 @@ import functools from collections import namedtuple -RE_WORD = re.compile('\w+') +RE_WORD = re.compile(r'\w+') RE_UNICODE_NAME = re.compile('^[A-Z0-9 -]+$') -RE_CODEPOINT = re.compile('U\+([0-9A-F]{4,6})') +RE_CODEPOINT = re.compile(r'U\+([0-9A-F]{4,6})') INDEX_NAME = 'charfinder_index.pickle' MINIMUM_SAVE_LEN = 10000 diff --git a/18-asyncio/charfinder/charfinder.py b/18-asyncio/charfinder/charfinder.py index 7e06792..4e97a0a 100755 --- a/18-asyncio/charfinder/charfinder.py +++ b/18-asyncio/charfinder/charfinder.py @@ -64,7 +64,7 @@ import functools from collections import namedtuple -RE_WORD = re.compile('\w+') +RE_WORD = re.compile(r'\w+') RE_UNICODE_NAME = re.compile('^[A-Z0-9 -]+$') RE_CODEPOINT = re.compile('U\+([0-9A-F]{4,6})') diff --git a/attic/concurrency/charfinder/charfinder.py b/attic/concurrency/charfinder/charfinder.py index d73f60b..d18db89 100755 --- a/attic/concurrency/charfinder/charfinder.py +++ b/attic/concurrency/charfinder/charfinder.py @@ -63,9 +63,9 @@ import itertools from collections import namedtuple -RE_WORD = re.compile('\w+') +RE_WORD = re.compile(r'\w+') RE_UNICODE_NAME = re.compile('^[A-Z0-9 -]+$') -RE_CODEPOINT = re.compile('U\+([0-9A-F]{4,6})') +RE_CODEPOINT = re.compile(r'U\+([0-9A-F]{4,6})') INDEX_NAME = 'charfinder_index.pickle' MINIMUM_SAVE_LEN = 10000 diff --git a/attic/dicts/index_alex.py b/attic/dicts/index_alex.py index 27d7175..73db8c6 100644 --- a/attic/dicts/index_alex.py +++ b/attic/dicts/index_alex.py @@ -8,7 +8,7 @@ import sys import re -NONWORD_RE = re.compile('\W+') +NONWORD_RE = re.compile(r'\W+') idx = {} with open(sys.argv[1], encoding='utf-8') as fp: diff --git a/attic/sequences/sentence_slice.py b/attic/sequences/sentence_slice.py index d275989..918338d 100644 --- a/attic/sequences/sentence_slice.py +++ b/attic/sequences/sentence_slice.py @@ -6,9 +6,9 @@ import reprlib -RE_TOKEN = re.compile('\w+|\s+|[^\w\s]+') +RE_TOKEN = re.compile(r'\w+|\s+|[^\w\s]+') RE_WORD = re.compile('\w+') -RE_PUNCTUATION = re.compile('[^\w\s]+') +RE_PUNCTUATION = re.compile(r'[^\w\s]+') class SentenceSlice: From a58ecf6bdea847515a8b4871af56e3647849fe8f Mon Sep 17 00:00:00 2001 From: Luciano Ramalho Date: Thu, 16 May 2019 18:54:52 -0300 Subject: [PATCH 03/11] simplified spinner_* examples --- 18-asyncio-py3.7/spinner_asyncio.py | 8 ++------ 18-asyncio-py3.7/spinner_thread.py | 11 +++-------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/18-asyncio-py3.7/spinner_asyncio.py b/18-asyncio-py3.7/spinner_asyncio.py index adbd611..369a8f0 100755 --- a/18-asyncio-py3.7/spinner_asyncio.py +++ b/18-asyncio-py3.7/spinner_asyncio.py @@ -9,21 +9,17 @@ # BEGIN SPINNER_ASYNCIO import asyncio import itertools -import sys async def spin(msg): # <1> - write, flush = sys.stdout.write, sys.stdout.flush for char in itertools.cycle('|/-\\'): status = char + ' ' + msg - write(status) - flush() - write('\x08' * len(status)) + print(status, flush=True, end='\r') try: await asyncio.sleep(.1) # <2> except asyncio.CancelledError: # <3> break - write(' ' * len(status) + '\x08' * len(status)) + print(' ' * len(status), end='\r') async def slow_function(): # <4> diff --git a/18-asyncio-py3.7/spinner_thread.py b/18-asyncio-py3.7/spinner_thread.py index dffcca6..bffc921 100755 --- a/18-asyncio-py3.7/spinner_thread.py +++ b/18-asyncio-py3.7/spinner_thread.py @@ -10,20 +10,15 @@ import threading import itertools import time -import sys -def spin(msg, done): # <2> - write, flush = sys.stdout.write, sys.stdout.flush +def spin(msg, done): # <1> for char in itertools.cycle('|/-\\'): # <3> status = char + ' ' + msg - write(status) - flush() - write('\x08' * len(status)) # <4> + print(status, flush=True, end='\r') if done.wait(.1): # <5> break - write(' ' * len(status) + '\x08' * len(status)) # <6> - + print(' ' * len(status), end='\r') def slow_function(): # <7> # pretend waiting a long time for I/O From 7cd136bb680c410c4b9b3601168c19028b0faa92 Mon Sep 17 00:00:00 2001 From: Hunter Duan <308960474@qq.com> Date: Fri, 15 Nov 2019 16:49:18 +0800 Subject: [PATCH 04/11] fixed the param 'index' is redundant --- 14-it-generator/sentence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/14-it-generator/sentence.py b/14-it-generator/sentence.py index 6a20c15..447a192 100644 --- a/14-it-generator/sentence.py +++ b/14-it-generator/sentence.py @@ -17,7 +17,7 @@ def __init__(self, text): def __getitem__(self, index): return self.words[index] # <2> - def __len__(self, index): # <3> + def __len__(self): # <3> return len(self.words) def __repr__(self): From 93a9ce6407cd8042a8bce82eed5148c9c4eba4e5 Mon Sep 17 00:00:00 2001 From: Luciano Ramalho Date: Mon, 2 Mar 2020 15:14:05 -0300 Subject: [PATCH 05/11] updated tcp_charfinder.py to Python 3.8 asyncio API --- 18-asyncio-py3.7/charfinder/tcp_charfinder.py | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/18-asyncio-py3.7/charfinder/tcp_charfinder.py b/18-asyncio-py3.7/charfinder/tcp_charfinder.py index 86e27c9..4980b92 100755 --- a/18-asyncio-py3.7/charfinder/tcp_charfinder.py +++ b/18-asyncio-py3.7/charfinder/tcp_charfinder.py @@ -38,26 +38,17 @@ async def handle_queries(reader, writer): # <3> # END TCP_CHARFINDER_TOP # BEGIN TCP_CHARFINDER_MAIN -def main(address='127.0.0.1', port=2323): # <1> +async def main(address='127.0.0.1', port=2323): # <1> port = int(port) - loop = asyncio.get_event_loop() - server_coro = asyncio.start_server(handle_queries, address, port, - loop=loop) # <2> - server = loop.run_until_complete(server_coro) # <3> + server = await asyncio.start_server(handle_queries, address, port) # <2> - host = server.sockets[0].getsockname() # <4> - print('Serving on {}. Hit CTRL-C to stop.'.format(host)) # <5> - try: - loop.run_forever() # <6> - except KeyboardInterrupt: # CTRL+C pressed - pass + host = server.sockets[0].getsockname() # <3> + print('Serving on {}. Hit CTRL-C to stop.'.format(host)) # <4> - print('Server shutting down.') - server.close() # <7> - loop.run_until_complete(server.wait_closed()) # <8> - loop.close() # <9> + async with server: + await server.serve_forever() if __name__ == '__main__': - main(*sys.argv[1:]) # <10> + asyncio.run(main(*sys.argv[1:])) # <5> # END TCP_CHARFINDER_MAIN From ca6c957cfff5bc3682ee554617e108c7426cf5e9 Mon Sep 17 00:00:00 2001 From: Simon Ilincev Date: Wed, 26 Aug 2020 11:28:08 +0200 Subject: [PATCH 06/11] correct LookupError We are no longer inside of the BingoCage class, so we should change the LookupError's wording to reflect that this is an error within the new LotteryBlower class. --- 11-iface-abc/lotto.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/11-iface-abc/lotto.py b/11-iface-abc/lotto.py index 2295b71..da8c2de 100644 --- a/11-iface-abc/lotto.py +++ b/11-iface-abc/lotto.py @@ -17,7 +17,7 @@ def pick(self): try: position = random.randrange(len(self._balls)) # <2> except ValueError: - raise LookupError('pick from empty BingoCage') + raise LookupError('pick from empty LotteryBlower') return self._balls.pop(position) # <3> def loaded(self): # <4> From d920e38720d1002761ae9aa856438bbed4d2d30d Mon Sep 17 00:00:00 2001 From: Luciano Ramalho Date: Sun, 20 Dec 2020 14:12:06 -0300 Subject: [PATCH 07/11] Update README.rst --- README.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 99a0ede..fac11b4 100644 --- a/README.rst +++ b/README.rst @@ -1,9 +1,7 @@ Fluent Python: example code =========================== -Example code for the book `Fluent Python`_ by Luciano Ramalho (O'Reilly, 2014). - - **BEWARE**: This is a work in progress, like the book itself. +Example code for the book `Fluent Python, First Edition` by Luciano Ramalho (O'Reilly, 2015). * Code here may change and disappear without warning. From 58dbea0b927f5467d7f442541df32d65fa10d55d Mon Sep 17 00:00:00 2001 From: Luciano Ramalho Date: Sun, 20 Dec 2020 14:12:30 -0300 Subject: [PATCH 08/11] Update README.rst --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index fac11b4..af75b88 100644 --- a/README.rst +++ b/README.rst @@ -1,5 +1,5 @@ -Fluent Python: example code -=========================== +Fluent Python, First Edition: example code +========================================== Example code for the book `Fluent Python, First Edition` by Luciano Ramalho (O'Reilly, 2015). From 169d3c68c4012b7ebe36b79d73e9134a1a3f32de Mon Sep 17 00:00:00 2001 From: Tim Gates Date: Thu, 24 Dec 2020 16:20:29 +1100 Subject: [PATCH 09/11] docs: fix simple typo, shorcut -> shortcut There is a small typo in 18-asyncio/charfinder/charfinder.py, attic/concurrency/charfinder/charfinder.py. Should read `shortcut` rather than `shorcut`. --- 18-asyncio/charfinder/charfinder.py | 2 +- attic/concurrency/charfinder/charfinder.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/18-asyncio/charfinder/charfinder.py b/18-asyncio/charfinder/charfinder.py index 4e97a0a..c061f90 100755 --- a/18-asyncio/charfinder/charfinder.py +++ b/18-asyncio/charfinder/charfinder.py @@ -163,7 +163,7 @@ def find_chars(self, query, start=0, stop=None): result_sets = [] for word in tokenize(query): chars = self.index.get(word) - if chars is None: # shorcut: no such word + if chars is None: # shortcut: no such word result_sets = [] break result_sets.append(chars) diff --git a/attic/concurrency/charfinder/charfinder.py b/attic/concurrency/charfinder/charfinder.py index d18db89..72e21a4 100755 --- a/attic/concurrency/charfinder/charfinder.py +++ b/attic/concurrency/charfinder/charfinder.py @@ -165,7 +165,7 @@ def find_chars(self, query, start=0, stop=None): for word in tokenize(query): if word in self.index: result_sets.append(self.index[word]) - else: # shorcut: no such word + else: # shortcut: no such word result_sets = [] break if result_sets: From 7a2a652eab1febebf59ac9bca62c97081a997ee4 Mon Sep 17 00:00:00 2001 From: Luciano Ramalho Date: Sat, 19 Jun 2021 13:44:21 -0300 Subject: [PATCH 10/11] Update README.rst Fix suggested by @diraol --- 18-asyncio-py3.7/README.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/18-asyncio-py3.7/README.rst b/18-asyncio-py3.7/README.rst index 0f4f1b8..aad52a3 100644 --- a/18-asyncio-py3.7/README.rst +++ b/18-asyncio-py3.7/README.rst @@ -4,8 +4,8 @@ From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015) http://shop.oreilly.com/product/0636920032519.do ################################################################## -NOTE: this "18b" directory contains the examples of chapter 18 -rewritten using the new async/await syntax available in Python 3.5 -ONLY, instead of the "yield-from" syntax which works since Python -3.3 (and will still work with Python 3.5). +NOTE: this directory contains the examples of chapter 18 +rewritten using the new async/await syntax available from Python +3.5+, instead of the "yield-from" syntax of Python 3.3 and 3.4. +The code was tested with Python 3.7 ################################################################## From d5133ad6e4a48eac0980d2418ed39d7ff693edbe Mon Sep 17 00:00:00 2001 From: Luciano Ramalho Date: Thu, 2 Dec 2021 11:39:34 -0300 Subject: [PATCH 11/11] Update README.rst --- README.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.rst b/README.rst index af75b88..39b95c4 100644 --- a/README.rst +++ b/README.rst @@ -1,6 +1,8 @@ Fluent Python, First Edition: example code ========================================== +**This repository is archived and will not be updated. Please visit https://github.com/fluentpython/example-code-2e** + Example code for the book `Fluent Python, First Edition` by Luciano Ramalho (O'Reilly, 2015). * Code here may change and disappear without warning.