Skip to content

Instantly share code, notes, and snippets.

@heyimalex
heyimalex / tripp.py
Last active December 23, 2015 05:39
Short script to generate something like a tripcode securely from a string. Security dependent on length (and secretness) of your secret key.
from hashlib import sha512
import hmac
import string
# random key, generated with os.urandom(20)
secret_key = 'y6!n\xe1\x15\x96\x8c\xe8VX:\xa7\ta\xe6c\xf9\x04+'
# charset to use in tripcode
charset = string.ascii_letters + string.digits + '$#!@%&*'
# tripcode length
length = 10
@heyimalex
heyimalex / pygments_def_filter.py
Created January 28, 2014 16:42
hacky pygments filter to change "def" keyword token type
from markdown2 import Markdown
patched = Markdown._get_pygments_lexer
def patch_get_pygments_lexer(self, lexer_name):
if lexer_name != "python":
return patched(self, lexer_name)
from pygments.lexers import PythonLexer
from pygments.token import Name, Keyword
from pygments.filter import Filter
@heyimalex
heyimalex / fig.conf
Created March 19, 2014 19:27
fig upstart script
description "fig service runner"
start on filesystem and started docker
stop on runlevel [!2345]
respawn
chdir /wherever/your/fig/file/is
script
# Wait for docker to finish starting up first.
FILE=/var/run/docker.sock
while [ ! -e $FILE ] ; do
inotifywait -t 2 -e create $(dirname $FILE)
@heyimalex
heyimalex / descriptor.py
Last active August 29, 2015 13:59
Call Descriptor
"""
Call Descriptor
~~~~~~~~~~~~~~~
Allows you to dynamically modify an instance's __call__ value.
You may be asking, why not just use an attribute on the object
instead of this magical descriptor?
Good fucking question...
@heyimalex
heyimalex / react-promise-state.js
Last active July 14, 2017 05:46
Helper for dealing with promise-y state in React.js
const PENDING = 0;
const FULFILLED = 1;
const REJECTED = 2;
function PromiseState(component, stateKey, initial) {
this.component = component;
this.stateKey = stateKey;
var self = this;
@heyimalex
heyimalex / react-state-cache-mixin.js
Created August 4, 2014 19:52
React mixin for caching state in sessionStorage across mounts.
var StateCacheMixin = function(key) {
if (!Modernizr.sessionstorage) {
return {};
}
return {
componentWillMount: function() {
var prevState = sessionStorage.getItem(key);
if (prevState === null) return;
try {
prevState = JSON.parse(prevState);
@heyimalex
heyimalex / Selectize.jsx
Created August 15, 2014 17:30
React wrapper around Selectize.js
/** @jsx React.DOM */
// Wrapper around the selectize jQuery plugin.
module.exports = React.createClass({
getDefaultProps: function() {
return {
// HTML attrs
disabled: false,
multiple: false,
@heyimalex
heyimalex / MousetrapMixin.js
Created November 21, 2014 16:49
Mixin for components that bind to keyboard events using Mousetrap.
// Mixin for components that bind to keyboard events using Mousetrap.
// Use this.bind like you would Mousetrap.bind.
function nop(){}
module.exports = {
bind: function(keys, callback, action) {
this._mousetrapBindings.push([keys, action]);
Mousetrap.bind(keys, callback.bind(this), action);
@heyimalex
heyimalex / Semaphore.js
Last active August 29, 2015 14:11
Asynchronous concurreny control in Javascript
/**
* For limiting concurrency, Javascript style.
*
* Created for a document viewer; basically a number of thumbnails and
* a full image of the current page. "Changing the page" entailed
* changing the src of the full image, and the expectation was for that
* page to load. However, the thumbnails would saturate the browser's
* download limit, so the page wouldn't change until _all_ of the
* thumbnails had finished downloading. With 300+ page documents and
@heyimalex
heyimalex / History.js
Created June 12, 2015 21:23
Undo/Redo helper for immutable state changes.
export default function History(initialState) {
this.reset(initialState);
}
History.prototype = {
reset: function(initialState) {
this.history = [initialState];
this.pointer = 0;
this.tail = 0;