Skip to content

Instantly share code, notes, and snippets.

View reclosedev's full-sized avatar

Roman Haritonov reclosedev

View GitHub Profile
@reclosedev
reclosedev / new_order.py
Last active May 13, 2016 13:39
Magical decorator to convert dicts declarations to ordered dict. Don't ever use it in your code.
"""
Module contains ``order_dicts`` decorator. When used, all dicts declarations are converted to OrderedDict. Example::
from new_order import order_dicts
@order_dicts
def func():
d1 = {1: 2, 3: 4, 5: 6}
d2 = {"test": 2 ** 42, "foo": "bar", "baz": d1}
print("What do we have here? %s" % d1)
import resource
from cassandra.cluster import Cluster
def create_and_shutdown():
cluster = Cluster()
session = cluster.connect()
session.shutdown()
cluster.shutdown()
@reclosedev
reclosedev / singleton_scheduler.py
Last active October 23, 2015 22:52
Singleton Scheduler for celery-beat with Redis Sentinel
from os import urandom
from celery.beat import PersistentScheduler
from some_place import create_readis_connection # !
class SingletonScheduler(PersistentScheduler):
def __init__(self, *args, **kwargs):
super(SingletonScheduler, self).__init__(*args, **kwargs)
@reclosedev
reclosedev / celery_sentinel.py
Last active April 24, 2019 00:01
Temporary hack. Redis Sentinel support for Celery.
"""
This module adds Redis Sentinel transport support to Celery.
Current version of celery doesn't support Redis sentinel client, which is must have for automatic failover.
To use it::
import register_celery_alias
register_celery_alias("redis-sentinel")
celery = Celery(..., broker="redis-sentinel://...", backend="redis-sentinel://...")
@reclosedev
reclosedev / remote_file.py
Last active December 19, 2015 07:49
Seekable remote stream. Can be used to obtain listing (or download selected files) from huge .zip archives (or another structured file) from internet without downloading whole file. Requires Requests library.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
class RemoteFile(object):
def __init__(self, url, session=None):
self._url = url
self._session = session or requests.Session()
self._need_to_close_session = session is None
@reclosedev
reclosedev / parsimonious_json.py
Last active August 29, 2018 19:13
JSON parser implementation using https://github.com/erikrose/parsimonious + simple demo and benchmark.
# -*- coding: utf-8 -*-
import ast
from parsimonious.grammar import Grammar
from parsimonious.nodes import NodeVisitor
json_syntax = r'''
json_file = ws? json ws?
json = object / array
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Based on http://www.marsohod.org/index.php/projects/plata1/205-carctrlandr
import os
import BaseHTTPServer
from threading import Thread
import urlparse
import time
import string
@reclosedev
reclosedev / lockfile.py
Created June 30, 2012 21:28 — forked from ionrock/lockfile.py
A file locking example
"""
A file lock implementation that tries to avoid platform specific
issues. It is inspired by a whole bunch of different implementations
listed below.
- https://bitbucket.org/jaraco/yg.lockfile/src/6c448dcbf6e5/yg/lockfile/__init__.py
- http://svn.zope.org/zc.lockfile/trunk/src/zc/lockfile/__init__.py?rev=121133&view=markup
- http://stackoverflow.com/questions/489861/locking-a-file-in-python
- http://www.evanfosmark.com/2009/01/cross-platform-file-locking-support-in-python/
- http://packages.python.org/lockfile/lockfile.html
@reclosedev
reclosedev / benchmark_super.py
Created May 8, 2012 11:35
Benchmark Python super() function
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import timeit
import sys
class A(object):
"""Original"""
def x(self):
return 2
@reclosedev
reclosedev / transliterator.py
Created January 12, 2012 15:00
Implementation of transliteration class, as a simple state machine, with posibility to call it from one-key-prees events. E.g for implementing QTextEdit with translit
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# http://stackoverflow.com/a/8785689/1052325
class SMTransliterator(object):
two_letters = {u'Ch': u'Ч', u'yo': u'ё', u'Yo': u'Ё', u'Ya': u'Я', u'yu': u'ю',
u'sh': u'щ', u'ch': u'ч', u'ya': u'я', u'Sh': u'Щ', u'zh': u'ж', u'Yu': u'Ю', }
two_letters_set = set([s[0] for s in two_letters])