Skip to content

Instantly share code, notes, and snippets.

View alecthomas's full-sized avatar
😀

Alec Thomas alecthomas

😀
View GitHub Profile
@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
#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 / 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;
@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 / jsonreturnhandler.go
Created May 22, 2014 19:37
Convenient return handler for Martini JSON API calls
import (
"encoding/json"
"net/http"
"reflect"
"github.com/codegangsta/inject"
"github.com/go-martini/martini"
)
const (
@alecthomas
alecthomas / commitorrollbackonerror.go
Last active August 29, 2015 14:02
CommitOrRollbackOnError commits or rolls back a transaction based on the value of an error reference.
// CommitOrRollbackOnError commits or rolls back a transaction based on the
// value of an error reference. It helps avoid error prone boilerplate
// rollback calls.
//
// Most useful in a defer statement:
//
// tx, err := db.Begin()
// if err != nil {
// return err
// }
@alecthomas
alecthomas / sprite.cc
Last active August 30, 2015 17:15
EntityX sprite movement example
// Compile with: c++ -std=c++11 -lentityx -lsfml-graphics t.cc
#include "SFML/Graphics.hpp"
#include "entityx/entityx.h"
using namespace entityx;
struct Movement : public Component<Movement> {
sf::Vector2f position, direction;
};
@alecthomas
alecthomas / raml-0.8.md
Last active August 29, 2015 14:05
RAML 0.8 spec with TOC
package main
import (
"errors"
"fmt"
"strconv"
"github.com/alecthomas/kingpin"
)