Skip to content

Instantly share code, notes, and snippets.

View kmerenkov's full-sized avatar

Konstantin Merenkov kmerenkov

  • Klarna
  • Berlin, Germany
View GitHub Profile
@kmerenkov
kmerenkov / comp.py
Created June 21, 2013 05:06
Inspired by clojure/comp
fs = [lambda x: x*x, lambda y: y+y, lambda z: str(z)]
comp = lambda fs, arg: reduce(lambda x, y: y(x), fs, arg)
comp(fs, 5) # returns '50', i.e. str((5*5)+(5*5))
@kmerenkov
kmerenkov / pass_result_to.py
Created January 31, 2013 13:43
Няшный job-security декоратор
from functools import partial
def pass_result_to(wrap_with):
def wrapper(f):
def wrapper_(*args, **kwargs):
return wrap_with(f(*args, **kwargs))
return wrapper_
return wrapper
@kmerenkov
kmerenkov / gist:3874555
Created October 11, 2012 18:32
Way to call a method on all parents
# -*- coding: utf-8 -*-
def maybe_call_super(cls, instance, method):
next_mro_obj = super(cls, instance)
method = getattr(next_mro_obj, method, None)
if method:
method()
@kmerenkov
kmerenkov / gist:2209258
Created March 26, 2012 20:02
Magic values template
class MagicValue(object):
def __eq__(self, obj):
return type(self) is type(obj)
def __nonzero__(self):
return False
def __hash__(self):
return id(self)
@kmerenkov
kmerenkov / connection.py
Created March 6, 2012 17:10
Соединятор с редисом, можно брать соединения для read+write или для readonly операций.
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
import itertools
import redis
import logging
from django.core.cache import cache
log = logging.getLogger('db.connection')
$ cat n_line.py
# -*- coding: utf-8 -*-
from itertools import islice
from cStringIO import StringIO
_content = """1st line
2nd line
3rd line
4th line
5th line"""
@kmerenkov
kmerenkov / gist:1085374
Created July 15, 2011 19:32
redis-server.init: init script for redis-server
#! /bin/sh
### BEGIN INIT INFO
# Provides: redis-server
# Required-Start: $syslog
# Required-Stop: $syslog
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db
@kmerenkov
kmerenkov / Random phone number
Created February 7, 2011 15:04
Returns random phone number
import random
from itertools import islice
def dev_urandom():
while True:
yield random.randint(0, 9)
def random_phone_number():
return '+7' + "".join(map(str, islice(dev_urandom(), 0, 10)))
@kmerenkov
kmerenkov / bzr-grep-with-colors.diff
Created August 4, 2010 04:53
Colorful output from bzr grep plugin
=== modified file '__init__.py'
--- __init__.py 2008-01-20 20:07:02 +0000
+++ __init__.py 2010-08-04 04:51:49 +0000
@@ -141,11 +141,11 @@
# bzr grep foo -- -i file
# and pass the -i through to grep.
+ files = []
+ options = [u'--color=always']
+
# -*- coding: utf-8 -*-
class A(object):
def do(self, x):
print ' A.foo(%s)' % (x,)
class B(object):
def do(self, x):
print ' B.foo(%s)' % (x,)