Skip to content

Instantly share code, notes, and snippets.

View sergray's full-sized avatar

Sergey Panfilov sergray

View GitHub Profile
@sergray
sergray / snippet.js
Created April 14, 2012 07:45 — forked from necolas/snippet.js
Optimised async loading of cross-domain scripts
/*
* Updated to use the function-based method described in http://www.phpied.com/social-button-bffs/
* Better handling of scripts without supplied ids.
*
* N.B. Be sure to include Google Analytics's _gaq and Facebook's fbAsyncInit prior to this function.
*/
(function(doc, script) {
var js,
fjs = doc.getElementsByTagName(script)[0],
@sergray
sergray / TEDTranscript.js
Created April 7, 2012 15:25
Returns the full transcript of TED talk (copy&paste snippet to developer's console on page with TED talk)
(function() {
var transcript = "";
$('#transcriptText p').each(function(i, para) {
if(i > 0) transcript += "\n";
$('.transcriptLink', para).each(function(j, link) {
if(j > 0) transcript += " ";
transcript += link.text;
});
});
return transcript;
@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
"""
@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 / 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 / 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 / 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 / .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 / 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 / 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 = []