Skip to content

Instantly share code, notes, and snippets.

View niwinz's full-sized avatar

Andrey Antukh niwinz

View GitHub Profile
@niwinz
niwinz / psql.py
Created August 28, 2012 07:22 — forked from fcurella/psql.py
postgres PubSub vs Redis
#!/usr/bin/env python
import select
import time
import psycopg2
import psycopg2.extensions
import sys
def get_cursor():
conn = psycopg2.connect("dbname=pgpubsub")
@niwinz
niwinz / dinamic_inheritance.py
Created September 6, 2012 10:24
Dynamic Inheritance with python3
class DynamicInheritance(type):
"""
Dinamicaly modify class with some extra mixins.
"""
def __call__(cls, *args, **kwargs):
_mixins = kwargs.pop("_mixins", None)
if _mixins:
assert isinstance(_mixins, tuple), "_mixin patemeter must be a tuple"
@niwinz
niwinz / test-tuple.cpp
Created September 15, 2012 19:42
Boost.Python converter for C++ tuple
// Compile with:
// clang++ -std=c++11 -shared -l boost_python3 -I /usr/include/python3.2mu -fPIC -o bptuple.so tuple-test.cpp
#include <tuple>
#include <string>
#include <boost/python.hpp>
namespace py = boost::python;
using std::string;
@niwinz
niwinz / gist:3818659
Created October 2, 2012 12:30
What is the simplest way of using Python pdb to inspect the cause of an unhandled exception?
import functools
import pdb
def debug_on(*exceptions):
if not exceptions:
exceptions = (AssertionError, )
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
try:
@niwinz
niwinz / bench.py
Created October 2, 2012 21:17
PostgreSQL int array slice benchmark (with postgresql arrays and bytea fields)
from __future__ import print_function
import timeit
import array
import struct
import psycopg2 as pg
connection = pg.connect(host="localhost", dbname="test")
[3/5.0.0]niwi@vaio:~/wrk> ab -n 10000 -c 2 http://127.0.0.1:8000/section0/feature0
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
@niwinz
niwinz / gist:3868290
Created October 10, 2012 20:42
other
(env)[3/5.0.0]niwi@vaio:~/helloworld/02-routing> python benchmark.py
[...]
static[-1] msec rps tcalls funcs
bottle 8056 12413 64 34
django 263270 380 1733 70
[...]
@niwinz
niwinz / array_order.sql
Created October 21, 2012 09:24
Array order with postgres
CREATE OR REPLACE FUNCTION idx(anyarray, anyelement)
RETURNS int AS
$$
SELECT i FROM (
SELECT generate_series(array_lower($1,1),array_upper($1,1))
) g(i)
WHERE $1[i] = $2
LIMIT 1;
$$ LANGUAGE sql IMMUTABLE;
@niwinz
niwinz / hl7.py
Created November 18, 2012 16:06
HL7 Prototype
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function
import io
SEGMENT_SEPARATOR = b'\r'
SECTION_SEPARATOR = b'^'
FIELD_SEPARATOR = b'|'
@niwinz
niwinz / urls.py
Created December 18, 2012 18:54
Simple serve media files with django dev server.
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
[...] # main urls
)
def mediafiles_urlpatterns():