From 29d6835f3b06ed9e125cb752f8ef2d407c2afb3f Mon Sep 17 00:00:00 2001
From: Luciano Ramalho
Date: Wed, 23 Jan 2019 15:04:03 -0200
Subject: [PATCH] updated some examples from ch. 17 e 18 to Python 3.7
---
.../{flags_asyncio.py => asyncio_flags.py} | 0
...lags_threadpool.py => threadpool_flags.py} | 0
.../README.rst | 0
.../charfinder/.gitignore | 0
.../charfinder/charfinder.py | 0
.../charfinder/tcp_charfinder.py | 0
.../charfinder/test_charfinder.py | 0
.../countries/README.rst | 0
.../spinner_asyncio.py | 6 +-
.../spinner_curio.py | 0
.../spinner_thread.py | 0
.../charfinder/http_charfinder.html | 19 -----
18b-async-await/charfinder/http_charfinder.py | 71 -------------------
18b-async-await/spinner_asyncio.py | 53 --------------
14 files changed, 2 insertions(+), 147 deletions(-)
rename 17-futures-py3.7/countries/{flags_asyncio.py => asyncio_flags.py} (100%)
rename 17-futures-py3.7/countries/{flags_threadpool.py => threadpool_flags.py} (100%)
rename {18b-async-await => 18-asyncio-py3.7}/README.rst (100%)
rename {18b-async-await => 18-asyncio-py3.7}/charfinder/.gitignore (100%)
rename {18b-async-await => 18-asyncio-py3.7}/charfinder/charfinder.py (100%)
rename {18b-async-await => 18-asyncio-py3.7}/charfinder/tcp_charfinder.py (100%)
rename {18b-async-await => 18-asyncio-py3.7}/charfinder/test_charfinder.py (100%)
rename {18b-async-await => 18-asyncio-py3.7}/countries/README.rst (100%)
rename 18b-async-await/spinner_await.py => 18-asyncio-py3.7/spinner_asyncio.py (85%)
rename {18b-async-await => 18-asyncio-py3.7}/spinner_curio.py (100%)
rename {18b-async-await => 18-asyncio-py3.7}/spinner_thread.py (100%)
delete mode 100644 18b-async-await/charfinder/http_charfinder.html
delete mode 100755 18b-async-await/charfinder/http_charfinder.py
delete mode 100644 18b-async-await/spinner_asyncio.py
diff --git a/17-futures-py3.7/countries/flags_asyncio.py b/17-futures-py3.7/countries/asyncio_flags.py
similarity index 100%
rename from 17-futures-py3.7/countries/flags_asyncio.py
rename to 17-futures-py3.7/countries/asyncio_flags.py
diff --git a/17-futures-py3.7/countries/flags_threadpool.py b/17-futures-py3.7/countries/threadpool_flags.py
similarity index 100%
rename from 17-futures-py3.7/countries/flags_threadpool.py
rename to 17-futures-py3.7/countries/threadpool_flags.py
diff --git a/18b-async-await/README.rst b/18-asyncio-py3.7/README.rst
similarity index 100%
rename from 18b-async-await/README.rst
rename to 18-asyncio-py3.7/README.rst
diff --git a/18b-async-await/charfinder/.gitignore b/18-asyncio-py3.7/charfinder/.gitignore
similarity index 100%
rename from 18b-async-await/charfinder/.gitignore
rename to 18-asyncio-py3.7/charfinder/.gitignore
diff --git a/18b-async-await/charfinder/charfinder.py b/18-asyncio-py3.7/charfinder/charfinder.py
similarity index 100%
rename from 18b-async-await/charfinder/charfinder.py
rename to 18-asyncio-py3.7/charfinder/charfinder.py
diff --git a/18b-async-await/charfinder/tcp_charfinder.py b/18-asyncio-py3.7/charfinder/tcp_charfinder.py
similarity index 100%
rename from 18b-async-await/charfinder/tcp_charfinder.py
rename to 18-asyncio-py3.7/charfinder/tcp_charfinder.py
diff --git a/18b-async-await/charfinder/test_charfinder.py b/18-asyncio-py3.7/charfinder/test_charfinder.py
similarity index 100%
rename from 18b-async-await/charfinder/test_charfinder.py
rename to 18-asyncio-py3.7/charfinder/test_charfinder.py
diff --git a/18b-async-await/countries/README.rst b/18-asyncio-py3.7/countries/README.rst
similarity index 100%
rename from 18b-async-await/countries/README.rst
rename to 18-asyncio-py3.7/countries/README.rst
diff --git a/18b-async-await/spinner_await.py b/18-asyncio-py3.7/spinner_asyncio.py
similarity index 85%
rename from 18b-async-await/spinner_await.py
rename to 18-asyncio-py3.7/spinner_asyncio.py
index f9a3dfc..b9fc9d5 100644
--- a/18b-async-await/spinner_await.py
+++ b/18-asyncio-py3.7/spinner_asyncio.py
@@ -31,7 +31,7 @@ async def slow_function(): # <5>
async def supervisor(): # <7>
- spinner = asyncio.ensure_future(spin('thinking!')) # <8>
+ spinner = asyncio.create_task(spin('thinking!')) # <8>
print('spinner object:', spinner) # <9>
result = await slow_function() # <10>
spinner.cancel() # <11>
@@ -39,9 +39,7 @@ async def supervisor(): # <7>
def main():
- loop = asyncio.get_event_loop() # <12>
- result = loop.run_until_complete(supervisor()) # <13>
- loop.close()
+ result = asyncio.run(supervisor()) # <12>
print('Answer:', result)
diff --git a/18b-async-await/spinner_curio.py b/18-asyncio-py3.7/spinner_curio.py
similarity index 100%
rename from 18b-async-await/spinner_curio.py
rename to 18-asyncio-py3.7/spinner_curio.py
diff --git a/18b-async-await/spinner_thread.py b/18-asyncio-py3.7/spinner_thread.py
similarity index 100%
rename from 18b-async-await/spinner_thread.py
rename to 18-asyncio-py3.7/spinner_thread.py
diff --git a/18b-async-await/charfinder/http_charfinder.html b/18b-async-await/charfinder/http_charfinder.html
deleted file mode 100644
index 43b9cd7..0000000
--- a/18b-async-await/charfinder/http_charfinder.html
+++ /dev/null
@@ -1,19 +0,0 @@
-
-
-
-
- Charfinder
-
-
- Examples: {links}
-
-
-
-
-
-
diff --git a/18b-async-await/charfinder/http_charfinder.py b/18b-async-await/charfinder/http_charfinder.py
deleted file mode 100755
index ace15c5..0000000
--- a/18b-async-await/charfinder/http_charfinder.py
+++ /dev/null
@@ -1,71 +0,0 @@
-#!/usr/bin/env python3
-
-import sys
-import asyncio
-from aiohttp import web
-
-from charfinder import UnicodeNameIndex
-
-TEMPLATE_NAME = 'http_charfinder.html'
-CONTENT_TYPE = 'text/html; charset=UTF-8'
-SAMPLE_WORDS = ('bismillah chess cat circled Malayalam digit'
- ' Roman face Ethiopic black mark symbol dot'
- ' operator Braille hexagram').split()
-
-ROW_TPL = '{code_str} | {char} | {name} |
'
-LINK_TPL = '{0}'
-LINKS_HTML = ', '.join(LINK_TPL.format(word) for word in
- sorted(SAMPLE_WORDS, key=str.upper))
-
-
-index = UnicodeNameIndex()
-with open(TEMPLATE_NAME) as tpl:
- template = tpl.read()
-template = template.replace('{links}', LINKS_HTML)
-
-# BEGIN HTTP_CHARFINDER_HOME
-def home(request): # <1>
- query = request.GET.get('query', '').strip() # <2>
- print('Query: {!r}'.format(query)) # <3>
- if query: # <4>
- descriptions = list(index.find_descriptions(query))
- res = '\n'.join(ROW_TPL.format(**vars(descr))
- for descr in descriptions)
- msg = index.status(query, len(descriptions))
- else:
- descriptions = []
- res = ''
- msg = 'Enter words describing characters.'
-
- html = template.format(query=query, result=res, # <5>
- message=msg)
- print('Sending {} results'.format(len(descriptions))) # <6>
- return web.Response(content_type=CONTENT_TYPE, text=html) # <7>
-# END HTTP_CHARFINDER_HOME
-
-
-# BEGIN HTTP_CHARFINDER_SETUP
-async def init(loop, address, port): # <1>
- app = web.Application(loop=loop) # <2>
- app.router.add_route('GET', '/', home) # <3>
- handler = app.make_handler() # <4>
- server = await loop.create_server(handler,
- address, port) # <5>
- return server.sockets[0].getsockname() # <6>
-
-def main(address="127.0.0.1", port=8888):
- port = int(port)
- loop = asyncio.get_event_loop()
- host = loop.run_until_complete(init(loop, address, port)) # <7>
- print('Serving on {}. Hit CTRL-C to stop.'.format(host))
- try:
- loop.run_forever() # <8>
- except KeyboardInterrupt: # CTRL+C pressed
- pass
- print('Server shutting down.')
- loop.close() # <9>
-
-
-if __name__ == '__main__':
- main(*sys.argv[1:])
-# END HTTP_CHARFINDER_SETUP
diff --git a/18b-async-await/spinner_asyncio.py b/18b-async-await/spinner_asyncio.py
deleted file mode 100644
index 3366998..0000000
--- a/18b-async-await/spinner_asyncio.py
+++ /dev/null
@@ -1,53 +0,0 @@
-# spinner_asyncio.py
-
-# credits: Example by Luciano Ramalho inspired by
-# Michele Simionato's multiprocessing example in the python-list:
-# https://mail.python.org/pipermail/python-list/2009-February/538048.html
-
-# BEGIN SPINNER_ASYNCIO
-import asyncio
-import itertools
-import sys
-
-
-@asyncio.coroutine # <1>
-def spin(msg): # <2>
- write, flush = sys.stdout.write, sys.stdout.flush
- for char in itertools.cycle('|/-\\'):
- status = char + ' ' + msg
- write(status)
- flush()
- write('\x08' * len(status))
- try:
- yield from asyncio.sleep(.1) # <3>
- except asyncio.CancelledError: # <4>
- break
- write(' ' * len(status) + '\x08' * len(status))
-
-
-@asyncio.coroutine
-def slow_function(): # <5>
- # pretend waiting a long time for I/O
- yield from asyncio.sleep(3) # <6>
- return 42
-
-
-@asyncio.coroutine
-def supervisor(): # <7>
- spinner = asyncio.async(spin('thinking!')) # <8>
- print('spinner object:', spinner) # <9>
- result = yield from slow_function() # <10>
- spinner.cancel() # <11>
- return result
-
-
-def main():
- loop = asyncio.get_event_loop() # <12>
- result = loop.run_until_complete(supervisor()) # <13>
- loop.close()
- print('Answer:', result)
-
-
-if __name__ == '__main__':
- main()
-# END SPINNER_ASYNCIO