Skip to content

Instantly share code, notes, and snippets.

View davesque's full-sized avatar

David Sanders davesque

  • Seattle, WA
View GitHub Profile
@davesque
davesque / profile.py
Created September 20, 2013 21:58
Simple profiling context manager
import cProfile
import contextlib
import os
@contextlib.contextmanager
def profile(filename='~/python.profile', *args, **kwargs):
profile = cProfile.Profile(*args, **kwargs)
profile.enable()
@davesque
davesque / g
Created September 20, 2013 18:21
#!/usr/bin/env bash
if [[ $# -eq 0 ]]; then
git status
else
git $@
fi
int sum3(int x, int y, int z) {
int word1, word2;
word1 = (x ^ y) ^ z;
word2 = (x & y) | (x & z) | (y & z);
word2 <<= 1;
return word1 + word2;
}
@davesque
davesque / patcher_test_case.py
Created August 30, 2013 20:09
setUp/tearDown patcher pattern for python unittest and python mock
import unittest
from mock import call, patch
class PatcherTestCase(unittest.TestCase):
def setUp(self):
self.patcher = patch('some_module.some_object')
self.mock_object = self.patcher.start()
@davesque
davesque / .pythonrc.py
Last active December 18, 2015 01:59
Cleaned up .pythonrc.py file
from code import InteractiveConsole
import os
import sys
completer_locals = locals()
HISTFILE = os.path.expanduser("%s/.pyhistory" % os.environ["HOME"])
@davesque
davesque / functional.py
Created May 20, 2013 04:54
Simple illustration of recursion as it is sometimes used in functional languages
# Again, these are contrived examples, but they do a slightly better job of
# illustrating the use of recursion in functional languages when no concept of
# program state is available.
# Notice that the if statement in the functional example 1 includes an else
# statement. This makes it more like a mathematical expression (because it
# causes the function to always return a certain value). Functional example 2
# uses a syntax available in python that truly is an expression built with if
# and else statements.
@davesque
davesque / anagrams.py
Created April 16, 2013 20:08
My solution to the Thumbtack PyCon challenge 2013
#!/usr/bin/env python
import itertools
import sys
from collections import defaultdict
from pyparsing import CharsNotIn, Group, Optional, Word, ZeroOrMore, alphanums
word = Word(alphanums)
space = CharsNotIn(alphanums)
doc = ZeroOrMore(Group(word + Optional(space)))
@davesque
davesque / plane_timer.py
Created March 23, 2013 06:12
A little timer for international airplane flights
import sys
import time
from datetime import datetime, timedelta
START_TIME = datetime(2013, 3, 23, 0, 0, 0, 0)
TRIP_TIME = timedelta(minutes=8.7 * 60)
END_TIME = START_TIME + TRIP_TIME
BAR_WIDTH = 100
@davesque
davesque / rich_text_utils.py
Created March 12, 2013 04:07
Utilities for updating links in rich text content
"""
Utilities for dynamically updating links in rich text fields.
We had a project in which many models had rich text fields. These rich text
fields contained links to model instance detail pages. Since these links were
being entered by hand, we needed a way to update them in the event that the
instance for the detail page they were pointing to changed.
The routines in this file allow links in rich text content to be automatically
updated with a python html parsing library called `BeautifulSoup`.
@davesque
davesque / se.zsh
Created February 12, 2013 17:08
Grep wrapper
local base_cmd="grep --exclude-dir=CACHE --exclude-dir=ckeditor --binary-files=without-match --colour -Enr"
if [[ $# -eq 1 ]]; then
eval "$base_cmd $1 *"
elif [[ $# -gt 1 ]]; then
eval "$base_cmd $@"
else
echo "usage: se SEARCH_STRING [FILE_PATTERN]"
fi