Skip to content

Instantly share code, notes, and snippets.

View alecthomas's full-sized avatar
😀

Alec Thomas alecthomas

😀
View GitHub Profile
@alecthomas
alecthomas / shopping_cart.py
Created August 8, 2013 15:57
Example of using Injector without annotating application-level classes
from injector import Module, Injector, provides, inject, singleton
from some_credit_card_provider import CreditCardConnection, CreditCardVendor
class CreditCardProcessor(object):
def __init__(self, vendor, connection):
self.vendor = vendor
self.connection = connection
def charge_order(self, card, amount):
@alecthomas
alecthomas / gist:6124383
Created July 31, 2013 17:53
FINE, FINER and FINEST logging for Python
logging.FINE = 7
logging.FINER = 5
logging.FINEST = 1
logging.addLevelName(logging.FINE, 'FINE')
logging.addLevelName(logging.FINER, 'FINER')
logging.addLevelName(logging.FINEST, 'FINEST')
logging.Logger.fine = lambda self, *args, **kwargs: self.log(logging.FINE, *args, **kwargs)
logging.Logger.finer = lambda self, *args, **kwargs: self.log(logging.FINER, *args, **kwargs)
@alecthomas
alecthomas / gist:6089974
Last active December 20, 2015 06:59
My Sublime plugins

Favourites

  • SublimeCodeIntel: "IntelliSense" for Sublime. Provides context-sensitive symbol/argument completion for a bunch of languages, Python included.
  • SublimeLinter: Shows lint/compile errors in realtime for a whole bunch of languages.
  • SyncedSideBar: Expands the sidebar to show the currently open file.
  • SideBarEnhancements: Adds a bunch of very useful context-menu entriest to the sidebar.
  • Color Highlighter: Highlight text that describes a color, in that color eg. #FFFFFF will show up as white, "red" will show up as red, and so on
  • DashDoc: Open the currently selected symbol in Dash (document viewer).
  • DocBlockr: Helper for Java-style documentation blocks.
  • Git: Useful stuff for Git, like viewing diffs in Sublime.
@alecthomas
alecthomas / signaturevalidatingabc.py
Last active December 20, 2015 04:59
An ABCMeta that validates the method signatures of concrete implementations.
import inspect
from abc import ABCMeta
class SignatureValidatingABCMeta(ABCMeta):
"""An ABCMeta that validates the method signatures of concrete implementations.
That is, given:
class Class(object):
@alecthomas
alecthomas / gist:5779354
Last active December 18, 2015 11:59
Monkey patching context manager
from contextlib import contextmanager
@contextmanager
def patch(owner, attr, value):
"""Monkey patch context manager.
with patch(os, 'open', myopen):
...
"""
old = getattr(owner, attr)
@alecthomas
alecthomas / gist:5758156
Created June 11, 2013 16:02
Example of Injector assisted injection using @provides.
from injector import *
class Session(object):
def __init__(self, key, site):
self.key = key
self.site = site
SessionKey = Key('SessionKey')
@alecthomas
alecthomas / gist:5408999
Created April 18, 2013 00:51
Django: Users joined by hour
models.User.objects.order_by('date_joined').extra({'day_joined': 'date(date_joined)', 'hour_joined': 'hour(date_joined)'}).values('day_joined', 'hour_joined').annotate(joined_count=Count('id'))
@alecthomas
alecthomas / console_log_call_stack.js
Created March 22, 2013 20:24
Javascript console.log() with collapsed call stack
if (typeof console !== undefined) {
console.logJack = console.log;
console.log = function(){
console.groupCollapsed.apply(console, arguments);
console.logJack(new Error().stack);
console.groupEnd();
};
}
@alecthomas
alecthomas / gist:4497119
Created January 9, 2013 21:28
*Very* hacky YAML + Salt structure validation.
import itertools
import functools
import fnmatch
import os
from StringIO import StringIO
import yaml
from yaml.parser import ParserError
import voluptuous as V
@alecthomas
alecthomas / gist:4103124
Created November 18, 2012 02:46
Flask + Injector - A dependency-injected web framework.
from injector import Module, inject, singleton
from flask import Flask, Request, jsonify
from flask.ext.cache import Cache
from flask.ext.injector import Builder, route, decorator
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import Column, String
"""