Skip to content

Instantly share code, notes, and snippets.

@lemiant
lemiant / gist:e87dc2165c7f825aad60
Created December 3, 2014 18:33
markDirty()/cleanUp() function decorator
(function() {
window.batch_function = function(target) {
var state = "clean";
var lastArgs = [window, []];
var run = function() {
if (state === "clean") {
state = "running";
target.apply(this, arguments);
}
else {
@lemiant
lemiant / tabs.css
Created October 22, 2014 18:50
CSS for Bootstrap tabs
/*
* This is the compiled css for Bootstrap tabs and pills.
* Compiled from bootstrap 2.3.2 (less/navs.less)
*/
.clearfix {
*zoom: 1;
}
.clearfix:before,
.clearfix:after {
@lemiant
lemiant / thumbs.py
Created October 8, 2014 20:40
Make Thumbnails from a Directory of images
from PIL import Image
import os, argparse, sys
import imghdr
parser = argparse.ArgumentParser(description='Create thumbs for all the images in a directory')
parser.add_argument('dir', help="The directory the images are in")
parser.add_argument('--width', help="Thumbnail max-width", type=int, default=128)
parser.add_argument('--height', help="Thumbnail max-height", type=int, default=128)
args = parser.parse_args()
{
"metadata": {
"name": "",
"signature": "sha256:5897937ca974f647822cf4323d573b62f4b92a81227691433746e3340b6fce59"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
"""Ultra-minimalist logger with context
e.g.:
from context_log import Log
Log.out = open('report.txt')
Log.formatter = lambda dictionary: "%(filename)s>%(lineno)03d: %(session)s: %message" % dictionary
Log.context['session'] = 'Cammping Trip'
Log.warn("There's a bear behind you")
@lemiant
lemiant / async.py
Last active August 29, 2015 13:59
Allows dead simple paralellization of a Python function call
def async(func):
"""A decorator to make a function run in its own thread and return its result on .join()"""
def launch(*args, **kwargs):
target = ThreadedFunction(func, *args, **kwargs)
target.start()
return target
return launch
class ThreadedFunction(threading.Thread):
def __init__(self, func, *args, **kwargs):
self.args = args
@lemiant
lemiant / ReadWriteLock.py
Last active May 12, 2022 08:26
Reader Biased ReadWriteLock in Python
import threading
class ReadWriteLock(object):
""" An implementation of a read-write lock for Python.
Any number of readers can work simultaneously but they
are mutually exclusive with any writers (which can
only have one at a time).
This implementation is reader biased. This can be harmful
under heavy load because it can starve writers.
However under light load it will be quite perfomant since
@lemiant
lemiant / html5_audio_promises.js
Last active December 15, 2015 19:10
Handling HTML5 audio/video with jQuery promises
// This function plays an audio/video element and creates a promise
// that resolves when the audio/video finishes playing
// It's nice :)
function HTML5Play(target){
var t = $(target);
if(t.length != 1 || !t.is('audio, video')) return undefined; //These are invalid cases
var deferred = new $.Deferred();
t.on('ended', function(){ deferred.resolve(t) })