Skip to content

Instantly share code, notes, and snippets.

View mvmocanu's full-sized avatar

Mihai Mocanu mvmocanu

View GitHub Profile
@mvmocanu
mvmocanu / yre.py
Created January 10, 2013 13:45
Youtube video ID from youtube urls or from youtube embeded code (the old one or the new one)
# 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)/
@mvmocanu
mvmocanu / gist:5141177
Last active December 14, 2015 20:08
Profile decorator Used for profiling a function. The generated profile can be viewed with: http://www.vrplumber.com/programming/runsnakerun/
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
@mvmocanu
mvmocanu / gist:5194683
Created March 19, 2013 09:11
How to close database connections in Django
from django.db import close_connection
close_connection()
@mvmocanu
mvmocanu / fat_cache.py
Last active December 15, 2015 10:18
How to cache large sets of data that are greater than memcache block size (which is around 1MB)
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.
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):
import zope.component as c
from zca import render, User, Lion, Kitty, Tiger, AnimalToID
from zca import TigerToID
c.provideAdapter(TigerToID)
c.provideAdapter(AnimalToID)
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
@mvmocanu
mvmocanu / postgres_queries_and_commands.sql
Last active April 1, 2021 08:29 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- 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%'