Skip to content

Instantly share code, notes, and snippets.

View louisswarren's full-sized avatar

Louis Warren louisswarren

View GitHub Profile
@louisswarren
louisswarren / xrtost.py
Created June 11, 2016 00:05
Convert .Xresources colours to st config format
#!/bin/python3
from sys import argv
with open(argv[1]) as f:
lines = f.readlines()
d = dict()
for line in lines:
if line[0] != '*' or 'color' not in line:
continue
@louisswarren
louisswarren / wifibridge.sh
Last active June 26, 2016 10:04
Configure device as a wifi bridge
#!/bin/sh
/sbin/ifconfig eth0 10.0.0.1 netmask 255.255.255.0 up
/sbin/iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
echo "1" | /usr/bin/tee /proc/sys/net/ipv4/ip_forward
@louisswarren
louisswarren / memeoisation.py
Last active August 2, 2016 07:21
MEMEoisation (hardy har har)
def meme(f):
memes = {}
def inner(*args, **kwargs):
key = (args, tuple(sorted(kwargs.items())))
if key in memes:
return memes[key]
ret = f(*args, **kwargs)
memes[key] = ret
return ret
return inner
@louisswarren
louisswarren / tautologies.py
Last active September 7, 2016 10:46
Generate all tautologies
from itertools import product, permutations
from pyaccum import accumulate
class Proposition:
def __invert__(self):
return Impl(self, bottom)
def __or__(self, other):
return Disj(self, other)
@louisswarren
louisswarren / ai_range_syntax.py
Created September 19, 2016 04:26
This should be compatible with your Cosc367 project
from sequences import *
class ai_class:
def __getitem__(self, items):
if items[-1] is not Ellipsis:
yield from items
return
expression = find_sequence(items[:-1])
yield from items[:-1]
@louisswarren
louisswarren / builtins.py
Last active September 21, 2016 10:08
Co-sane implementations of python builtins and more
import itertools
sum = lambda x: list(itertools.accumulate(x)).pop()
len = lambda x: sum(1 for _ in x)
range = lambda n, step=1: itertools.accumulate(itertools.repeat(step, times=n-1), lambda x, y: x + y)
# Full implementation
range = lambda min, max=None, step=1: itertools.accumulate(itertools.chain((min if max is not None else 0,),
itertools.repeat(step, (max-min-1)//step if max is not None else None)))
user="louisswarren"
github="https://api.github.com"
pyjsonlines="'\n'.join(r[sys.argv[-1]] for r in json.loads(sys.stdin.read()))"
pycmd="import sys, json; print($pyjsonlines)"
curl -s "$github/users/$user/repos" | python3 -c "$pycmd" git_url |
while read x; do
git clone "$x"
done
@louisswarren
louisswarren / a7booklet.py
Created September 28, 2016 09:32
How to print a 64 page book on 4 a4 sheets
front = 16,1,14,3,12,5,10,7
back = 4,13,2,15,8,9,6,11
for x in range(4):
print("Sheet", x + 1)
for side in front, back:
print(',\t'.join(str(p + 16*x) for p in side))
print()
# Sheet 1
@louisswarren
louisswarren / mp4tomp3.sh
Created September 29, 2016 09:54
Convert mp4 to mp3
for f in *.mp4; do
target="${f%.mp4}.mp3"
ffmpeg -i "$f" -vn -acodec libmp3lame -ac 2 -ab 320k -ar 48000 "$target"
done
@louisswarren
louisswarren / accidental_lisp.py
Last active October 5, 2016 16:25
It was around the time I added car and cdr that I realised this wasn't about natural numbers anymore
from util import *
class Obj:
@constructor
def __init__(self, name):
pass
def __str__(self):
return str(self.name)