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 / 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 / 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()
type: map_type | base_type
map_type:
'map' '<' base_type ',' 'map' '<' base_type ',' type '>>' |
'map' '<' base_type ',' type '>'
base_type: NAME
@davesque
davesque / watchfiles.sh
Created September 18, 2019 18:39
Portable file watcher in bash.
#!/usr/bin/env bash
_usage() {
printf 'usage: %s [-p|--poll <sec per poll>] [-a|--all] [<find args>] -- <command> [<args>]\n' "$(basename "$0")" >&2
exit 64
}
# Default arg values
sec_per_poll=2
find_all=false
@davesque
davesque / vassal.ini
Created January 7, 2013 18:33
Uwsgi ini file for django
[uwsgi]
socket = /tmp/example.com.sock
; Worker processes
master = 1
processes = 4
; Virtualenv and home directory
virtualenv = /var/virtualenvs/example.com
chdir = /var/www/example.com
@davesque
davesque / json_tool.py
Created June 19, 2018 21:13
A little script for manipulating JSON
#!/usr/bin/env python3
import argparse
import json
import sys
parser = argparse.ArgumentParser(description='Manipulate JSON documents.')
parser.add_argument(
'-i', '--indent',
metavar='N',
@davesque
davesque / Screen Shot 2018-04-26 at 3.27.34 PM.png
Last active April 26, 2018 22:02
Circle CI migration steps
Screen Shot 2018-04-26 at 3.27.34 PM.png
import parsimonious
parser = parsimonious.Grammar(r"""
type = tuple_type / basic_type
tuple_type = "(" type next_type* ")"
next_type = "," type
basic_type = base sub? arrlist?
import parsimonious
parser = parsimonious.Grammar(r"""
type = tuple_type / basic_type
tuple_type = "(" type ("," type)* ")"
basic_type = base sub? arrlist?
base = alphas
Python 3.6.4 (default, Mar 14 2018, 11:02:01)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from functools import lru_cache
In [2]: class Adder:
...: def __init__(self, n):
...: self.n = n
...: @lru_cache()