Skip to content

Instantly share code, notes, and snippets.

@saghul
saghul / client_test.py
Created October 5, 2012 15:05
Twisted Wokkel disco client example
from application import log
from functools import partial
from optparse import OptionParser
from twisted.internet import reactor
from twisted.words.protocols.jabber.jid import JID
from wokkel.client import DeferredClientFactory, clientCreator
from wokkel.disco import DiscoClientProtocol
def cb(handle):
print("%r, closed=%s" % (handle, handle.closed))
loop.walk(cb)
@saghul
saghul / xqueue.py
Created November 1, 2012 22:47
Queue implementation using pyuv locks
"""A multi-producer, multi-consumer queue."""
from collections import deque
from time import time as _time
from pyuv import thread as _thread
__all__ = ['Empty', 'Full', 'Queue']
class Empty(Exception):
"Exception raised by Queue.get(block=0)/get_nowait()."
from threading import Thread
from time import sleep
n_items = 1000
queue = None
def put_stuff():
for x in xrange(n_items):
queue.put_nowait("xxx")
@saghul
saghul / testpartial.py
Created November 9, 2012 21:34
functools.partial example
from functools import partial
class WorkItem(object):
def __init__(self, func, *args, **kwargs):
self.func = func
self.args = args
self.kwargs = kwargs
class _WorkItem(object):
def __init__(self, future, fn, args, kwargs):
self.future = future
self.fn = fn
self.args = args
self.kwargs = kwargs
def run(self):
if not self.future.set_running_or_notify_cancel():
return
@saghul
saghul / test.py
Created November 20, 2012 16:45
PyQt / PySide bug example
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created: Tue Nov 20 17:41:14 2012
# by: PyQt4 UI code generator 4.9.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
@saghul
saghul / glocal.py
Created November 26, 2012 22:43
greenlet locals for greenlet >= 0.4.0
from greenlet import getcurrent
__all__ = ['local']
def _get_local_dict():
current = getcurrent()
s = '_%s__local_dict__' % current.__class__.__name__
if not hasattr(current, s):
@saghul
saghul / p.py
Created December 31, 2012 09:30
import pyuv
import signal
import socket
class SignalWaker(object):
def __init__(self):
# Based on Zope async.py: http://svn.zope.org/zc.ngi/trunk/src/zc/ngi/async.py
@saghul
saghul / i1.py
Created January 9, 2013 22:17
pyuv loop.stop() vs custom loop
import pyuv
N = 1000000
def idle_cb(handle):
handle.count += 1
if handle.count == N:
handle.loop.stop()