Skip to content
Browse files

Merge pull request #41 from steenzout/pep8

PEP8 fix and testing
  • Loading branch information...
2 parents 571a5e5 + 574117c commit 57c7653078d00210e59e603c41eae445985572ce @sah sah committed Feb 16, 2016
View
9 examples/basics.py
@@ -1,20 +1,25 @@
import monocle
+
from monocle import Return, InvalidYieldException
+
@monocle.o
def square(x):
- yield Return(x*x)
+ yield Return(x * x)
print "not reached"
+
@monocle.o
def fail():
raise Exception("boo")
print (yield square(2))
+
@monocle.o
def invalid_yield():
yield "this should fail"
+
@monocle.o
def main():
value = yield square(5)
@@ -31,9 +36,11 @@ def main():
else:
assert False
+
def func_fail():
raise Exception("boo")
+
monocle.launch(fail)
monocle.launch(func_fail)
monocle.launch(main)
View
5 examples/client_server.py
@@ -1,13 +1,14 @@
import sys
import time
-
import monocle
+
from monocle import _o
monocle.init(sys.argv[1])
from monocle.stack import eventloop
from monocle.stack.network import add_service, Service, Client, ConnectionLost
+
@_o
def handle_echo(conn):
while True:
@@ -17,6 +18,7 @@ def handle_echo(conn):
break
yield conn.write("you said: %s\r\n" % message.strip())
+
@_o
def do_echos():
try:
@@ -33,6 +35,7 @@ def do_echos():
client.close()
eventloop.halt()
+
add_service(Service(handle_echo, port=8000))
monocle.launch(do_echos)
eventloop.run()
View
5 examples/client_timeouts.py
@@ -1,10 +1,13 @@
import sys
import monocle
-from monocle import _o, Return
+
+from monocle import _o
monocle.init(sys.argv[1])
+
from monocle.stack import eventloop
from monocle.stack.network import Client
+
@_o
def main():
c = Client()
View
3 examples/echo_server.py
@@ -1,16 +1,19 @@
import sys
import monocle
+
from monocle import _o
monocle.init(sys.argv[1])
from monocle.stack import eventloop
from monocle.stack.network import add_service, Service
+
@_o
def echo(conn):
their_message = yield conn.readline()
yield conn.write("you said: %s\r\n" % their_message.strip())
+
add_service(Service(echo, 7050))
eventloop.run()
View
5 examples/eventloop.py
@@ -1,25 +1,30 @@
import sys
import monocle
+
from monocle import _o
monocle.init(sys.argv[1])
from monocle.stack import eventloop
from monocle.util import sleep
+
@_o
def yielding_oroutine(x, z=1):
yield sleep(1)
print x
+
def nonyielding_oroutine(x, z=1):
print x
+
@_o
def fail():
raise Exception("whoo")
yield sleep(1)
+
eventloop.queue_task(0, yielding_oroutine, x="oroutine worked")
eventloop.queue_task(0, nonyielding_oroutine, x="function worked")
eventloop.queue_task(0, fail)
View
3 examples/http_client.py
@@ -1,12 +1,14 @@
import sys
import monocle
+
from monocle import _o
monocle.init(sys.argv[1])
from monocle.stack import eventloop
from monocle.stack.network.http import HttpClient
+
@_o
def req():
client = HttpClient()
@@ -25,5 +27,6 @@ def req():
finally:
eventloop.halt()
+
monocle.launch(req)
eventloop.run()
View
5 examples/http_server.py
@@ -1,8 +1,8 @@
import sys
import logging
-
import monocle
-from monocle import _o, Return
+
+from monocle import Return
monocle.init(sys.argv[1])
from monocle.stack import eventloop
@@ -25,5 +25,6 @@ def hello_http(req):
headers.add('Set-Cookie', 'test1=blar; Path=/')
yield Return(200, headers, content)
+
add_service(s)
eventloop.run()
View
5 examples/service_error.py
@@ -1,20 +1,23 @@
import sys
-
import monocle
+
from monocle import _o
monocle.init(sys.argv[1])
from monocle.stack import eventloop
from monocle.stack.network import add_service, Service
+
@_o
def lower_one(conn):
raise Exception("testing")
yield
+
@_o
def top_one(conn):
yield lower_one(conn)
+
add_service(Service(top_one, 12345))
eventloop.run()
View
20 examples/sieve.py
@@ -50,34 +50,43 @@
# -sah
import sys
-
import monocle
+
from monocle import _o
monocle.init(sys.argv[1])
from monocle.stack import eventloop
from monocle.experimental import Channel
-# Send the sequence 2, 3, 4, ... to channel 'ch'.
+
@_o
def generate(ch):
+ """
+ Send the sequence 2, 3, 4, ... to channel 'ch'.
+ """
i = 2
while True:
yield ch.send(i) # Send 'i' to channel 'ch'.
i += 1
-# Copy the values from channel 'inc' to channel 'outc',
-# removing those divisible by 'prime'.
+
@_o
def filter(inc, outc, prime):
+ """
+ Copy the values from channel 'inc' to channel 'outc',
+ removing those divisible by 'prime'.
+ """
while True:
i = yield inc.recv() # Receive value of new variable 'i' from 'in'.
if i % prime != 0:
yield outc.send(i) # Send 'i' to channel 'outc'.
-# The prime sieve: Daisy-chain filter processes together.
+
@_o
def main():
+ """
+ The prime sieve: Daisy-chain filter processes together.
+ """
ch = Channel() # Create a new channel.
monocle.launch(generate, ch) # Start generate() as an o-routine.
while True:
@@ -87,5 +96,6 @@ def main():
filter(ch, ch1, prime)
ch = ch1
+
monocle.launch(main)
eventloop.run()
View
3 examples/simplechan.py
@@ -1,12 +1,14 @@
import sys
import monocle
+
from monocle import _o
monocle.init(sys.argv[1])
from monocle.stack import eventloop
from monocle.experimental import Channel
+
@_o
def main():
s = 2
@@ -20,5 +22,6 @@ def main():
print (yield ch.recv())
print "done"
+
monocle.launch(main)
eventloop.run()
View
4 examples/sleep.py
@@ -1,25 +1,29 @@
import sys
import monocle
+
from monocle import _o
monocle.init(sys.argv[1])
from monocle.stack import eventloop
from monocle.util import sleep
+
@_o
def print_every_second():
for i in xrange(5):
print "1"
yield sleep(1)
+
@_o
def print_every_two_seconds():
for i in xrange(5):
print "2"
yield sleep(2)
eventloop.halt()
+
monocle.launch(print_every_second)
monocle.launch(print_every_two_seconds)
eventloop.run()
View
9 examples/tb.py
@@ -8,32 +8,40 @@
from monocle.stack import eventloop
from monocle.stack.network.http import HttpClient
+
@_o
def req():
client = HttpClient()
yield client.connect("localhost", 12344, timeout=1)
+
def die():
raise Exception("boom")
+
@_o
def fifth():
die()
+
def fourth():
return fifth()
+
@_o
def third():
yield fourth()
+
def second():
return third()
+
@_o
def first():
yield second()
+
@_o
def first_evlp():
try:
@@ -43,6 +51,7 @@ def first_evlp():
finally:
eventloop.halt()
+
launch(first)
eventloop.queue_task(0, first_evlp)
eventloop.run()
View
6 examples/tcp_proxy.py
@@ -1,13 +1,15 @@
import sys
-import time
+import time
import monocle
+
from monocle import _o
monocle.init(sys.argv[1])
from monocle.stack import eventloop
from monocle.stack.network import add_service, Service, Client, ConnectionLost
+
@_o
def pump(input, output):
while True:
@@ -18,12 +20,14 @@ def pump(input, output):
output.close()
break
+
@_o
def handle_socks(conn):
client = Client()
yield client.connect('localhost', 8050)
monocle.launch(pump, conn, client)
yield pump(client, conn)
+
add_service(Service(handle_socks, port=7050))
eventloop.run()
View
3 monocle/__init__.py
@@ -1,11 +1,12 @@
import sys
-
import core
from core import _o, o, launch, Return, InvalidYieldException, log_exception
VERSION = '0.37'
_stack_name = None
+
+
def init(stack_name):
global _stack_name
_stack_name = stack_name
View
1 monocle/asyncore_stack/eventloop.py
@@ -4,6 +4,7 @@
from monocle import launch
+
class EventLoop(object):
def __init__(self):
self._running = True
View
7 monocle/callback.py
@@ -1,6 +1,9 @@
-# Sort of like Twisted's Deferred, but simplified. We don't do
-# callback chaining, since oroutines replace that mechanism.
class Callback(object):
+ """
+ Sort of like Twisted's Deferred, but simplified. We don't do
+ callback chaining, since oroutines replace that mechanism.
+ """
+
def __init__(self):
self._handlers = []
View
14 monocle/core.py
@@ -15,16 +15,20 @@
from twisted.python.failure import Failure as TwistedFailure
from twisted.internet.defer import Deferred as TwistedDeferred
except ImportError:
- class TwistedFailure: pass
- class TwistedDeferred: pass
+ class TwistedFailure:
+ pass
+
+ class TwistedDeferred:
+ pass
logging.basicConfig(stream=sys.stderr,
format="%(message)s")
log = logging.getLogger("monocle")
-blocking_warn_threshold = 500 # ms
+blocking_warn_threshold = 500 # ms
tracebacks_elide_internals = True
+
class Return(object):
def __init__(self, *args):
# mimic the semantics of the return statement
@@ -79,8 +83,8 @@ def format_tb(e, elide_internals=tracebacks_elide_internals):
for i, (tb, stack) in enumerate(reversed(e._monocle['tracebacks'])):
lines = tb.split('\n')
- first = lines[0] # "Traceback (most recent call last)"
- last = lines[-2] # Line describing the exception
+ first = lines[0] # "Traceback (most recent call last)"
+ last = lines[-2] # Line describing the exception
stack_lines = []
if not is_eventloop_stack(stack):
View
6 monocle/experimental.py
@@ -4,6 +4,7 @@
from collections import deque
from callback import Callback
+
from monocle.stack.eventloop import queue_task
from monocle import _o, Return
@@ -72,17 +73,18 @@ def cb(result, i=i):
yield Return([(True, r) if x == i else None for i in xrange(len(a))])
-waits = {}
-
@_o
def fire(name, value):
if name in waits:
cb = waits[name]
waits.pop(name)
cb(value)
+
@_o
def wait(name):
waits.setdefault(name, Callback())
r = yield waits[name]
yield Return(r)
+
+waits = {}
View
6 monocle/repl.py
@@ -7,14 +7,18 @@
from threading import Thread
import monocle
+
from monocle import _o, Return
monocle.init(sys.argv[1])
+
from monocle.stack import eventloop
from monocle.callback import Callback
+
# it's annoying to ever see these warnings at the repl, so tolerate a lot
monocle.core.blocking_warn_threshold = 10000
+
class HistoryConsole(code.InteractiveConsole):
def __init__(self, locals=None, filename="<console>",
histfile=os.path.expanduser("~/.console-history")):
@@ -33,6 +37,7 @@ def init_history(self, histfile):
def save_history(self, histfile):
readline.write_history_file(histfile)
+
@_o
def main():
print "Monocle", monocle.VERSION, "/", "Python", sys.version
@@ -48,6 +53,7 @@ def main():
source += "\n"
cb = Callback()
+
def wait_for_input():
try:
prompt = ">>> "
View
2 monocle/stack/__init__.py
@@ -1,6 +1,6 @@
import monocle
+
if monocle._stack_name == 'twisted':
from monocle.twisted_stack import *
elif monocle._stack_name == 'tornado':
from monocle.tornado_stack import *
-
View
1 monocle/stack/multiprocess/__init__.py
@@ -83,6 +83,7 @@ def recv(self):
value = yield self.split_chan.recv(self.name)
yield Return(value)
+
def make_subchannels(chan, subchans):
splitchan = SplitChannel(chan, subchans)
return [SubChannel(splitchan, name) for name in subchans]
View
4 monocle/stack/multiprocess/sync.py
@@ -38,7 +38,7 @@ def log_receive(chan):
h.setFormatter(h.old_formatter)
-### using sockets ###
+# using sockets
class SyncSockChannel(object):
def __init__(self, sock):
self.sock = sock
@@ -196,7 +196,7 @@ def launch_proc_with_sockets(target, port, *args, **kwargs):
yield Return(p, main_chan)
-### using pipes ###
+# using pipes
class PipeHandler(logging.Handler):
def __init__(self, pipe):
logging.Handler.__init__(self)
View
50 monocle/stack/network/__init__.py
@@ -3,35 +3,37 @@
from monocle.callback import Callback, defer
from monocle.stack import eventloop as evlp
+
class ConnectionLost(Exception):
pass
-# Connection takes a stack_conn, which should have this interface:
-#
-# def write(data):
-# writes the data
-#
-# read_cb: callback which is called when a read completes
-# connect_cb: callback which is called when the connection completes
-#
-# def resume():
-# resumes reading
-#
-# def reading():
-# returns a boolean indicating the current reading state
-#
-# def closed():
-# returns a boolean indicating the current closed state
-#
-# def disconnect():
-# closes the connection
-#
-# if the read/read_until implementations here are used:
-#
-# buffer: string buffer which grows before read_cb is called
-
class Connection(object):
+ """
+ Connection takes a stack_conn, which should have this interface:
+
+ def write(data):
+ writes the data
+
+ read_cb: callback which is called when a read completes
+ connect_cb: callback which is called when the connection completes
+
+ def resume():
+ resumes reading
+
+ def reading():
+ returns a boolean indicating the current reading state
+
+ def closed():
+ returns a boolean indicating the current closed state
+
+ def disconnect():
+ closes the connection
+
+ if the read/read_until implementations here are used:
+
+ buffer: string buffer which grows before read_cb is called
+ """
def __init__(self, stack_conn=None):
self._stack_conn = stack_conn
self.writing = False
View
4 monocle/stack/network/http.py
@@ -218,8 +218,7 @@ def read_response(conn):
elif content_length:
body = yield conn.read(content_length)
elif ((proto == 'http/1.0' and
- not headers.get('Connection', '').lower() == 'keep-alive')
- or
+ not headers.get('Connection', '').lower() == 'keep-alive') or
(proto == 'http/1.1' and
headers.get('Connection', '').lower() == 'close')):
while True:
@@ -458,6 +457,7 @@ def handle_request(self, req):
import monocle
+
if monocle._stack_name == 'twisted':
from monocle.twisted_stack.network.http import *
elif monocle._stack_name == 'tornado':
View
1 monocle/tornado_stack/eventloop.py
@@ -23,6 +23,7 @@ def __init__(self):
def queue_task(self, delay, callable, *args, **kw):
def task():
return launch(callable, *args, **kw)
+
def queue():
now = time.time()
timeout = self._tornado_ioloop.add_timeout(now + delay, task)
View
3 monocle/tornado_stack/network/__init__.py
@@ -18,12 +18,14 @@
from monocle.tornado_stack.eventloop import evlp
from monocle.util import monkeypatch
+
# monkeypatch IOStream to provide a reactive read
@monkeypatch(IOStream)
def __init__(orig_method, self, *a, **k):
orig_method(self, *a, **k)
self._read_some = None
+
@monkeypatch(IOStream)
def _read_from_buffer(orig_method, self, *a, **k):
if self._read_some is not None:
@@ -36,6 +38,7 @@ def _read_from_buffer(orig_method, self, *a, **k):
else:
return orig_method(self, *a, **k)
+
@monkeypatch(IOStream)
def read_some(orig_method, self, callback):
"""Call callback when we read some bytes."""
View
3 monocle/tornado_stack/network/http.py
@@ -13,7 +13,8 @@
from monocle.tornado_stack.network import Service
-class HttpException(Exception): pass
+class HttpException(Exception):
+ pass
class HttpClient(HttpClient):
View
3 monocle/twisted_stack/network/__init__.py
@@ -13,12 +13,14 @@
from monocle.util import monkeypatch
from twisted.protocols.tls import TLSMemoryBIOProtocol, WantReadError
+
# monkeypatch TLSMemoryBIOProtocol to resume reading when necessary for writes
@monkeypatch(TLSMemoryBIOProtocol)
def __init__(orig_method, self, *a, **k):
orig_method(self, *a, **k)
self._was_paused = False
+
@monkeypatch(TLSMemoryBIOProtocol)
def _write(orig_method, self, bytes):
was_blocked = self._writeBlockedOnRead
@@ -28,6 +30,7 @@ def _write(orig_method, self, bytes):
self._was_paused = True
self.transport.resumeProducing()
+
@monkeypatch(TLSMemoryBIOProtocol)
def dataReceived(orig_method, self, bytes):
was_blocked = self._writeBlockedOnRead
View
4 monocle/twisted_stack/network/http.py
@@ -17,7 +17,9 @@
log = logging.getLogger("monocle.twisted_stack.network.http")
-class HttpException(Exception): pass
+
+class HttpException(Exception):
+ pass
class _HttpServerResource(resource.Resource):
View
5 monocle/twisted_stack/utils.py
@@ -1,12 +1,15 @@
-from twisted.python.failure import Failure
from twisted.internet.defer import Deferred
+from twisted.python.failure import Failure
+
def cb_to_df(cb):
df = Deferred()
+
def call_deferred_back(v, df=df):
if isinstance(v, Exception):
df.errback(Failure(v, type(v), None))
else:
df.callback(v)
+
cb.add(call_deferred_back)
return df
View
4 monocle/util.py
@@ -3,19 +3,23 @@
from monocle.stack.eventloop import queue_task
from monocle.callback import Callback
+
def sleep(seconds):
cb = Callback()
queue_task(seconds, cb, None)
return cb
+
def monkeypatch(cls):
def decorator(f):
orig_method = None
method = getattr(cls, f.func_name, None)
if method:
orig_method = lambda *a, **k: method(*a, **k)
+
def g(*a, **k):
return f(orig_method, *a, **k)
+
g.func_name = f.func_name
setattr(cls, f.func_name,
new.instancemethod(g, None, cls))
View
2 o_test.py
@@ -160,6 +160,7 @@ def main(args):
class State(object):
debug = False
+
def trace(self, msg):
if self.debug:
print msg
@@ -169,6 +170,7 @@ class Config(object):
class Session(object):
config = Config()
+
def isinitpath(self, fn):
return False
View
1 tests/channel.py
@@ -16,6 +16,7 @@ def test_send_recv():
r = yield ch.recv()
assert r == 'ok%s' % x
+
@_o
def sendxs(ch, xs):
print "in sendxs"
View
4 tests/stack_network.py
@@ -66,9 +66,11 @@ def test_read_delay(self):
data = 'ok'
self.buffer = 'o'
self.conn.timeout = 0.2
+
def populate():
self.buffer += 'k'
self.stack_conn.read_cb(None)
+
eventloop.queue_task(0.1, populate)
r = yield self.conn.read(2)
assert r == data
@@ -99,9 +101,11 @@ def test_read_some(self):
def test_read_some_delay(self):
data = 'ok'
self.conn.timeout = 0.2
+
def populate():
self.buffer = data
self.stack_conn.read_cb(None)
+
eventloop.queue_task(0.1, populate)
r = yield self.conn.read_some()
assert r == data
View
2 tox.ini
@@ -16,6 +16,7 @@ deps =
tornado: tornado
twisted: twisted
twisted26: twisted<15.5.0
+ pep8
pylint
pyOpenSSL
pytest
@@ -31,6 +32,7 @@ commands =
python examples/client_server.py {env:MONOCLE_STACK}
python examples/sleep.py {env:MONOCLE_STACK}
python examples/tb.py {env:MONOCLE_STACK}
+ pep8 --ignore='E125,E129,E265,E402,E501,E731,W291,W293'
[testenv:asyncore]
setenv =

0 comments on commit 57c7653

Please sign in to comment.
Something went wrong with that request. Please try again.