Skip to content

Instantly share code, notes, and snippets.

@dnozay
dnozay / gist:1894258
Created February 23, 2012 18:35
Fixture-friendly DateTimeField w.r.t auto_now, auto_now_add
# Fixture-friendly DateTimeField w.r.t auto_now, auto_now_add
# >>> created_at = AutoDateTimeField(auto_now_add=True)
# >>> updated_at = AutoDateTimeField(auto_now=True)
# see the following links for context
# http://stackoverflow.com/questions/1737017/django-auto-now-and-auto-now-add
# http://groups.google.com/group/django-developers/browse_thread/thread/4cd631c225cb4e52
class AutoDateTimeField(models.DateTimeField):
'''Fixture friendly DateTimeField
@dnozay
dnozay / gist:1996782
Created March 7, 2012 22:35
ldap paged search
#!/usr/bin/python
# samples
# http://www.grotan.com/ldap/python-ldap-samples.html
# useraccountcontrol
# http://msdn.microsoft.com/en-us/library/windows/desktop/ms680832(v=vs.85).aspx
# paging search
# http://www.novell.com/coolsolutions/tip/18274.html
import ldap
from ldap.controls import SimplePagedResultsControl
@dnozay
dnozay / gist:2052823
Created March 16, 2012 21:25
dependency graphs (using heapq)
# dependency graphs.
# utilities to declare dependencies between items and get which ones to do first.
# this is using heapq for walking up a dependency chain.
from heapq import *
class DependencyGraph(object):
'''
Represent a dependency graph.
All nodes should have dependencies presented as a list of node names.
@dnozay
dnozay / gist:2287061
Created April 2, 2012 20:39
toposort
# http://en.wikipedia.org/wiki/Topological_sorting
# sort build targets thanks to dependency information.
#
def toposort(buildgraph):
'''
For topological sort:
An edge from x to y means x needs to be done before y.
This is the opposite of our graph where target A depends on a list of
components.
@dnozay
dnozay / p4_renumber.py
Created June 14, 2012 23:34
Renumber a perforce change via p4 python api.
# renumber a change.
# this will create a copy of the change form, without the files
# then reopen the files under the new change.
import argparse
import P4
def renumber(changenum):
handle = P4.P4()
handle.connect()
@dnozay
dnozay / gist:3869050
Created October 10, 2012 22:53
Prepare CentOS - for webserver / django / nginx / gunicorn use.
#!/bin/bash
# please run this as root. script provided "AS IS".
#.--------------------------------------------------------------------------------
#. install python 2.7.x
#. https://github.com/scalp42/python-2.7.x-on-Centos-5.x - Apache License
#.--------------------------------------------------------------------------------
pushd /tmp
https_proxy=$http_proxy wget https://raw.github.com/scalp42/python-2.7.x-on-Centos-5.x/master/install_python27.sh
bash ./install_python27.sh
@dnozay
dnozay / example.py
Created December 5, 2012 19:17
Unit testing code using 'requests' with the 'fudge' library.
# this is the example code with the what we need to test.
import requests
def function_under_test():
'''return True if www.example.com is accessible'''
alive = False
try:
response = requests.get('http://www.example.com')
response.raise_for_status()
alive = True
@dnozay
dnozay / gist:4965322
Created February 16, 2013 03:02
prettytable and textwrap to format a dictionary (e.g. os.environ)
# some simple function that uses prettytable and textwrap to format a dictionary
# http://code.google.com/p/prettytable/wiki/Tutorial
from prettytable import PrettyTable
from textwrap import wrap
VAL_WRAP_WIDTH = 60
def pretty_dictionary(dic):
tab = PrettyTable(['key', 'value'])
# get mysql table sizes
# based on http://stackoverflow.com/questions/9620198
SELECT
concat(table_schema, '.', table_name) AS "Table",
table_rows AS "number of rows",
round((data_length / 1024 / 1024), 2) "Data in MB",
round((index_length / 1024 / 1024), 2) "Indexes in MB"
FROM information_schema.TABLES;
@dnozay
dnozay / distutils.patch
Last active April 12, 2017 16:22
installing pypy on 64bit centos 6.4
--- /usr/lib64/pypy-1.9/lib-python/2.7/distutils/unixccompiler.py 2012-06-15 11:33:20.000000000 -0700
+++ /tmp/unixccompiler.py 2013-06-17 12:39:10.212783192 -0700
@@ -297,7 +297,9 @@
# this time, there's no way to determine this information from
# the configuration data stored in the Python installation, so
# we use this hack.
- compiler = os.path.basename(sysconfig.get_config_var("CC"))
+ # XXX: http://bugs.python.org/issue14030
+ # compiler = os.path.basename(sysconfig.get_config_var("CC"))
+ compiler = os.path.basename(self.compiler[0])