Skip to content

Instantly share code, notes, and snippets.

View wolever's full-sized avatar

David Wolever wolever

View GitHub Profile
"""
Of special interest:
- The ``Target`` class
- The ``Target.confirm_command`` and ``Target.confirm_git_branch`` methods
- The list of target definitions (``targets = [...]``)
- Using the ``.ak-default-target`` file to define a default target (can be different across branches)
Example use: ``fab ak-dev deploy``
"""
@wolever
wolever / api_client.py
Created November 19, 2014 21:28
A simple example of how I do logging in an API client
"""
When I integrate with a 3rd party API, I write a thin wrapper around it to
normalize any eccentricities and provide a well-defined Python-level API.
"""
log = logging.getLogger("acme.api")
class AcmeAPIClient(object):
def get_widgets(self):
"""
@wolever
wolever / Conly.vim
Created February 3, 2015 20:50
:Conly Vim command to close all windows in the current column.
" :Co[nly] closes all other windows in the current column.
" For example, if the cursor is in the "cur" window, below, windows 1, 2, and
" 4 will be closed:
" +-------+ +-------+
" | win 1 | | |
" +-------+ | |
" | win 2 | | |
" +-------+ --> :Conly --> | cur |
" | cur | | |
" +-------+ | |
@wolever
wolever / pyhierarchy2dot.py
Last active August 29, 2015 14:16
pyhierarchy2dot.py: generates a simple dot graph class hierarchy from a set of base classes.
"""
Generates a simple dot graph class hierarchy from a set of base classes.
Usage::
$ python pyhierarchy2dot.py sqlalchemy.String sqlalchemy.Integer sqlalchemy.DateTime
"""
import sys
import importlib
@wolever
wolever / ctagsrc
Last active August 29, 2015 14:16
Angular definitions for ctags (http://ctags.sourceforge.net/)
# Add the following lines to ~/.ctags to capture Angular definitions like `ngModule.service('MyService', …)`.
# See also: http://ctags.sourceforge.net/
--regex-javascript=/\.module\(\s*['"]([A-Za-z0-9._$]+)['"]/\1/c,ng-module/
--regex-javascript=/\.controller\(\s*['"]([A-Za-z0-9._$]+)['"]/\1/c,ng-controller/
--regex-javascript=/\.factory\(\s*['"]([A-Za-z0-9._$]+)['"]/\1/c,ng-factory/
--regex-javascript=/\.directive\(\s*['"]([A-Za-z0-9._$]+)['"]/\1/c,ng-directive/
--regex-javascript=/\.service\(\s*['"]([A-Za-z0-9._$]+)['"]/\1/c,ng-service/
--regex-javascript=/\.filter\(\s*['"]([A-Za-z0-9._$]+)['"]/\1/c,ng-filter/
@wolever
wolever / signal-handlers-example.py
Created March 30, 2015 16:45
Django signal handlers are stored as weak refs and should *not* be added inside closures.
# BAD: Signal receivers will not be executed because, when the function
# returns, all references to them will be lost and they will be GC'd.
from django.db.models.signals import pre_delete
from django.dispatch import receiver
def add_signal_receivers_unsafe():
@receiver(pre_delete)
def receive_pre_delete(sender, **kwargs):
print "pre_delete received!"
@wolever
wolever / mail_managers_similar_emails.py
Created April 21, 2015 15:11
Sends an email to managers with a list of similar emails so a human can help fix typos in password reset requests.
from django.db import connection
def mail_managers_similar_emails(request, email):
""" Sends an email to managers with a list of emails similar to ``email``
so a human can help fix typos in password reset requests. """
try:
cur = connection.cursor()
cur.execute("""
@wolever
wolever / ezdirective.js
Created May 6, 2015 23:09
ezdirective makes it possible to create AngularJS directives without checking the documentation.
/*
* ezdirective makes it possible to create AngularJS directives without
* checking the documentation.
*
* Sane defaults are used (`restrict: 'E'` and `scope: {}`), and the link()
* function is injected using $injector, which which can also inject the
* `$scope`, `$elem`, and `attrs` that a normal directive link function would
* expect.
*
* Examples:
@wolever
wolever / str_format_will_crash_your_app.py
Last active August 29, 2015 14:22
Why I _strongly_ dislike str.format(…) on Python 2
# Python 2's %-formatting will "upgrade" the format string to unicode if an
# argument is unicode, where str.format(…) will downgrade unicode arguments to
# bytes. This leads to unnecessarily fragile code, as very small programmer
# mistakes can cause show-stopping Unicode-related exceptions.
# Consider the simplest "hello world" of string formatting with:
>>> name = u"Aléx ✨"
# Using %-formatting:
>>> "Hello, %s!" %(name, )
@wolever
wolever / divbyzero.ipynb
Created July 31, 2015 19:48
The unique handling of division by zero across Pandas and NumPy
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.