Skip to content

Instantly share code, notes, and snippets.

View cpdean's full-sized avatar

Conrad cpdean

View GitHub Profile
import pickle
path = "temp.db"
def get_db():
#touch the file. if it didn't exist, return empty dict
with open(path, 'a+') as f:
if len(f.read()) == 0:
print "file was empty"
@cpdean
cpdean / filters.py
Last active December 14, 2015 23:39 — forked from rouge8/filters.py
# nope
# combines several repositories into one,
# taking care to combine all trunks naiively,
# and all branches intelligently
import sys
import pysvn
import functools
def indempotent(f):
@cpdean
cpdean / segfault.py
Created June 14, 2013 20:24
demonstrates a segfault on python 2.7.3 and python 3.2.3
import itertools, timeit
import sys
if sys.version_info < (3,):
range = xrange
a = (i for i in range(11**7)); b,c = itertools.tee(a)
print(timeit.timeit(lambda : sum(b), number=1))
# this line segfaults:
a = (i for i in range(11**7)); b,c = itertools.tee(a)
@cpdean
cpdean / .vimrc
Created June 18, 2013 14:34
basic windows 7 gvim settings
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
set backupdir=~\vim\tmp\
set directory=~\vim\tmp\
set gfn=Ubuntu_Mono_for_Powerline:h14:cANSI
set bg=dark
colorscheme desert
#!/bin/sh
# install df dependencies for ubuntu precise32
# download and unzip df tar.bz2
# manipulate config file for text mode
sudo apt-get update -qq
sudo apt-get install libgtk2.0-0 libsdl-image1.2 libsdl-ttf2.0-0 libglu1-mesa -qq
cd /home/vagrant/
if [ ! -f df_34_11_linux.tar.bz2 ]
@cpdean
cpdean / profile_it.py
Created June 27, 2013 00:17
simple profiling decorator with an optional comment string
import functools
import logging
import time
logging.basicConfig(level=logging.INFO)
def profile(comment=""):
def time_part(f):
@functools.wraps(f)
@cpdean
cpdean / apache.wsgi
Created June 27, 2013 23:38
wsgi apache setup with error logging and virtualenv
import sys
sys.stdout = sys.stderr
sys.path.insert(0,'/home/user/public/appname.example.com/public/appname')
activate_this = '/home/user/public/appname.example.com/public/appname/appvirtualenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
from app import app as application
@cpdean
cpdean / README.markdown
Last active December 19, 2015 03:38
Comparing the performance between two interleave implementations.

When you want to combine iterables, the common way you write out list comprehensions and list operations wastes a lot of memory. For an easy performance speed up, try switching to generators/iterators.

They're data structures that are computed lazily as their elements are requested. That way you can chain together list operations like combining, interleaving, and slicing without needing to store the sequence in memory.

You lose flexibility, since you can't use subscripts or slice syntax, but your gains from avoiding python memory allocation can be pretty huge.

This is how mock.patch should be used, but I'm having some problems on my own codebase.

It's weird, but in this tiny example everything works just fine...