Skip to content

Instantly share code, notes, and snippets.

Tom Manderson TRManderson

View GitHub Profile
@TRManderson
TRManderson / caching.py
Last active Aug 22, 2017
Cache decorator and cached property decorator
View caching.py
from functools import wraps
def cache(fn):
cache_dict = {}
@wraps(fn)
def result(*args, **kwargs):
# can't cache kwargs
assert not kwargs
if args not in cache_dict:
cache_dict[args] = fn(*args)
@TRManderson
TRManderson / keybase.md
Created Jul 1, 2017
Verifying myself on Keybase
View keybase.md

Keybase proof

I hereby claim:

  • I am trmanderson on github.
  • I am trm (https://keybase.io/trm) on keybase.
  • I have a public key whose fingerprint is 2EF6 0F25 F529 D0CE FD37 1095 E056 0B6E A3AB FDFD

To claim this, I am signing this object:

@TRManderson
TRManderson / log_exc.py
Created May 26, 2017
Handy decorator for exception logging using `logging`
View log_exc.py
import logger
from functools import wraps
def log_exc(fn=None, reraise=None, msg=None, exc=None, logger=None):
"""
Decorator to log exceptions in a function.
Usage:
@log_exc
@TRManderson
TRManderson / .gitignore
Last active Feb 8, 2017
Testing the new metaclass support for mypy in Py2
View .gitignore
__pycache__/
*.py[cod]
*$py.class
pip-log.txt
pip-delete-this-directory.txt
.venv
@TRManderson
TRManderson / index.html
Last active Oct 5, 2016
Example jquery.load
View index.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#viewer").load("/viewer.html");
console.log("Done");
});
</script>
@TRManderson
TRManderson / rec_dl.py
Created May 19, 2016
Recursively download everything from a start page, so long as it starts with a given root url
View rec_dl.py
import mechanize, os, errno
from urlparse import urldefrag
from time import sleep
def recursive_download(root, page, browser=mechanize.Browser(), visited=set()):
if page in visited or not page.startswith(root):
return
else:
visited.add(page)
View oil_blending.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View Farmer Jones (integer).ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@TRManderson
TRManderson / Makefile
Created Feb 20, 2016
My AVR makefile for a uni course last year
View Makefile
# Name: Makefile
# Author: Tom Manderson
# Based on https://github.com/obdev/CrossPack-AVR/
DEVICE = atmega324a
CLOCK = 8000000
PROGRAMMER = -c avrispmkii
OBJECTS = project.o buttons.o game.o ledmatrix.o score.o scrolling_char_display.o serialio.o spi.o terminalio.o timer0.o
AVRDUDE = avrdude $(PROGRAMMER) -p m324pa -F
COMPILE = avr-gcc -Wall -Os -std=c99 -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)
@TRManderson
TRManderson / check_gh.py
Last active Jan 28, 2016
Repeatedly hit the Github status API to see if it's down or not (py2/3 compatible and cross-platform)
View check_gh.py
from __future__ import print_function
import requests
import os
import time
if os.name == "posix":
def sound():
os.system("play --no-show-progress --null --channels 1 synth 5 sin 440")
else:
You can’t perform that action at this time.