Skip to content

Instantly share code, notes, and snippets.

View alecthomas's full-sized avatar
😀

Alec Thomas alecthomas

😀
View GitHub Profile
@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: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 / 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 / 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 / shopping_cart_coupled_di.py
Last active December 20, 2015 19:49
Example of using Injector with DI coupling to application-level classes
from injector import Module, Injector, provides, inject, singleton
from some_credit_card_provider import CreditCardConnection, CreditCardVendor
@singleton
class CreditCardProcessor(object):
@inject(vendor=CreditCardVendor, connection=CreditCardConnection)
def __init__(self, vendor, connection):
self.vendor = vendor
self.connection = connection
@alecthomas
alecthomas / python_folding.vim
Created August 14, 2013 20:30
Fold Python docstrings in VIM.
"
" Fold multi-line Python comments into one line.
"
" Also maps the "-" key to toggle expansion and <C-f> to toggle all folding.
"
setlocal foldmethod=syntax
setlocal foldtext=FoldText()
setlocal fillchars=
map <buffer> - za
@alecthomas
alecthomas / sfml.rb
Created August 27, 2013 00:26
SFML2 homebrew keg
require 'formula'
# Documentation: https://github.com/mxcl/homebrew/wiki/Formula-Cookbook
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
class Sfml < Formula
homepage 'http://www.sfml-dev.org'
version '2.1'
url 'http://www.sfml-dev.org/download/sfml/2.1/SFML-2.1-sources.zip'
@alecthomas
alecthomas / gist:6414976
Created September 2, 2013 16:54
Inconsistent "go vet" behaviour...
[alec@cavern:~/.go/src/github.com/alecthomas/gozmq]vet *.go
zmq_3_x_test.go:204: unreachable code
zmq_test.go:127: arg iothreads for printf verb %s of wrong type: int
zmq_test.go:140: arg iothreads for printf verb %s of wrong type: int
zmq_test.go:168: arg rc for printf verb %d of wrong type: error
[alec@cavern:~/.go/src/github.com/alecthomas/gozmq]vet zmq_test.go
[alec@cavern:~/.go/src/github.com/alecthomas/gozmq]vet zmq_3_x_test.go
@alecthomas
alecthomas / weak_intrusive_ptr.hpp
Created September 3, 2013 15:49
Weak pointer support for boost::intrusive_ptr
#pragma once
#include <boost/intrusive_ptr.hpp>
using boost::intrusive_ptr;
struct _GC {
@alecthomas
alecthomas / query.py
Last active December 27, 2015 08:29
A generic search query parser.
# encoding: utf-8
#
# Copyright (C) 2009 Alec Thomas <[email protected]>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#
# Author: Alec Thomas <[email protected]>