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

Change import statements to import only what's needed #468

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions examples/cacheclt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

import argparse
import asyncio
from asyncio import test_utils
import asyncio.test_utils
import json
import logging

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports should be sorted alphabetically (that's what we try to do in asyncio and CPython code bases).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@1st1 I wasn't aware of that, sorry. I was going by the PEP8 guidelines, where it tells us to import the standard library modules first. But I'll have that fix following your recommendations.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@denisra Asyncio is technically a stdlib module. ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SethMichaelLarson I can't say that it isn't :) will send send in another commit in just a few min with that fixed


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

key = 'foo-%s' % label
while True:
logging.info('%s %s', label, '-'*20)
logging.info('%s %s', label, '-' * 20)
try:
ret = yield from w(cache.set(key, 'hello-%s-world' % label))
logging.info('%s set %s', label, ret)
Expand Down
6 changes: 5 additions & 1 deletion examples/child_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# Return a write-only transport wrapping a writable pipe
#


@asyncio.coroutine
def connect_write_pipe(file):
loop = asyncio.get_event_loop()
Expand All @@ -36,10 +37,12 @@ def connect_write_pipe(file):
# Wrap a readable pipe in a stream
#


@asyncio.coroutine
def connect_read_pipe(file):
loop = asyncio.get_event_loop()
stream_reader = asyncio.StreamReader(loop=loop)

def factory():
return asyncio.StreamReaderProtocol(stream_reader)
transport, _ = yield from loop.connect_read_pipe(factory, file)
Expand Down Expand Up @@ -85,7 +88,7 @@ def writeall(fd, buf):
stderr, stderr_transport = yield from connect_read_pipe(p.stderr)

# interact with subprocess
name = {stdout:'OUT', stderr:'ERR'}
name = {stdout: 'OUT', stderr: 'ERR'}
registered = {asyncio.Task(stderr.readline()): stderr,
asyncio.Task(stdout.readline()): stdout}
while registered:
Expand Down Expand Up @@ -116,6 +119,7 @@ def writeall(fd, buf):
stdout_transport.close()
stderr_transport.close()


if __name__ == '__main__':
if sys.platform == 'win32':
loop = ProactorEventLoop()
Expand Down
6 changes: 3 additions & 3 deletions examples/crawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def send_request(self):
self.headers.append(('User-Agent', 'asyncio-example-crawl/0.0'))
self.headers.append(('Host', self.netloc))
self.headers.append(('Accept', '*/*'))
##self.headers.append(('Accept-Encoding', 'gzip'))
# self.headers.append(('Accept-Encoding', 'gzip'))
for key, value in self.headers:
line = '%s: %s' % (key, value)
yield from self.putline(line)
Expand Down Expand Up @@ -519,7 +519,7 @@ def fetch(self):
self.exceptions.append(exc)
self.log(1, 'try', self.tries, 'for', self.url,
'raised', repr(exc))
##import pdb; pdb.set_trace()
# import pdb; pdb.set_trace()
# Don't reuse the connection in this case.
finally:
if self.request is not None:
Expand All @@ -534,7 +534,7 @@ def fetch(self):
self.next_url = urllib.parse.urljoin(self.url, next_url)
if self.max_redirect > 0:
self.log(1, 'redirect to', self.next_url, 'from', self.url)
self.crawler.add_url(self.next_url, self.max_redirect-1)
self.crawler.add_url(self.next_url, self.max_redirect - 1)
else:
self.log(0, 'redirect limit reached for', self.next_url,
'from', self.url)
Expand Down
3 changes: 3 additions & 0 deletions examples/echo_client_tulip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import asyncio


END = b'Bye-bye!\n'


@asyncio.coroutine
def echo_client():
reader, writer = yield from asyncio.open_connection('localhost', 8000)
Expand All @@ -15,6 +17,7 @@ def echo_client():
break
writer.close()


loop = asyncio.get_event_loop()
loop.run_until_complete(echo_client())
loop.close()
3 changes: 3 additions & 0 deletions examples/echo_server_tulip.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import asyncio


@asyncio.coroutine
def echo_server():
yield from asyncio.start_server(handle_connection, 'localhost', 8000)


@asyncio.coroutine
def handle_connection(reader, writer):
while True:
Expand All @@ -12,6 +14,7 @@ def handle_connection(reader, writer):
break
writer.write(data)


loop = asyncio.get_event_loop()
loop.run_until_complete(echo_server())
try:
Expand Down
9 changes: 4 additions & 5 deletions examples/fetch0.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""Simplest possible HTTP client."""

import asyncio
import sys

from asyncio import *


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


def main():
loop = get_event_loop()
loop = asyncio.get_event_loop()
try:
body = loop.run_until_complete(fetch())
finally:
Expand Down
22 changes: 12 additions & 10 deletions examples/fetch1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
This version adds URL parsing (including SSL) and a Response object.
"""

import asyncio
import sys
import urllib.parse

from asyncio import *


class Response:

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

@coroutine
@asyncio.coroutine
def read(self, reader):
@coroutine
@asyncio.coroutine
def getline():
return (yield from reader.readline()).decode('latin-1').rstrip()
status_line = yield from getline()
if self.verbose: print('<', status_line, file=sys.stderr)
if self.verbose:
print('<', status_line, file=sys.stderr)
self.http_version, status, self.reason = status_line.split(None, 2)
self.status = int(status)
while True:
header_line = yield from getline()
if not header_line:
break
if self.verbose: print('<', header_line, file=sys.stderr)
if self.verbose:
print('<', header_line, file=sys.stderr)
# TODO: Continuation lines.
key, value = header_line.split(':', 1)
self.headers.append((key, value.strip()))
if self.verbose: print(file=sys.stderr)
if self.verbose:
print(file=sys.stderr)


@coroutine
@asyncio.coroutine
def fetch(url, verbose=True):
parts = urllib.parse.urlparse(url)
if parts.scheme == 'http':
Expand All @@ -57,7 +59,7 @@ def fetch(url, verbose=True):
request = 'GET %s HTTP/1.0\r\n\r\n' % path
if verbose:
print('>', request, file=sys.stderr, end='')
r, w = yield from open_connection(parts.hostname, port, ssl=ssl)
r, w = yield from asyncio.open_connection(parts.hostname, port, ssl=ssl)
w.write(request.encode('latin-1'))
response = Response(verbose)
yield from response.read(r)
Expand All @@ -66,7 +68,7 @@ def fetch(url, verbose=True):


def main():
loop = get_event_loop()
loop = asyncio.get_event_loop()
try:
body = loop.run_until_complete(fetch(sys.argv[1], '-v' in sys.argv))
finally:
Expand Down
38 changes: 21 additions & 17 deletions examples/fetch2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
This version adds a Request object.
"""

import asyncio
from http.client import BadStatusLine
import sys
import urllib.parse
from http.client import BadStatusLine

from asyncio import *


class Request:
Expand All @@ -34,13 +33,13 @@ def __init__(self, url, verbose=True):
self.reader = None
self.writer = None

@coroutine
@asyncio.coroutine
def connect(self):
if self.verbose:
print('* Connecting to %s:%s using %s' %
(self.hostname, self.port, 'ssl' if self.ssl else 'tcp'),
file=sys.stderr)
self.reader, self.writer = yield from open_connection(self.hostname,
self.reader, self.writer = yield from asyncio.open_connection(self.hostname,
self.port,
ssl=self.ssl)
if self.verbose:
Expand All @@ -51,20 +50,22 @@ def connect(self):
def putline(self, line):
self.writer.write(line.encode('latin-1') + b'\r\n')

@coroutine
@asyncio.coroutine
def send_request(self):
request = '%s %s %s' % (self.method, self.full_path, self.http_version)
if self.verbose: print('>', request, file=sys.stderr)
if self.verbose:
print('>', request, file=sys.stderr)
self.putline(request)
if 'host' not in {key.lower() for key, _ in self.headers}:
self.headers.insert(0, ('Host', self.netloc))
for key, value in self.headers:
line = '%s: %s' % (key, value)
if self.verbose: print('>', line, file=sys.stderr)
if self.verbose:
print('>', line, file=sys.stderr)
self.putline(line)
self.putline('')

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

@coroutine
@asyncio.coroutine
def getline(self):
return (yield from self.reader.readline()).decode('latin-1').rstrip()

@coroutine
@asyncio.coroutine
def read_headers(self):
status_line = yield from self.getline()
if self.verbose: print('<', status_line, file=sys.stderr)
if self.verbose:
print('<', status_line, file=sys.stderr)
status_parts = status_line.split(None, 2)
if len(status_parts) != 3:
raise BadStatusLine(status_line)
Expand All @@ -98,13 +100,15 @@ def read_headers(self):
header_line = yield from self.getline()
if not header_line:
break
if self.verbose: print('<', header_line, file=sys.stderr)
if self.verbose:
print('<', header_line, file=sys.stderr)
# TODO: Continuation lines.
key, value = header_line.split(':', 1)
self.headers.append((key, value.strip()))
if self.verbose: print(file=sys.stderr)
if self.verbose:
print(file=sys.stderr)

@coroutine
@asyncio.coroutine
def read(self):
nbytes = None
for key, value in self.headers:
Expand All @@ -118,7 +122,7 @@ def read(self):
return body


@coroutine
@asyncio.coroutine
def fetch(url, verbose=True):
request = Request(url, verbose)
yield from request.connect()
Expand All @@ -129,7 +133,7 @@ def fetch(url, verbose=True):


def main():
loop = get_event_loop()
loop = asyncio.get_event_loop()
try:
body = loop.run_until_complete(fetch(sys.argv[1], '-v' in sys.argv))
finally:
Expand Down
Loading