Skip to content

Commit 5e4efd7

Browse files
committed
Some minor improvements on coding style.
Fixes issue python#464
1 parent 3863b43 commit 5e4efd7

24 files changed

+180
-115
lines changed

examples/cacheclt.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"""
55

66
import argparse
7-
import asyncio
8-
from asyncio import test_utils
97
import json
108
import logging
119

10+
import asyncio
11+
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
@@ -172,7 +173,7 @@ def main():
172173
loop = asyncio.new_event_loop()
173174
sslctx = None
174175
if args.tls:
175-
sslctx = test_utils.dummy_ssl_context()
176+
sslctx = asyncio.test_utils.dummy_ssl_context()
176177
cache = CacheClient(args.host, args.port, sslctx=sslctx, loop=loop)
177178
try:
178179
loop.run_until_complete(
@@ -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/cachesvr.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@
5757
"""
5858

5959
import argparse
60-
import asyncio
6160
import json
6261
import logging
6362
import os
6463
import random
6564

65+
import asyncio
66+
67+
6668
ARGS = argparse.ArgumentParser(description='Cache server example.')
6769
ARGS.add_argument(
6870
'--tls', action='store_true', dest='tls',

examples/child_process.py

Lines changed: 5 additions & 1 deletion
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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
# - Handle out of file descriptors directly? (How?)
1616

1717
import argparse
18-
import asyncio
19-
import asyncio.locks
20-
import cgi
21-
from http.client import BadStatusLine
2218
import logging
2319
import re
2420
import sys
2521
import time
2622
import urllib.parse
23+
import cgi
24+
from http.client import BadStatusLine
25+
26+
import asyncio
27+
import asyncio.locks
2728

2829

2930
ARGS = argparse.ArgumentParser(description="Web crawler")
@@ -341,7 +342,7 @@ def send_request(self):
341342
self.headers.append(('User-Agent', 'asyncio-example-crawl/0.0'))
342343
self.headers.append(('Host', self.netloc))
343344
self.headers.append(('Accept', '*/*'))
344-
##self.headers.append(('Accept-Encoding', 'gzip'))
345+
# self.headers.append(('Accept-Encoding', 'gzip'))
345346
for key, value in self.headers:
346347
line = '%s: %s' % (key, value)
347348
yield from self.putline(line)
@@ -519,7 +520,7 @@ def fetch(self):
519520
self.exceptions.append(exc)
520521
self.log(1, 'try', self.tries, 'for', self.url,
521522
'raised', repr(exc))
522-
##import pdb; pdb.set_trace()
523+
# import pdb; pdb.set_trace()
523524
# Don't reuse the connection in this case.
524525
finally:
525526
if self.request is not None:
@@ -534,7 +535,7 @@ def fetch(self):
534535
self.next_url = urllib.parse.urljoin(self.url, next_url)
535536
if self.max_redirect > 0:
536537
self.log(1, 'redirect to', self.next_url, 'from', self.url)
537-
self.crawler.add_url(self.next_url, self.max_redirect-1)
538+
self.crawler.add_url(self.next_url, self.max_redirect - 1)
538539
else:
539540
self.log(0, 'redirect limit reached for', self.next_url,
540541
'from', self.url)

examples/echo_client_tulip.py

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import sys
44

5-
from asyncio import get_event_loop, open_connection, coroutine
5+
import asyncio
66

77

8-
@coroutine
8+
@asyncio.coroutine
99
def fetch():
10-
r, w = yield from open_connection('python.org', 80)
10+
r, w = yield from asyncio.open_connection('python.org', 80)
1111
request = 'GET / HTTP/1.0\r\n\r\n'
1212
print('>', request, file=sys.stderr)
1313
w.write(request.encode('latin-1'))
@@ -23,7 +23,7 @@ def fetch():
2323

2424

2525
def main():
26-
loop = get_event_loop()
26+
loop = asyncio.get_event_loop()
2727
try:
2828
body = loop.run_until_complete(fetch())
2929
finally:

examples/fetch1.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sys
77
import urllib.parse
88

9-
from asyncio import get_event_loop, open_connection, coroutine
9+
import asyncio
1010

1111

1212
class Response:
@@ -18,27 +18,30 @@ def __init__(self, verbose=True):
1818
self.reason = None # 'Ok'
1919
self.headers = [] # [('Content-Type', 'text/html')]
2020

21-
@coroutine
21+
@asyncio.coroutine
2222
def read(self, reader):
23-
@coroutine
23+
@asyncio.coroutine
2424
def getline():
2525
return (yield from reader.readline()).decode('latin-1').rstrip()
2626
status_line = yield from getline()
27-
if self.verbose: print('<', status_line, file=sys.stderr)
27+
if self.verbose:
28+
print('<', status_line, file=sys.stderr)
2829
self.http_version, status, self.reason = status_line.split(None, 2)
2930
self.status = int(status)
3031
while True:
3132
header_line = yield from getline()
3233
if not header_line:
3334
break
34-
if self.verbose: print('<', header_line, file=sys.stderr)
35+
if self.verbose:
36+
print('<', header_line, file=sys.stderr)
3537
# TODO: Continuation lines.
3638
key, value = header_line.split(':', 1)
3739
self.headers.append((key, value.strip()))
38-
if self.verbose: print(file=sys.stderr)
40+
if self.verbose:
41+
print(file=sys.stderr)
3942

4043

41-
@coroutine
44+
@asyncio.coroutine
4245
def fetch(url, verbose=True):
4346
parts = urllib.parse.urlparse(url)
4447
if parts.scheme == 'http':
@@ -57,7 +60,7 @@ def fetch(url, verbose=True):
5760
request = 'GET %s HTTP/1.0\r\n\r\n' % path
5861
if verbose:
5962
print('>', request, file=sys.stderr, end='')
60-
r, w = yield from open_connection(parts.hostname, port, ssl=ssl)
63+
r, w = yield from asyncio.open_connection(parts.hostname, port, ssl=ssl)
6164
w.write(request.encode('latin-1'))
6265
response = Response(verbose)
6366
yield from response.read(r)
@@ -66,7 +69,7 @@ def fetch(url, verbose=True):
6669

6770

6871
def main():
69-
loop = get_event_loop()
72+
loop = asyncio.get_event_loop()
7073
try:
7174
body = loop.run_until_complete(fetch(sys.argv[1], '-v' in sys.argv))
7275
finally:

examples/fetch2.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import urllib.parse
88
from http.client import BadStatusLine
99

10-
from asyncio import get_event_loop, open_connection, coroutine
10+
import asyncio
1111

1212

1313
class Request:
@@ -34,13 +34,13 @@ def __init__(self, url, verbose=True):
3434
self.reader = None
3535
self.writer = None
3636

37-
@coroutine
37+
@asyncio.coroutine
3838
def connect(self):
3939
if self.verbose:
4040
print('* Connecting to %s:%s using %s' %
4141
(self.hostname, self.port, 'ssl' if self.ssl else 'tcp'),
4242
file=sys.stderr)
43-
self.reader, self.writer = yield from open_connection(self.hostname,
43+
self.reader, self.writer = yield from asyncio.open_connection(self.hostname,
4444
self.port,
4545
ssl=self.ssl)
4646
if self.verbose:
@@ -51,20 +51,22 @@ def connect(self):
5151
def putline(self, line):
5252
self.writer.write(line.encode('latin-1') + b'\r\n')
5353

54-
@coroutine
54+
@asyncio.coroutine
5555
def send_request(self):
5656
request = '%s %s %s' % (self.method, self.full_path, self.http_version)
57-
if self.verbose: print('>', request, file=sys.stderr)
57+
if self.verbose:
58+
print('>', request, file=sys.stderr)
5859
self.putline(request)
5960
if 'host' not in {key.lower() for key, _ in self.headers}:
6061
self.headers.insert(0, ('Host', self.netloc))
6162
for key, value in self.headers:
6263
line = '%s: %s' % (key, value)
63-
if self.verbose: print('>', line, file=sys.stderr)
64+
if self.verbose:
65+
print('>', line, file=sys.stderr)
6466
self.putline(line)
6567
self.putline('')
6668

67-
@coroutine
69+
@asyncio.coroutine
6870
def get_response(self):
6971
response = Response(self.reader, self.verbose)
7072
yield from response.read_headers()
@@ -81,14 +83,15 @@ def __init__(self, reader, verbose=True):
8183
self.reason = None # 'Ok'
8284
self.headers = [] # [('Content-Type', 'text/html')]
8385

84-
@coroutine
86+
@asyncio.coroutine
8587
def getline(self):
8688
return (yield from self.reader.readline()).decode('latin-1').rstrip()
8789

88-
@coroutine
90+
@asyncio.coroutine
8991
def read_headers(self):
9092
status_line = yield from self.getline()
91-
if self.verbose: print('<', status_line, file=sys.stderr)
93+
if self.verbose:
94+
print('<', status_line, file=sys.stderr)
9295
status_parts = status_line.split(None, 2)
9396
if len(status_parts) != 3:
9497
raise BadStatusLine(status_line)
@@ -98,13 +101,15 @@ def read_headers(self):
98101
header_line = yield from self.getline()
99102
if not header_line:
100103
break
101-
if self.verbose: print('<', header_line, file=sys.stderr)
104+
if self.verbose:
105+
print('<', header_line, file=sys.stderr)
102106
# TODO: Continuation lines.
103107
key, value = header_line.split(':', 1)
104108
self.headers.append((key, value.strip()))
105-
if self.verbose: print(file=sys.stderr)
109+
if self.verbose:
110+
print(file=sys.stderr)
106111

107-
@coroutine
112+
@asyncio.coroutine
108113
def read(self):
109114
nbytes = None
110115
for key, value in self.headers:
@@ -118,7 +123,7 @@ def read(self):
118123
return body
119124

120125

121-
@coroutine
126+
@asyncio.coroutine
122127
def fetch(url, verbose=True):
123128
request = Request(url, verbose)
124129
yield from request.connect()
@@ -129,7 +134,7 @@ def fetch(url, verbose=True):
129134

130135

131136
def main():
132-
loop = get_event_loop()
137+
loop = asyncio.get_event_loop()
133138
try:
134139
body = loop.run_until_complete(fetch(sys.argv[1], '-v' in sys.argv))
135140
finally:

0 commit comments

Comments
 (0)