Skip to content

Instantly share code, notes, and snippets.

View kstrauser's full-sized avatar

Kirk Strauser kstrauser

View GitHub Profile
@kstrauser
kstrauser / substrcomparatormaker.py
Created October 4, 2011 22:35
Decorator to help with legacy database that aren't 1NF
def substrcomparatormaker(column, i, j=None):
"""Return a comparator that tests for the substring of 'column'
from 'i' to 'j', as specified with Python slice values. This
means setting the start column to i + 1 because Python is 0-based
and SQL is 1-based, and setting the length to j - i.
Please don't create new tables that use this! It's only for legacy,
unnormalized data where multiple values are lumped together in a single
column.
@kstrauser
kstrauser / gist:1300151
Created October 20, 2011 01:11
Python unittest for a named object's existence

I use this decorator in unittest modules to verify that a particularly-named object exists:

def existsin(namespace):
    """Given a namespace, this decorator asserts that an object with
    the same name as the function it's wrapping (minus ) exists in that
    namespace"""
    def wrapper(namedunittest):
        assert namedunittest.__name__.startswith('test_'), \
            "The wrapped '%s' function doesn't look like a unit test" % namedunittest.__name__
@kstrauser
kstrauser / freezewheels.py
Created August 29, 2013 17:45
This works like a hypothetical "pip freezewheels" that makes wheels of all installed packages.
#!/usr/bin/env python
"""
Build wheels of all currently installed packages (as listed by "pip freeze")
"""
import glob
import importlib
from subprocess import call

Keybase proof

I hereby claim:

  • I am kstrauser on github.
  • I am ks (https://keybase.io/ks) on keybase.
  • I have a public key whose fingerprint is 19CF F48E 5C06 16B4 4A15 1E80 54D9 906A 294B 7FF3

To claim this, I am signing this object:

from itertools import count
def fizz():
for i in count(1):
yield '' if i % 3 else 'fizz'
def buzz():
for i in count(1):
yield '' if i % 5 else 'buzz'
@kstrauser
kstrauser / mocked.py
Last active March 25, 2017 01:31
Use assert functions instead of methods to avoid insidious test problems
>>> import mock
>>> m = mock.Mock()
>>> m.assert_called_once() # Old version of Mock? This may not be what you expect.
<Mock name='mock.assert_called_once()' id='4558624144'>
>>> assert_called_once = mock.Mock.assert_called_once # Doesn't exist. Now you know!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'Mock' has no attribute 'assert_called_once'
@kstrauser
kstrauser / ghost2wp.py
Last active August 2, 2018 07:20 — forked from tzangms/ghost2wp.py
Migrate Ghost to Wordpress
"""
Requirements:
* A Wordpress Blog
* Ghost export file (json).
* Python Packages: python-wordpress-xmlrpc
>>> pip install python-wordpress-xmlrpc
WARNING:
@kstrauser
kstrauser / Export to OmniFocus.scpt
Last active April 12, 2021 13:08
Export an OmniOutliner document as a set of OmniFocus actions
set isdate to (display dialog "When are you traveling?" default answer (current date)'s date string)'s text returned
tell date isdate to set travelDate to date (((its month as integer) & "/" & day & "/" & year) as text)
set lastOf to {}
tell application "OmniOutliner"
set theDoc to front document
-- Find the When and Context column numbers
set contextColumnNum to 0
@kstrauser
kstrauser / workon.sh
Created December 6, 2017 18:31
Cheap hack to emulate virtualenvwrapper's workon script
function workon () {
# Find the configured virtualenvwrapper home, or use Kirk's
# clearly excellent choice.
if [[ -z $PROJECT_HOME ]]; then
envs=$PROJECT_HOME
else
envs=~/Envs
fi
# Override the paths for specific environments
@kstrauser
kstrauser / black.md
Last active March 18, 2023 00:24
Using the "black" Python formatter in VS Code

This is how to use the Black Python code formatter in VS Code.

Make a Python 3.6 virtualenv for running Black

Black itself requires Python 3.6 to run, but few of our projects are on that version. The VS Code plugin conveniently lets you run black from its own virtualenv.

I had to give a specific version of black this morning. I didn't yesterday. Don't specify the version unless it makes you (and if you do, give the current version, not the one from this doc).

$ cd ~/Envs