Skip to content

Instantly share code, notes, and snippets.

@andialbrecht
andialbrecht / models.py
Created March 11, 2011 12:03
Adds Meta option db_column_prefix that prepends a per-model prefix to db column names
# -*- coding: utf-8 -*-
# This hack adds a new Meta option "db_column_prefix". If not None all
# database columns in a model will be prepended with this prefix (the
# database columns, not the attributes). See Person model for an
# example.
#
# Issues:
# - How to set a prefix for a m2m table? - If "through" argument is
# not specified on a M2MField, Django automatically adds a model
@andialbrecht
andialbrecht / gist:917126
Created April 13, 2011 07:26
Regex to parse pylint output
# Regex to extract error messages from "pylint -f parseable" output
import re
P_PYLINT_ERROR = re.compile(r"""
^(?P<file>.+?):(?P<line>[0-9]+):\ # file name and line number
\[(?P<type>[a-z])(?P<errno>\d+) # message type and error number, e.g. E0101
(,\ (?P<hint>.+))?\]\ # optional class or function name
(?P<msg>.*) # finally, the error message
""", re.IGNORECASE|re.VERBOSE)
@andialbrecht
andialbrecht / closure-gjslint.el
Created May 16, 2011 09:58
Emacs mode for running gjslint from Closure library
;;; closure-gjslint.el --- Closure Lint mode
;; Copyright (C) 2011 Andi Albrecht
;; Author: Andi Albrecht <[email protected]>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
@andialbrecht
andialbrecht / git-branch-io
Created May 31, 2011 08:06
git command to summarize incoming and outgoing changes on local branches
#!/usr/bin/env python2
# git-branch-io -- show incoming and outgoing commits
#
# Place this file somewhere in your PATH.
#
# Usage: git branch-io [branch]
#
# If branch is not given the current branch will be used. The script
# prints out how many commits should go in either direction to bring
@andialbrecht
andialbrecht / python-flake8.el
Created September 26, 2011 08:09
Thin wrapper around python-pylint to run flake8
;;; python-flake8.el --- Thin wrapper around python-pylint to run flake8
;; Copyright (C) 2011 Andi Albrecht
;; Author: Andi Albrecht <[email protected]>
;; Keywords: languages
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
try:
from collections import OrderedDict
except ImportError:
OrderedDict = None
from UserDict import UserDict
if OrderedDict is not None:
class Cache(OrderedDict):
def flush(self):
#!/bin/bash
# A hacky command for a common task:
# - stash away all changes in working branch
# - update master from SVN
# - rebase working branch on master
# - flush all committed changes from working branch to master
# - push them to SVN
# - update working branch again
# - patch in changes
@andialbrecht
andialbrecht / customtreemodel.py
Created January 5, 2013 19:46
Simple script demonstrating a custom Gtk.TreeModel for Gtk 3 (known as GenericTreeModel in PyGtk 2).
#!/usr/bin/env python3
# This script demonstrates a custom TreeModel (known as
# GenericTreeModel in PyGtk 2).
#
# Note: This script requires python-gi (pygobject) >= 3.4.
from gi.repository import GObject, Gtk
application: your-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: demo.application
@andialbrecht
andialbrecht / tulipshell.py
Created May 27, 2013 18:26
Helper to run a background tulip loop in an interpreter session.
# -*- coding: utf-8 -*-
"""Set up an interactive interpreter to have a underlying tulip loop.
The setup() function replaces the default loop policy, that starts a loop only
for the main thread, by a policy that has a single global loop available in any
thread.
The loop will be started when calling setup() in a background thread so that
the main thread could still be used by the interactive shell.