Skip to content

Instantly share code, notes, and snippets.

@dnozay
dnozay / gist:1525886
Created December 28, 2011 02:32
@cache_condition, @cache_last_modified, @cache_etag
# decorators to leverage cache based on conditional view processing.
# cache_condition decorator based on django.views.decorators.http.condition
# cache_last_modified, cache_etag shortcuts.
# settings.py
# -----------
# you may or may not want this.
# CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
@dnozay
dnozay / mockimporter.py
Created December 28, 2011 22:10
mock importer
# import hook (sys.meta_path) for mocking imports
# using mock library (http://www.voidspace.org.uk/python/mock/)
#
import sys
import mock
import logging
IMPORTER = None
MOCK = None
@dnozay
dnozay / find_dupes.py
Created December 28, 2011 22:16
find duplicate files based on their md5sum
#!/usr/bin/python
# find duplicate files based on their md5sum
import os
import hashlib
import mmap
def chunks(file_):
try:
map = mmap.mmap(file_.fileno(), 0)
@dnozay
dnozay / lcov_merge.py
Created December 28, 2011 22:24
merge LCOV file counters
#!/usr/bin/python
# merge LCOV file counters
import sys
filedict = {}
currentitem = {}
currentfile = ''
for line in sys.stdin:
subline = line[3:][:-1]
@dnozay
dnozay / pythonstartup.py
Created December 28, 2011 22:29
python startup file for autocomplete
# python startup file to toggle autocomplete on.
# e.g. export PYTHONSTARTUPFILE=pythonstartup.py
#
import readline
import rlcompleter
readline.parse_and_bind('tab: complete')
@dnozay
dnozay / gist:1530205
Created December 28, 2011 22:43
discover url patterns in django project
#
# discover all url patterns in django project
#
def discover_urls(urllist, parent):
for entry in urllist:
item = parent.setdefault(entry.regex.pattern, {})
if hasattr(entry, 'url_patterns'):
discover_urls(entry.url_patterns, item)
@dnozay
dnozay / procname.py
Created December 29, 2011 20:59
set/get process name (using ctypes & prctl)
# set/get process name (using ctypes & prctl)
# set_proc_name('python rocks')
# name = get_proc_name()
import ctypes
from ctypes.util import find_library
libc = ctypes.CDLL(find_library('c'))
PR_SET_NAME = 15
PR_GET_NAME = 16
@dnozay
dnozay / gist:1541581
Created December 30, 2011 21:37
django admin @register class decorator
# django admin @register class decorator, e.g.
# @register(Entry)
# class EntryAdmin(models.ModelAdmin):
# pass
def register(model):
def inner(admin_cls):
admin.site.register(model, admin_cls)
return admin_cls
return inner
@dnozay
dnozay / gist:1561381
Created January 4, 2012 18:33
AdminTestMixin for django testcases whose models expose admin interface.
# Mixin for checkin admin interface.
# To be used in a django testcase (using test client).
# e.g.
# from django.test import TestCase
# class EntryTest(AdminTestMixin, TestCase):
# pass
class AdminTestMixin(object):
def test_model_admin(self):
@dnozay
dnozay / httpd.conf
Created January 11, 2012 20:42
apache2 threads w/ 512K stack (default linux 8MB) + wsgi
# apache2 config tuning for linux (8MB default stack size)
# allows to run more threads, min stack size should depend on worse case scenario
#
#MaxClients 1000
#ThreadsPerChild 64
#MinSpareThreads 500
# default linux 8MB
#ThreadStackSize 8388608
#ThreadStackSize 4194304