Skip to content

Commit

Permalink
Some minor improvements on coding style.
Browse files Browse the repository at this point in the history
Fixes issue python#464
  • Loading branch information
denisra committed Nov 16, 2016
1 parent 3863b43 commit 5e4efd7
Show file tree
Hide file tree
Showing 24 changed files with 180 additions and 115 deletions.
11 changes: 6 additions & 5 deletions examples/cacheclt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"""

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

import asyncio


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 @@ -172,7 +173,7 @@ def main():
loop = asyncio.new_event_loop()
sslctx = None
if args.tls:
sslctx = test_utils.dummy_ssl_context()
sslctx = asyncio.test_utils.dummy_ssl_context()
cache = CacheClient(args.host, args.port, sslctx=sslctx, loop=loop)
try:
loop.run_until_complete(
Expand All @@ -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
4 changes: 3 additions & 1 deletion examples/cachesvr.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@
"""

import argparse
import asyncio
import json
import logging
import os
import random

import asyncio


ARGS = argparse.ArgumentParser(description='Cache server example.')
ARGS.add_argument(
'--tls', action='store_true', dest='tls',
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
15 changes: 8 additions & 7 deletions examples/crawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
# - Handle out of file descriptors directly? (How?)

import argparse
import asyncio
import asyncio.locks
import cgi
from http.client import BadStatusLine
import logging
import re
import sys
import time
import urllib.parse
import cgi
from http.client import BadStatusLine

import asyncio
import asyncio.locks


ARGS = argparse.ArgumentParser(description="Web crawler")
Expand Down Expand Up @@ -341,7 +342,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 +520,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 +535,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
8 changes: 4 additions & 4 deletions examples/fetch0.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import sys

from asyncio import get_event_loop, open_connection, coroutine
import asyncio


@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 +23,7 @@ def fetch():


def main():
loop = get_event_loop()
loop = asyncio.get_event_loop()
try:
body = loop.run_until_complete(fetch())
finally:
Expand Down
21 changes: 12 additions & 9 deletions examples/fetch1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
import urllib.parse

from asyncio import get_event_loop, open_connection, coroutine
import asyncio


class Response:
Expand All @@ -18,27 +18,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 +60,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 +69,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
35 changes: 20 additions & 15 deletions examples/fetch2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import urllib.parse
from http.client import BadStatusLine

from asyncio import get_event_loop, open_connection, coroutine
import asyncio


class Request:
Expand All @@ -34,13 +34,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 +51,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 +83,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 +101,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 +123,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 +134,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

0 comments on commit 5e4efd7

Please sign in to comment.