Skip to content

Instantly share code, notes, and snippets.

Avatar

Louis Warren louisswarren

View GitHub Profile
@louisswarren
louisswarren / xrtost.py
Created Jun 11, 2016
Convert .Xresources colours to st config format
View xrtost.py
#!/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 Jun 26, 2016
Configure device as a wifi bridge
View wifibridge.sh
#!/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 Aug 2, 2016
MEMEoisation (hardy har har)
View memeoisation.py
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 Sep 7, 2016
Generate all tautologies
View tautologies.py
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 Sep 19, 2016
This should be compatible with your Cosc367 project
View ai_range_syntax.py
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 Sep 21, 2016
Co-sane implementations of python builtins and more
View builtins.py
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)))
View github.sh
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 Sep 28, 2016
How to print a 64 page book on 4 a4 sheets
View a7booklet.py
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 Sep 29, 2016
Convert mp4 to mp3
View mp4tomp3.sh
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 Oct 5, 2016
It was around the time I added car and cdr that I realised this wasn't about natural numbers anymore
View accidental_lisp.py
from util import *
class Obj:
@constructor
def __init__(self, name):
pass
def __str__(self):
return str(self.name)