This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Finds the youtube video id from youtube urls or from | |
# youtube embeded code (the old one and the new one) | |
import re | |
exp = re.compile(r""" | |
( | |
youtu.be/|youtube(-nocookie)?.com/ | |
( | |
watch\?(.*&)?v=|(embed|v)/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile | |
import time | |
def profile_this(fn): | |
def profiled_fn(*args, **kwargs): | |
fpath = "%s-%s.profile" % (fn.__name__, round(time.time())) | |
prof = cProfile.Profile() | |
ret = prof.runcall(fn, *args, **kwargs) | |
prof.dump_stats(fpath) | |
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import close_connection | |
close_connection() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cPickle as pickle | |
from collections import OrderedDict | |
import logging | |
from django.conf import settings | |
from newcache import CacheClass as _CacheClass | |
# the cache block is set to 512KB because we are doing a serialization bellow | |
# and also another serialization is done by memcache. So the size of the data | |
# to be saved in cache is greater. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_concurrently(times): | |
""" | |
Add this decorator to small pieces of code that you want to test | |
concurrently to make sure they don't raise exceptions when run at the | |
same time. E.g., some Django views that do a SELECT and then a subsequent | |
INSERT might fail when the INSERT assumes that the data has not changed | |
since the SELECT. | |
""" | |
def test_concurrently_decorator(test_func): | |
def wrapper(*args, **kwargs): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zope.component as c | |
from zca import render, User, Lion, Kitty, Tiger, AnimalToID | |
from zca import TigerToID | |
c.provideAdapter(TigerToID) | |
c.provideAdapter(AnimalToID) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile | |
def do_cprofile(func): | |
def profiled_func(*args, **kwargs): | |
profile = cProfile.Profile() | |
try: | |
profile.enable() | |
result = func(*args, **kwargs) | |
profile.disable() | |
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- show running queries (pre 9.2) | |
SELECT procpid, age(query_start, clock_timestamp()), usename, current_query | |
FROM pg_stat_activity | |
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%' | |
ORDER BY query_start desc; | |
-- show running queries (9.2) | |
SELECT pid, age(query_start, clock_timestamp()), usename, query | |
FROM pg_stat_activity | |
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%' |