Skip to content

Instantly share code, notes, and snippets.

View niwinz's full-sized avatar

Andrey Antukh niwinz

View GitHub Profile
@niwinz
niwinz / async_psycopg2.py
Created April 19, 2012 20:53 — forked from FSX/async_psycopg2.py
A module for asynchronous PostgreSQL queries in Tornado.
#!/usr/bin/env python
__author__ = 'Frank Smit <frank@61924.nl>'
__version__ = '0.1.0'
import functools
import psycopg2
from tornado.ioloop import IOLoop, PeriodicCallback
@niwinz
niwinz / output.txt
Created April 20, 2012 19:04
Mini SSH library for python3 based on subprocess
[niwi@vaio.niwi.be][~]% python3 subprocess_ssh.py
(0, b'Linux vaio.niwi.be 3.3.2-1-ARCH #1 SMP PREEMPT Sat Apr 14 09:48:37 CEST 2012 x86_64 Intel(R) Core(TM)2 Duo CPU T6600 @ 2.20GHz GenuineIntel GNU/Linux')
(0, b'processor\t: 0\nvendor_id\t: GenuineIntel\ncpu family\t: 6\nmodel\t\t: 23\nmodel name\t: Intel(R) Core(TM)2 Duo CPU T6600 @ 2.20GHz\nstepping\t: 10\nmicrocode\t: 0xa07\ncpu MHz\t\t: 2193.716\ncache size\t: 2048 KB\nphysical id\t: 0\nsiblings\t: 2\ncore id\t\t: 0\ncpu cores\t: 2\napicid\t\t: 0\ninitial apicid\t: 0\nfpu\t\t: yes\nfpu_exception\t: yes\ncpuid level\t: 13\nwp\t\t: yes\nflags\t\t: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good nopl aperfmperf pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm dts\nbogomips\t: 4389.24\nclflush size\t: 64\ncache_alignment\t: 64\naddress sizes\t: 36 bits physical, 48 bits virtual\npower management:\n
@niwinz
niwinz / django_interface.py
Created April 25, 2012 21:30
Django websockets with tornado, gevent and zeromq
# -*- coding: utf-8 -*-
from gevent import monkey; monkey.patch_all()
import gevent
from gevent_zeromq import zmq
class WebSocketHandler(object):
def __init__(self, _id, in_queue, socket):
@niwinz
niwinz / geventfd.py
Created June 16, 2012 07:57 — forked from wolever/geventfd.py
Simple wrapper around a file descriptor which will perform non-blocking reads/writes using gevent
import os
import fcntl
from gevent.core import wait_read, wait_write
class GeventFD(object):
""" Wrap a file descriptor so it can be used for non-blocking reads and writes with gevent.
>>> stdin = GeventFD(sys.stdin.fileno())
>>> stdin.read(5)
'hello'
@niwinz
niwinz / sigdispatch.py
Created June 27, 2012 14:08
Correct monky patching of django User.
# -*- coding: utf-8 -*-
from django.db.models import signals
def model_class_prepared(sender, **kwargs):
if sender._meta.app_label == 'auth' and sender._meta.module_name == 'user':
print "Monky patching user..."
email_field = sender._meta.get_field("email")
email_field.max_length = 200
email_field._unique = True
@niwinz
niwinz / irc.js
Created July 4, 2012 22:37
Evented Irc Client/lib on NodeJS
function Irc(host, port, nick){
this._host = host;
this._port = port;
this._nick = nick;
}
Irc.prototype.setChannels = function(channels) {
this._chans = channels;
}
@niwinz
niwinz / gist:3049907
Created July 4, 2012 22:39
Cache decorator for view methods (django)
from django.core.cache import cache
def cacheable(cache_key, timeout=3600):
def paramed_decorator(func):
def decorated(self):
key = cache_key % self.__dict__
res = cache.get(key)
if res == None:
res = func(self)
cache.set(key, res, timeout)
@niwinz
niwinz / solution1.py
Created July 4, 2012 22:43
kata: make list of list as plain list of elements
#from functools import reduce # uncomment if python3+ is used
lmb = lambda x, y: x+reduce(lmb, y, []) if isinstance(y, list) else x + [y]
reduce(lmb,[1,['a','b'],2,[[1,2, [3]]],'c'], [])
@niwinz
niwinz / Console result
Created July 6, 2012 17:08
Sqlite3 isolation_level and savepoint suport test
[3/4.3.17]niwi@vaio:~> python2 savepoint_sqlite3_test.py
python version: 2.7.3 (default, Apr 24 2012, 00:00:54)
[GCC 4.7.0 20120414 (prerelease)]
PySQLite version: 2.6.0
sqlite3 version: 3.7.13
Test with isolation_level = None (autocommit mode)
**********************************************************************
ROW: (0, 200)
@niwinz
niwinz / gist:3198204
Created July 29, 2012 12:02
Django model hooks

Django Model Hooks (prototype)

System hooks to insert raw sql.

References on django-developers google group:

* https://groups.google.com/forum/?fromgroups#!topic/django-developers/xN0aMzrmA1Y
* https://groups.google.com/forum/?fromgroups#!topic/django-developers/KhsGj1b6rnU