Skip to content

Instantly share code, notes, and snippets.

View wolever's full-sized avatar

David Wolever wolever

View GitHub Profile
@wolever
wolever / reverseadmin.py
Last active June 21, 2023 06:48 — forked from mzbyszewska/reverseadmin.py
Forked from https://gist.github.com/mzbyszewska/8b6afc312b024832aa85 , updated to work with Django 1.8
'''
adminreverse from here http://djangosnippets.org/snippets/2032/
changed for working with ForeignKeys
'''
'''
reverseadmin
============
Module that makes django admin handle OneToOneFields in a better way.
A common use case for one-to-one relationships is to "embed" a model
inside another one. For example, a Person may have multiple foreign
@wolever
wolever / google_maps_ipynb.py
Created August 19, 2015 16:05
Google Maps with IPython Notebook
import json
from jinja2 import Template
from IPython.core.display import HTML, Javascript
def init_maps():
HTML("""
<script>function dummy() {};</script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=visualization&callback=dummy"></script>
""")
@wolever
wolever / uuids-as-primary-keys.rst
Last active December 9, 2021 14:31
UUIDs as primary keys

The major tradeoffs between using UUIDs and sequential integers as primary keys fall into two categories:

  1. Usability tradeoffs, and
  2. Performance tradeoffs

And I'll say up front: I believe UUIDs are rarely a good idea, both for the reasons I've listed here, and the fact that I've almost never seen them used in the wild.

@wolever
wolever / test_tsquery_escape.py
Last active October 3, 2022 09:47
Parse and escape a query string so it's safe to use with Postgres' `to_tsquery(…)`
import re
from nose.tools import assert_equal
from nose_parameterized import parameterized
from tsquery_escape import tsquery_escape
@parameterized([
("1 OR 2", "1 | 2"),
("(1) 2", "( 1 ) & 2"),
("&", "'&':*"),
@wolever
wolever / divbyzero.ipynb
Created July 31, 2015 19:48
The unique handling of division by zero across Pandas and NumPy
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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 / 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 / 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 / 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 / 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/