Skip to content

Instantly share code, notes, and snippets.

View wolever's full-sized avatar

David Wolever wolever

View GitHub Profile
@wolever
wolever / django_template_custom_render.py
Created April 6, 2014 21:47
Shows how to render a Django template using a custom template tag library.
from django.conf import settings as s
from django.template import Parser, Lexer, Context, Library, StringOrigin
lib = Library()
def parse_template(library, origin, template_string):
if s.TEMPLATE_DEBUG:
from django.template.debug import DebugLexer, DebugParser
lexer_class, parser_class = DebugLexer, DebugParser
else:
@wolever
wolever / mkpkg.sh
Created March 14, 2014 20:30
Small shell function to create Python packages
mkpkg() {
if [[ -z "$1" ]]; then
echo "mkpkg: $0 PKG_DIR"
return 1
fi
if [[ ! -d "$1" ]]; then
mkdir "$1" || return $?
fi
touch "$1/__init__.py"
}

Keybase proof

I hereby claim:

  • I am wolever on github.
  • I am wolever (https://keybase.io/wolever) on keybase.
  • I have a public key whose fingerprint is B5A9 A97F CE49 06AF 8E13 DE44 43D2 86AB B230 230D

To claim this, I am signing this object:

@wolever
wolever / else_block_example.py
Created March 3, 2014 20:35
Apparently Python doesn't execute the `else` block of a `try/except/else` block if there is a return in the `try`.
"""
Python's ``else:`` block is *only* executed when the ``try:`` block does not reutrn:
$ python else_block_example.py
Running function, returning in 'try:' block: False
in 'else' block
in 'finally' block
Running function, returning in 'try:' block: True
in 'finally' block
"""
"""
Python 2:
$ python-2.7 finallyfun.py
e: Exception()
Python 3:
$ python3.3 finallyfun.py
Traceback (most recent call last):
File "finallyfun.py", line 9, in <module>
foo()
@wolever
wolever / histogram.sql
Last active April 19, 2023 20:28
Functions to create and draw histograms with PostgreSQL.
-- Functions to create and draw histograms with PostgreSQL.
--
-- psql# WITH email_lengths AS (
-- -# SELECT length(email) AS length
-- -# FROM auth_user
-- -# LIMIT 100
-- -# )
-- -# SELECT * FROM show_histogram((SELECT histogram(length, 0, 32, 6) FROM email_lengths))
-- bucket | range | count | bar | cumbar | cumsum | cumpct
-- --------+-------------------------------------+-------+--------------------------------+--------------------------------+--------+------------------------
@wolever
wolever / scopes.py
Last active August 29, 2015 13:56
Fun with Python scopes
global_var = 42
def outer_func():
outer_var = 21
un_used_outer_var = 16
def inner_func():
inner_var = 7
def inner_inner_func():
inner_inner_var = 3
# note: the outer_var must be explicitly referenced, otherwise it won't
@wolever
wolever / jquery.toggleClassPrefix.js
Created January 13, 2014 04:51
jQuery plugin to toggle an element's classes based on a prefix.
/**
* Toggles the CSS classes of an element based on a prefix::
* > elems = $(".status")
* > elems
* [<div class="status status-active">, <div class="status status-pending">]
* > elems.toggleClassPrefix("status-", "status-pending")
* > elems
* [<div class="status status-pending">, <div class="status status-pending">]
*/
$.fn.toggleClassPrefix = function(prefix, toAdd) {
@wolever
wolever / hoverzoom.js
Last active January 1, 2016 17:29
My preferred pattern for jQuery plugins.
// A simple plugin which zooms the element when it's hovered:
// $("a").hoverzoom();
// Demonstrates my preferred pattern for building jQuery plugins.
$.fn.hoverzoom = function() {
var method = arguments[0] || "init";
var parentArgs = arguments;
this.each(function() {
var args = Array.prototype.slice.call(parentArgs);
args[0] = $(this);
@wolever
wolever / plural.py
Last active March 29, 2018 18:05
A better pluralizing template tag for Django.
from django.template import Library
register = Library()
@register.simple_tag
def plural(n_str, singular, plural=None):
""" A better pluralization template tag.
The syntax is ``{% plural number "singular" "plural" %}``, where the