Skip to content

Instantly share code, notes, and snippets.

@saghul
saghul / zmq_websocket_gateway.py
Created June 28, 2011 18:49
A ZeroMQ - WebSocket gateway
# coding=utf8
# Copyright (C) 2011 Saúl Ibarra Corretgé <[email protected]>
#
import hashlib
import os
import re
import socket
import struct
@saghul
saghul / pyqt_opencv.py
Created June 29, 2011 22:22
Render OpenCV video on a PyQt widget
# coding=utf8
# Copyright (C) 2011 Saúl Ibarra Corretgé <[email protected]>
#
# Some inspiration taken from: http://www.morethantechnical.com/2009/03/05/qt-opencv-combined-for-face-detecting-qwidgets/
import cv
import sys
@saghul
saghul / gist:1114589
Created July 29, 2011 19:54
Don't forget __ne__
In [1]: class Foo(object):
...: def __init__(self, bar):
...: self.bar = bar
...: def __eq__(self, other):
...: return self.bar == other.bar
...:
...:
In [2]: Foo('baz') == Foo('baz')
Out[2]: True
@saghul
saghul / foo.c
Created August 2, 2011 21:25
Example Python C extension module
#include "Python.h"
#include "structmember.h"
/* Type definition */
typedef struct {
PyObject_HEAD
PyObject *bar;
PyObject *baz;
} Foo;
@saghul
saghul / gist:1124655
Created August 4, 2011 07:30
Check Python architecture reliably
# http://mail.python.org/pipermail/pythonmac-sig/2009-September/021649.html
# On Mac OSX Python may run in 32 or 64 bit mode so platform.architecture() is not reliable.
# Example: use 32bit Python: export VERSIONER_PYTHON_PREFER_32_BIT=yes
def arch():
import ctypes
return {4: "i386", 8: "x86_64"}[ctypes.sizeof(ctypes.c_size_t)]
@saghul
saghul / opensips-scket-sender.py
Created August 22, 2011 21:53
OpenSIPS socket sender with Twisted
# coding=utf-8
# Copyright (C) 2011 Saúl Ibarra Corretgé <[email protected]>
#
# Based on callcontrol/opensips.py, Copyright (C) 2006-2011 AG Projects.
# OpenSIPS configuration example:
# loadmodule "mi_datagram.so"
# modparam("mi_datagram", "socket_name", "/var/run/opensips/socket")
@saghul
saghul / helloworld.py
Created August 28, 2011 22:38
Hello world Plivo example using Flask
############################################
# host/port binding for http server
HOST = '0.0.0.0'
PORT = 5000
############################################
from flask import Flask, render_template
import plivohelper
import os
@saghul
saghul / test_localmeta.py
Created August 31, 2011 17:54
Thread local and metaclass experiment
import threading
class ThreadLocalMeta(type):
"""Metaclass to keep a single class instance per thread"""
_local = threading.local()
def __call__(cls, *args, **kwargs):
@saghul
saghul / consumer_tornadio.py
Created September 16, 2011 17:13
A ZeroMQ - SocketIO gateway using TornadIO
import os
import zmq
from zmq.eventloop import ioloop, zmqstream
ioloop.install()
import tornado
import tornado.web
import tornadio
import tornadio.router
@saghul
saghul / test_assert.c
Created September 22, 2011 21:06
Custom assertion macro that is not disabled by NDEBUG
#include <stdio.h>
#include <stdlib.h>
#define ASSERT(x) \
do { \
if (!(x)) { \
fprintf (stderr, "%s:%u: %s: Assertion `" #x "' failed.\n", \
__FILE__, __LINE__, __func__); \