Skip to content

Instantly share code, notes, and snippets.

import "dart:math";
enum Thing { one, two, three }
// The compiler complains that this function could return null, even though the switch is exhaustive.
// It seems to be due to the parentheses around each case clause.
String randomThing() {
final thing = Thing.values[Random().nextInt(Thing.values.length)];
switch (thing) {
case (Thing.one):
from collections import namedtuple
from functools import partial
Inning = namedtuple("Inning", ["half", "of"])
top = partial(Inning, half="top")
bottom = partial(Inning, half="bottom")
inning = bottom(of=9)
@dowski
dowski / waas.py
Created September 15, 2017 16:48
Wins As A Service
from flask import Flask
app = Flask(__name__)
@app.route("/wins/<int:number>")
def wins(number):
if number > 162:
return "Too many wins!"
return ("W" * number) + "indians"
@dowski
dowski / sample.py
Last active December 23, 2015 00:19
import diesel
class MyClient(diesel.Client):
def __init__(self, *args, **kw):
super(MyClient, self).__init__(*args, **kw)
# fork a background Loop to handle async messages
diesel.fork_child(self._dispatch_messages)
@diesel.call
def send_request(self, req):
@dowski
dowski / router.py
Last active December 21, 2015 22:59
Router functionality for diesel with a demo of a hybrid diesel+cheroot HTTP server. (Requires diesel to be at least up to https://github.com/dowski/diesel/commit/caf938ef57c55be4564d65d3937c889701d8afa7)
import socket
import diesel.core
from diesel import Service
from diesel.core import Connection, Loop
from diesel.util.queue import Queue
def forward(service):
@dowski
dowski / gist:5980650
Created July 12, 2013 01:13
diesel.wait() for signals
diff --git a/diesel/__init__.py b/diesel/__init__.py
index 93335ea..b085667 100644
--- a/diesel/__init__.py
+++ b/diesel/__init__.py
@@ -1,7 +1,7 @@
# vim:ts=4:sw=4:expandtab
from logmod import log, levels as loglevels, set_log_level
import events
-from core import sleep, Loop, wait, fire, thread, until, Connection, UDPSocket, ConnectionClosed, ClientConnectionClosed, signal
+from core import sleep, Loop, wait, fire, thread, until, Connection, UDPSocket, ConnectionClosed, ClientConnectionClosed
(env) christian@dowski-dev:~/py332/src/diesel$ dnosetests tests/unit/ tests/integration/
...................................................
----------------------------------------------------------------------
Ran 51 tests in 8.300s
OK
diff --git a/diesel/core.py b/diesel/core.py
index 268b071..249895e 100644
--- a/diesel/core.py
+++ b/diesel/core.py
@@ -317,6 +317,7 @@ class Loop(object):
ClientConnectionTimeout("connection timeout (%s:%s)" % (host, port))
))
+ status = {'dead':False}
def connect_callback():
$ python br-tryexcept.py
[2013/06/03 14:18:19] {diesel} WARNING:Starting diesel <hand-rolled select.epoll>
[2013/06/03 14:18:19] {diesel} ERROR:internal error: expected empty read on disconnected socket
error connecting
[2013/06/03 14:18:20] {diesel} ERROR:internal error: expected empty read on disconnected socket
error connecting
[2013/06/03 14:18:21] {diesel} ERROR:internal error: expected empty read on disconnected socket
error connecting
[2013/06/03 14:18:22] {diesel} ERROR:internal error: expected empty read on disconnected socket
error connecting
import sys
import diesel
from diesel.protocols.redis import RedisClient
def main():
try:
c = RedisClient('localhost', int(sys.argv[1]))
except diesel.ClientConnectionError: