Skip to content

Instantly share code, notes, and snippets.

Avatar

Mihai Mocanu mvmocanu

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