Skip to content

Instantly share code, notes, and snippets.

View prophile's full-sized avatar

Alistair Lynn prophile

View GitHub Profile
@prophile
prophile / StateAutoConversion.hs
Created February 19, 2014 16:30
Conversion between Automaton and StateArrow
{-# LANGUAGE GADTs #-}
module StateAutoConversion(fromAutomaton, toAutomaton, StateResult(StateResult)) where
import Prelude hiding (id, (.))
import Control.Category
import Control.Arrow
import Control.Arrow.Transformer
@prophile
prophile / crypt.php
Created March 6, 2014 19:31
Some password crypting with PHP
<?php
define('PASSWD_SALT_LENGTH', 16);
define('PASSWD_HASH_ALGORITHM', 'sha512');
define('SALT_LENGTH', 16);
function passwordCrypt($password) {
$saltSource = fopen('/dev/random', 'rb');
$saltData = bin2hex(fread($saltSource, PASSWD_SALT_LENGTH));
fclose($saltSource);
import subprocess
import sys
try:
policy = subprocess.check_output(['apt-cache', 'policy', 'python-dev'])
if 'Installed: (none)' in policy:
print("ERROR: no python dev package installed")
print("Please run: sudo apt-get install python-dev")
sys.exit(1)
except OSError:
@prophile
prophile / wrappers.py
Created August 9, 2014 12:11
Sanity checks on Flask decorator
from __future__ import print_function
from flask import Flask
from functools import wraps
import json
import sys
app = Flask(__name__)
def example_wrapper(f):
print(f.func_dict)
from __future__ import print_function
from collections import namedtuple, defaultdict
Check = namedtuple('Check', 'description check')
Condition = namedtuple('Condition', 'severity format')
Failure = namedtuple('Failure', 'type severity message data')
class Validator(object):
def __init__(self):
self.checks = []
from collections import namedtuple
import cgi
import sys
import yaml
DATA = """
- type: checkbox
description: Did the robot move?
key: robot_moved
- type: natural
import itertools
def tail(seq):
return itertools.islice(seq, 1, None)
def fibs():
yield 0
yield 1
yield from (x + y for x, y in zip(fibs(), tail(fibs())))
@prophile
prophile / pages.md
Created September 7, 2014 21:25
Some remarks in favour of GitHub Pages

In Promotion of Jekyll

There's been heavy discussion of the various options surrounding the pages for the Sponge wiki. At the time of writing, the current contenders for implementation are:

  • MediaWiki
  • DokuWiki
  • GitHub Wiki
  • GitHub Pages/Jekyll
  • A homebrew option
@prophile
prophile / stv.py
Created October 29, 2014 03:25
An implementation of STV with the Droop quota, using the Gregory method and an ad-hoc tie breaking system
from copy import copy
from collections import defaultdict
from fractions import Fraction
def stv(candidates, ballots, seats=1):
if seats > len(candidates):
raise ValueError('Not enough candidates for election')
rounds = {}
@prophile
prophile / resdist.py
Last active August 29, 2015 14:08
Resource distribution brute-force algorithm in Python
from itertools import permutations
import yaml
import sys
#votes = {'a': ['cheese', 'hats', 'faces'],
# 'b': ['hats', 'cheese', 'faces'],
# 'c': ['hats', 'faces', 'cheese']}
votes = {'a': 'CHWESK',
'b': 'CESWHK',