Skip to content

Instantly share code, notes, and snippets.

View sergray's full-sized avatar

Sergey Panfilov sergray

View GitHub Profile
@sergray
sergray / gist:649788
Created October 27, 2010 19:39
legacy antigravity
>>> import antigravity
>>> antigravity.start()
'http://xkcd.com/353/'
# the source of http://pypi.python.org/pypi/antigravity/0.1
# is plain and there is no any magic of Python
#!/usr/bin/env python
STRIP_URL = "http://xkcd.com/353/"
@sergray
sergray / gist:649833
Created October 27, 2010 20:00
proposed antigravity
__doc__ = "Add fly function to namespace where module is imported"
class RealWorldLevitation(Exception):
pass
def expose(obj):
import inspect
frame = inspect.currentframe()
frame.f_back.f_back.f_globals[obj.__name__] = obj
return obj
@sergray
sergray / remem.py
Created January 17, 2011 17:59
Extend arbitrary interactive unix command with readline features
import sys
import subprocess
import readline
import select
import os
class Popen(subprocess.Popen):
def _communicate(self, input):
read_set = []
@sergray
sergray / gist:786384
Created January 19, 2011 16:17
Amazing Django ORM extra SQL for Postgres
>>> tasks = Task.objects.\
... extra(select = {'t': "date_trunc('minute', task_task.date_created)"}).\
... values('t').annotate(Count('id')).order_by('t')
>>> print "\n".join("%(t)s | %(id__count)d" % t for t in tasks)
2011-01-19 11:26:00 | 767
2011-01-19 11:27:00 | 613
2011-01-19 11:28:00 | 522
2011-01-19 11:29:00 | 721
2011-01-19 11:30:00 | 701
2011-01-19 11:31:00 | 679
@sergray
sergray / .vimrc
Created February 4, 2011 16:41
add tree of directories to vim path
python << END_PATH
import os
import vim
walker = os.walk(os.getcwd())
dir_list = ['.']
for root, dirs, paths in walker:
dir_list += map(lambda d: os.path.join(root, d), dirs)
vim.command('set path=%s' % ','.join(dir_list))
END_PATH
@sergray
sergray / get_jobs.py
Created March 14, 2011 13:38
Tracing SQL queries of Django Command.handle with trace_sql decorator
class SQLDebugCommand(object):
def __init__(self):
from devserver.modules.sql import SQLSummaryModule, SQLRealTimeModule
from devserver.logger import GenericLogger
from mock import Mock
self.modules = [
#SQLRealTimeModule(GenericLogger(SQLRealTimeModule)),
SQLSummaryModule(GenericLogger(SQLSummaryModule)),
]
self.mock_request = Mock()
@sergray
sergray / gist:881008
Created March 22, 2011 10:02
traceback of UnicodeDecodeError in pyredis transport of kombu
Traceback (most recent call last):
...
File "ve/lib/python2.6/site-packages/celery/task/base.py", line 320, in delay
return self.apply_async(args, kwargs)
File "ve/lib/python2.6/site-packages/celery/task/base.py", line 442, in apply_async
**options)
File "ve/lib/python2.6/site-packages/celery/app/amqp.py", line 230, in delay_task
send(body, exchange=exchange, **extract_msg_options(kwargs))
File "ve/lib/python2.6/site-packages/kombu/connection.py", line 196, in _insured
return fun(*args, **kwargs)
@sergray
sergray / gist:933846
Created April 21, 2011 06:24
Django 1.2.5 db.connection.cursor classes
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
IPython 0.10 -- An enhanced Interactive Python.
In [1]: from django.conf import settings
In [2]: settings.DEBUG
Out[2]: False
In [3]: from django.db import connection
@sergray
sergray / odesk_task_hours.py
Created September 1, 2011 15:46
Python console application which calculates hours spent on task given in argument
import sys
import odesk
from odesk import Q
from datetime import datetime, timedelta
import shelve
import os
# add your desktop app keys
public_key = ''
secret_key = ''
@sergray
sergray / mongolyze.py
Created February 21, 2012 19:41
Python script for automated analysis of slow queries in mongodb
"""
Script for automated analysis of profiling data in MongoDB,
gathered by Mongo with db.setProfilingLevel(1).
See <http://www.mongodb.org/display/DOCS/Database+Profiler>
TODO: pass collection and database with profiling data in arguments
TODO: make thread-safe
TODO: handle map-reduce operations
"""