Skip to content

Instantly share code, notes, and snippets.

Avatar

Louis Warren louisswarren

View GitHub Profile
@louisswarren
louisswarren / eduroam
Last active Oct 14, 2016
Config for netctl for eduroam wifi
View eduroam
# Write to /etc/netctl/eduroam and start with netctl start eduroam
# Replace <wireless device> <username> and <password> below
Description='Eduroam'
Interface=<wireless device>
Connection=wireless
Security=wpa-configsection
IP=dhcp
ESSID=eduroam
WPAConfigSection=(
@louisswarren
louisswarren / makemake.py
Created Oct 24, 2016
Generate C make files automatically
View makemake.py
import re
import sys
args = sys.argv[1:]
def find_dependencies(lines):
deps = set()
for line in lines:
match = re.match('#include "(.*)"', line)
if match:
@louisswarren
louisswarren / deadman.sh
Last active Oct 25, 2016
Automatically revert change to a config file
View deadman.sh
#!/bin/sh
# Example:
# deadman /etc/network/interfaces 120 && service networking restart &
#
# After two minutes, changes are reverted and the service is restarted.
# No getting locked out!
# (you were root weren't you?)
backup=`cat "$1"`
View twentyone.py
def twentyone(nums, stack=[]):
for index, num in enumerate(nums):
new_stack = stack + [num]
total = sum(new_stack)
if total == 21:
yield new_stack
elif total < 21:
yield from twentyone(nums[index+1:], new_stack)
print(list(twentyone([1,9,11,5,6])))
View all_primes.py
def is_prime(n):
return all(n % i != 0 for i in range(2, 1 + int(n**0.5)))
def get_primes():
n = 2
while True:
if is_prime(n):
yield n
n += 1
@louisswarren
louisswarren / humour.py
Last active Jan 23, 2017
A turing machine can't solve the halting problem, but it can solve the hilarity problem, right gang?
View humour.py
prepositions = """
aboard, about, above, across, after, against, along, amid, among, anti,
around, as, at, before, behind, below, beneath, beside, besides, between,
beyond, but, by, concerning, considering, despite, down, during, except,
excepting, excluding, following, for, from, in, inside, into, like, minus,
near, of, off, on, onto, opposite, outside, over, past, per, plus,
regarding, round, save, since, than, through, to, toward, towards, under,
underneath, unlike, until, up, upon, versus, via, with, within, without
""".split(', ')
@louisswarren
louisswarren / tail_recursion.py
Last active Jan 23, 2017
Decorator which allows recursive-style functions to be iterated instead
View tail_recursion.py
# tail_recurse decorator
# decorated function should return its base case, and yield the arguments for
# the recursion step (yielded value must be starable)
def tail_recurse(f):
def inner(*args, **kwargs):
ret = f(*args, **kwargs)
while True:
try:
recursion_args = next(ret)
View setfs.md

Replace hierarchical directories with categories.

Categories

/foo - Contains all files in the category foo.

/foo/bar

@louisswarren
louisswarren / date_rename.py
Created Mar 6, 2017
Rename files in a directory to have ordinal prefixes, sorted by US date (mm-dd-yyyy) in filename
View date_rename.py
import os
import re
US_DATE_REGEX = re.compile(r'(\d\d)-(\d\d)-(\d\d\d\d)')
def parse_name(name):
match = US_DATE_REGEX.search(os.path.basename(name))
if match:
month, day, year = map(int, match.groups())
@louisswarren
louisswarren / LICENSE.md
Created Mar 6, 2017
License for all of my gists (MIT)
View LICENSE.md

MIT License

Copyright (c) 2017 Louis Warren

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: