Skip to content

Instantly share code, notes, and snippets.

View willcalderbank's full-sized avatar

Will Calderbank willcalderbank

  • London
View GitHub Profile
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
@willcalderbank
willcalderbank / gist:36bd8f7b0b987a85a512
Created June 23, 2015 09:06
JS animation loop with duration
_animateSection: function(duration) {
if (duration === undefined) duration = 1;
var self = this;
_animationLoop();
function _animationLoop(_startTime, _timestamp) {
switchSrc: function(overlayId, src, duration) {
var $original = $('#' + overlayId);
$newElement = $original.clone();
$newElement.removeAttr('id');
$newElement.css({opacity: 0, background: 'url(' + src + ')'});
$original.after($newElement);
if (duration === undefined) {
duration = 200;
@willcalderbank
willcalderbank / cProfile.py
Created November 24, 2014 09:59
cProfile
import cProfile
def do_cprofile(func):
def profiled_func(*args, **kwargs):
profile = cProfile.Profile()
try:
profile.enable()
result = func(*args, **kwargs)
profile.disable()
return result
@willcalderbank
willcalderbank / etree_to_dict.py
Created November 20, 2014 09:43
Convert xml to dict
from collections import defaultdict
import functools
from lxml import etree
def etree_to_dict(t, ns=None):
"""
http://stackoverflow.com/questions/2148119/how-to-convert-an-xml-string-to-a-dictionary-in-python
"""
def strip_ns(tag):
if ns:
@willcalderbank
willcalderbank / humansort.py
Created November 3, 2014 11:06
Human Sort, sorts "something2" before "something10
import re
def tryint(s):
try:
return int(s)
except:
return s
def alphanum_key(s):
""" Turn a string into a list of string and number chunks.
@willcalderbank
willcalderbank / S3Property.py
Created September 22, 2014 15:06
S3Property
class S3Property(db.StringProperty):
"""
On a put the value in the property is moved from the temp bucket to the
stated permanent one.
get_value_for_datastore is called as the model instance is placed in the
datastore, at this point we interrupt to process and move the asset before
setting the assets new url in the datastore. get_value_for_datastore doesnt
effect the model
@willcalderbank
willcalderbank / retry.py
Last active July 15, 2022 11:58
Python retry decorator
def retry(times, exceptions):
"""
Retry Decorator
Retries the wrapped function/method `times` times if the exceptions listed
in ``exceptions`` are thrown
:param times: The number of times to repeat the wrapped function/method
:type times: Int
:param Exceptions: Lists of exceptions that trigger a retry attempt
@willcalderbank
willcalderbank / gist:6938004
Created October 11, 2013 16:39
This is a highly modified version of the standard ``tastypie.Serialiser.to_etree`` and ``tastypie.Serialiser.to_xml``. These modifications allow use to do the following by optionally setting arguments in the serialiser constructor: - Change the item nodes name (item_title) - Exclude the node types (define_types) - Exclude the resource uri (exclu…
def to_etree(self, data, name=None, depth=0):
"""
Given some data, converts that data to an ``etree.Element`` suitable
for use in the XML output. (Using recursion)
This is a highly modified version of the standard
``tastypie.Serialiser.to_etree``. These modifications allow use to do
the following by optionally setting arguments in the serialiser
constructor:
@willcalderbank
willcalderbank / gist:6916055
Created October 10, 2013 10:12
HMAC Signer The output of this file: Message: message URI: bWVzc2FnZQ%3D%3D%0A.WrOgAeqVpGmJRUAP8uyG24gilj3Jvbr9S5AUqNaYuNY%3D Correct key: message Incorrect key: False
import base64
import hashlib
import hmac
import urllib2
def generate(key, message):
signature = hmac.new(
key=key,
msg=message,
digestmod=hashlib.sha256