Skip to content

Instantly share code, notes, and snippets.

@brunal
brunal / markov_bulldog.py
Created October 20, 2015 15:42
admiralbulldog generator
#!/usr/bin/env python3
from collections import defaultdict
import random
FINAL_STATE = 'FINAL_STATE'
FINAL_STATE_SPACED = ' {} '.format(FINAL_STATE)
def get_corpus():
@brunal
brunal / ctypes_gio.py
Created March 26, 2015 09:22
Calling GIO with ctypes
import ctypes
import ctypes.util
class GioURI(object):
"""Use gio URI function g_file_get_uri. Paths must be utf-8 encoded.
"""
name = "GIO"
def __init__(self):
@brunal
brunal / generate_graphs.py
Created January 29, 2015 17:33
Generate graphs for python 2-3 2014 usage survey
import csv
from collections import Counter
from operator import itemgetter
from matplotlib import rcParams
import matplotlib.pyplot as plt
rcParams.update({'figure.autolayout': True})
answers_2013 = {"written_python2": (4660, 119),
@brunal
brunal / logging.py
Created January 5, 2015 08:52
Simple {}-style formatting logging for python 2.6 to 3.4
"""Allow {}-style logging on python 2 and 3
Provide everything the "logging" module does, the only difference is that when
getLogger(name) instantiates a logger that logger uses {}-style formatting.
It can be named logging if it is inside a package (e.g. foo.logging), otherwise
name it any way you like.
It requires special hacks for python 2.6 due to logging.Logger being an old-
style class and having no loggerClass attribute.
@brunal
brunal / deciles.py
Created November 24, 2014 16:11
python random perf
from sys import argv
def fast():
from numpy.random import uniform
return uniform(0.0, 1.0, 1000000)
def slow():
from random import uniform
@brunal
brunal / rankaggregation.py
Created April 17, 2013 14:39
Implementation of a rank aggregation algorithm using the paper http://www2007.org/papers/paper286.pdf The solver cvxopt is used with the frontend picos.
from cvxopt import matrix, sparse, spmatrix
import cvxopt.lapack
import picos
class MarkovChain():
def __init__(self, columns, target):
n = len(target)
l = len(columns)