Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit a5df171

Browse files
committed
Changed all the import statements in all modules in the examples
directory to import only what is needed. This fix issue #464
1 parent fff05d4 commit a5df171

23 files changed

+168
-120
lines changed

examples/cacheclt.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
import argparse
77
import asyncio
8-
from asyncio import test_utils
8+
import asyncio.test_utils
99
import json
1010
import logging
1111

12+
1213
ARGS = argparse.ArgumentParser(description='Cache client example.')
1314
ARGS.add_argument(
1415
'--tls', action='store_true', dest='tls',
@@ -106,7 +107,7 @@ def activity(self):
106107
self.reader, self.writer = yield from asyncio.open_connection(
107108
self.host, self.port, ssl=self.sslctx, loop=self.loop)
108109
except Exception as exc:
109-
backoff = min(args.max_backoff, backoff + (backoff//2) + 1)
110+
backoff = min(args.max_backoff, backoff + (backoff // 2) + 1)
110111
logging.info('Error connecting: %r; sleep %s', exc, backoff)
111112
yield from asyncio.sleep(backoff, loop=self.loop)
112113
continue
@@ -191,7 +192,7 @@ def w(g):
191192

192193
key = 'foo-%s' % label
193194
while True:
194-
logging.info('%s %s', label, '-'*20)
195+
logging.info('%s %s', label, '-' * 20)
195196
try:
196197
ret = yield from w(cache.set(key, 'hello-%s-world' % label))
197198
logging.info('%s set %s', label, ret)

examples/child_process.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
# Return a write-only transport wrapping a writable pipe
2727
#
2828

29+
2930
@asyncio.coroutine
3031
def connect_write_pipe(file):
3132
loop = asyncio.get_event_loop()
@@ -36,10 +37,12 @@ def connect_write_pipe(file):
3637
# Wrap a readable pipe in a stream
3738
#
3839

40+
3941
@asyncio.coroutine
4042
def connect_read_pipe(file):
4143
loop = asyncio.get_event_loop()
4244
stream_reader = asyncio.StreamReader(loop=loop)
45+
4346
def factory():
4447
return asyncio.StreamReaderProtocol(stream_reader)
4548
transport, _ = yield from loop.connect_read_pipe(factory, file)
@@ -85,7 +88,7 @@ def writeall(fd, buf):
8588
stderr, stderr_transport = yield from connect_read_pipe(p.stderr)
8689

8790
# interact with subprocess
88-
name = {stdout:'OUT', stderr:'ERR'}
91+
name = {stdout: 'OUT', stderr: 'ERR'}
8992
registered = {asyncio.Task(stderr.readline()): stderr,
9093
asyncio.Task(stdout.readline()): stdout}
9194
while registered:
@@ -116,6 +119,7 @@ def writeall(fd, buf):
116119
stdout_transport.close()
117120
stderr_transport.close()
118121

122+
119123
if __name__ == '__main__':
120124
if sys.platform == 'win32':
121125
loop = ProactorEventLoop()

examples/crawl.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def send_request(self):
341341
self.headers.append(('User-Agent', 'asyncio-example-crawl/0.0'))
342342
self.headers.append(('Host', self.netloc))
343343
self.headers.append(('Accept', '*/*'))
344-
##self.headers.append(('Accept-Encoding', 'gzip'))
344+
# self.headers.append(('Accept-Encoding', 'gzip'))
345345
for key, value in self.headers:
346346
line = '%s: %s' % (key, value)
347347
yield from self.putline(line)
@@ -519,7 +519,7 @@ def fetch(self):
519519
self.exceptions.append(exc)
520520
self.log(1, 'try', self.tries, 'for', self.url,
521521
'raised', repr(exc))
522-
##import pdb; pdb.set_trace()
522+
# import pdb; pdb.set_trace()
523523
# Don't reuse the connection in this case.
524524
finally:
525525
if self.request is not None:
@@ -534,7 +534,7 @@ def fetch(self):
534534
self.next_url = urllib.parse.urljoin(self.url, next_url)
535535
if self.max_redirect > 0:
536536
self.log(1, 'redirect to', self.next_url, 'from', self.url)
537-
self.crawler.add_url(self.next_url, self.max_redirect-1)
537+
self.crawler.add_url(self.next_url, self.max_redirect - 1)
538538
else:
539539
self.log(0, 'redirect limit reached for', self.next_url,
540540
'from', self.url)

examples/echo_client_tulip.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import asyncio
22

3+
34
END = b'Bye-bye!\n'
45

6+
57
@asyncio.coroutine
68
def echo_client():
79
reader, writer = yield from asyncio.open_connection('localhost', 8000)
@@ -15,6 +17,7 @@ def echo_client():
1517
break
1618
writer.close()
1719

20+
1821
loop = asyncio.get_event_loop()
1922
loop.run_until_complete(echo_client())
2023
loop.close()

examples/echo_server_tulip.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import asyncio
22

3+
34
@asyncio.coroutine
45
def echo_server():
56
yield from asyncio.start_server(handle_connection, 'localhost', 8000)
67

8+
79
@asyncio.coroutine
810
def handle_connection(reader, writer):
911
while True:
@@ -12,6 +14,7 @@ def handle_connection(reader, writer):
1214
break
1315
writer.write(data)
1416

17+
1518
loop = asyncio.get_event_loop()
1619
loop.run_until_complete(echo_server())
1720
try:

examples/fetch0.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""Simplest possible HTTP client."""
22

3+
import asyncio
34
import sys
45

5-
from asyncio import *
66

7-
8-
@coroutine
7+
@asyncio.coroutine
98
def fetch():
10-
r, w = yield from open_connection('python.org', 80)
9+
r, w = yield from asyncio.open_connection('python.org', 80)
1110
request = 'GET / HTTP/1.0\r\n\r\n'
1211
print('>', request, file=sys.stderr)
1312
w.write(request.encode('latin-1'))
@@ -23,7 +22,7 @@ def fetch():
2322

2423

2524
def main():
26-
loop = get_event_loop()
25+
loop = asyncio.get_event_loop()
2726
try:
2827
body = loop.run_until_complete(fetch())
2928
finally:

examples/fetch1.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
This version adds URL parsing (including SSL) and a Response object.
44
"""
55

6+
import asyncio
67
import sys
78
import urllib.parse
89

9-
from asyncio import *
10-
1110

1211
class Response:
1312

@@ -18,27 +17,30 @@ def __init__(self, verbose=True):
1817
self.reason = None # 'Ok'
1918
self.headers = [] # [('Content-Type', 'text/html')]
2019

21-
@coroutine
20+
@asyncio.coroutine
2221
def read(self, reader):
23-
@coroutine
22+
@asyncio.coroutine
2423
def getline():
2524
return (yield from reader.readline()).decode('latin-1').rstrip()
2625
status_line = yield from getline()
27-
if self.verbose: print('<', status_line, file=sys.stderr)
26+
if self.verbose:
27+
print('<', status_line, file=sys.stderr)
2828
self.http_version, status, self.reason = status_line.split(None, 2)
2929
self.status = int(status)
3030
while True:
3131
header_line = yield from getline()
3232
if not header_line:
3333
break
34-
if self.verbose: print('<', header_line, file=sys.stderr)
34+
if self.verbose:
35+
print('<', header_line, file=sys.stderr)
3536
# TODO: Continuation lines.
3637
key, value = header_line.split(':', 1)
3738
self.headers.append((key, value.strip()))
38-
if self.verbose: print(file=sys.stderr)
39+
if self.verbose:
40+
print(file=sys.stderr)
3941

4042

41-
@coroutine
43+
@asyncio.coroutine
4244
def fetch(url, verbose=True):
4345
parts = urllib.parse.urlparse(url)
4446
if parts.scheme == 'http':
@@ -57,7 +59,7 @@ def fetch(url, verbose=True):
5759
request = 'GET %s HTTP/1.0\r\n\r\n' % path
5860
if verbose:
5961
print('>', request, file=sys.stderr, end='')
60-
r, w = yield from open_connection(parts.hostname, port, ssl=ssl)
62+
r, w = yield from asyncio.open_connection(parts.hostname, port, ssl=ssl)
6163
w.write(request.encode('latin-1'))
6264
response = Response(verbose)
6365
yield from response.read(r)
@@ -66,7 +68,7 @@ def fetch(url, verbose=True):
6668

6769

6870
def main():
69-
loop = get_event_loop()
71+
loop = asyncio.get_event_loop()
7072
try:
7173
body = loop.run_until_complete(fetch(sys.argv[1], '-v' in sys.argv))
7274
finally:

examples/fetch2.py

+21-17
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
This version adds a Request object.
44
"""
55

6+
import asyncio
7+
from http.client import BadStatusLine
68
import sys
79
import urllib.parse
8-
from http.client import BadStatusLine
9-
10-
from asyncio import *
1110

1211

1312
class Request:
@@ -34,13 +33,13 @@ def __init__(self, url, verbose=True):
3433
self.reader = None
3534
self.writer = None
3635

37-
@coroutine
36+
@asyncio.coroutine
3837
def connect(self):
3938
if self.verbose:
4039
print('* Connecting to %s:%s using %s' %
4140
(self.hostname, self.port, 'ssl' if self.ssl else 'tcp'),
4241
file=sys.stderr)
43-
self.reader, self.writer = yield from open_connection(self.hostname,
42+
self.reader, self.writer = yield from asyncio.open_connection(self.hostname,
4443
self.port,
4544
ssl=self.ssl)
4645
if self.verbose:
@@ -51,20 +50,22 @@ def connect(self):
5150
def putline(self, line):
5251
self.writer.write(line.encode('latin-1') + b'\r\n')
5352

54-
@coroutine
53+
@asyncio.coroutine
5554
def send_request(self):
5655
request = '%s %s %s' % (self.method, self.full_path, self.http_version)
57-
if self.verbose: print('>', request, file=sys.stderr)
56+
if self.verbose:
57+
print('>', request, file=sys.stderr)
5858
self.putline(request)
5959
if 'host' not in {key.lower() for key, _ in self.headers}:
6060
self.headers.insert(0, ('Host', self.netloc))
6161
for key, value in self.headers:
6262
line = '%s: %s' % (key, value)
63-
if self.verbose: print('>', line, file=sys.stderr)
63+
if self.verbose:
64+
print('>', line, file=sys.stderr)
6465
self.putline(line)
6566
self.putline('')
6667

67-
@coroutine
68+
@asyncio.coroutine
6869
def get_response(self):
6970
response = Response(self.reader, self.verbose)
7071
yield from response.read_headers()
@@ -81,14 +82,15 @@ def __init__(self, reader, verbose=True):
8182
self.reason = None # 'Ok'
8283
self.headers = [] # [('Content-Type', 'text/html')]
8384

84-
@coroutine
85+
@asyncio.coroutine
8586
def getline(self):
8687
return (yield from self.reader.readline()).decode('latin-1').rstrip()
8788

88-
@coroutine
89+
@asyncio.coroutine
8990
def read_headers(self):
9091
status_line = yield from self.getline()
91-
if self.verbose: print('<', status_line, file=sys.stderr)
92+
if self.verbose:
93+
print('<', status_line, file=sys.stderr)
9294
status_parts = status_line.split(None, 2)
9395
if len(status_parts) != 3:
9496
raise BadStatusLine(status_line)
@@ -98,13 +100,15 @@ def read_headers(self):
98100
header_line = yield from self.getline()
99101
if not header_line:
100102
break
101-
if self.verbose: print('<', header_line, file=sys.stderr)
103+
if self.verbose:
104+
print('<', header_line, file=sys.stderr)
102105
# TODO: Continuation lines.
103106
key, value = header_line.split(':', 1)
104107
self.headers.append((key, value.strip()))
105-
if self.verbose: print(file=sys.stderr)
108+
if self.verbose:
109+
print(file=sys.stderr)
106110

107-
@coroutine
111+
@asyncio.coroutine
108112
def read(self):
109113
nbytes = None
110114
for key, value in self.headers:
@@ -118,7 +122,7 @@ def read(self):
118122
return body
119123

120124

121-
@coroutine
125+
@asyncio.coroutine
122126
def fetch(url, verbose=True):
123127
request = Request(url, verbose)
124128
yield from request.connect()
@@ -129,7 +133,7 @@ def fetch(url, verbose=True):
129133

130134

131135
def main():
132-
loop = get_event_loop()
136+
loop = asyncio.get_event_loop()
133137
try:
134138
body = loop.run_until_complete(fetch(sys.argv[1], '-v' in sys.argv))
135139
finally:

0 commit comments

Comments
 (0)