Skip to content

Instantly share code, notes, and snippets.

@dnozay
dnozay / gist:1525886
Created Dec 28, 2011
@cache_condition, @cache_last_modified, @cache_etag
View gist:1525886
# 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
View mockimporter.py
# 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 Dec 28, 2011
find duplicate files based on their md5sum
View find_dupes.py
#!/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 Dec 28, 2011
merge LCOV file counters
View lcov_merge.py
#!/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 Dec 28, 2011
python startup file for autocomplete
View pythonstartup.py
# 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 Dec 28, 2011
discover url patterns in django project
View gist:1530205
#
# 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 Dec 29, 2011
set/get process name (using ctypes & prctl)
View procname.py
# 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 Dec 30, 2011
django admin @register class decorator
View gist:1541581
# 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 Jan 4, 2012
AdminTestMixin for django testcases whose models expose admin interface.
View gist:1561381
# 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 Jan 11, 2012
apache2 threads w/ 512K stack (default linux 8MB) + wsgi
View httpd.conf
# 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