Skip to content

Instantly share code, notes, and snippets.

View alecthomas's full-sized avatar
😀

Alec Thomas alecthomas

😀
View GitHub Profile
@alecthomas
alecthomas / bytereader.go
Created May 19, 2014 18:17
Make a normal io.Reader also function as an io.ByteReader
type ByteReader struct {
io.Reader
}
func (b *ByteReader) ReadByte() (byte, error) {
var buf [1]byte
if _, err := io.ReadFull(b, buf[:]); err != nil {
return 0, err
}
return buf[0], nil
@alecthomas
alecthomas / random_insertion_containers.cc
Created April 5, 2014 15:32
Benchmarks for insertion of random values into C++ containers maintaining order
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <list>
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
#include <iostream>
#include <string>
#include <vector>
#include <entityx/entityx.h>
struct TestComponent : entityx::Component<TestComponent> {
explicit TestComponent(std::string _name = "test") {
name = _name;
}
@alecthomas
alecthomas / gist:7432931
Created November 12, 2013 15:34
Python auto-import IDE assistance.
$ cat u.py
def f():
with suppress(IntegrityError):
print 'moo'
$ ./autoimporter u.py
from sqlalchemy.exc import IntegrityError
from contextlib import suppress
@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]>
@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 / 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 / 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 / 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 / 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