Skip to content

Instantly share code, notes, and snippets.

@jiaaro
jiaaro / conv.py
Last active August 29, 2015 14:03
create hstore col
from django.db import connection
def hs_str(s):
s = unicode(s)
return s.replace('"', r'\"')
def dict_to_hstore(d):
hs_data = [u'"{0}" => "{1}"'.format(hs_str(k), hs_str(v)) for (k, v) in data.items()]
hs_data = u",".join(hs_data)
return "u'{0}'::hstore".format(hs_data)
@jiaaro
jiaaro / install_nodejs
Last active August 8, 2016 12:50
Put the following files in a folder called `bin` (so they'll be `bin/post_compile` and `bin/install_nodejs`)
#!/usr/bin/env bash
set -eo pipefail
NODE_VERSION=$(curl --silent --get https://semver.io/node/stable)
NODE_BASENAME=node-v${NODE_VERSION}-linux-x64
NODE_ARCHIVE="http://nodejs.org/dist/v${NODE_VERSION}/${NODE_BASENAME}.tar.gz"
# make a temp directory
tempdir="$( mktemp -t node_XXXX )"
rm -rf $tempdir
@jiaaro
jiaaro / exploit.js
Created May 29, 2014 14:45
XSS Game level 6
alert('adsf')
@jiaaro
jiaaro / marco_arment.py
Created March 7, 2014 14:07
Marco Compatibility
import warnings
import random
if random.random() > 0.999:
warnings.warn("Hi marco, nice day today, eh?")
@jiaaro
jiaaro / audioop.py
Last active January 2, 2016 12:59
add audioop.max() to pypy? twitter: @jiaaro - email: me@jiaaro.com
import struct
import math
from ctypes import create_string_buffer
class error(Exception):
pass
# this module redefines the names of some builtins that are used
max_ = max
@jiaaro
jiaaro / sub.js
Last active December 27, 2015 18:48
v1 of implementing xkcd substituions: http://xkcd.com/1288/
function replace(txt) {
txt = txt.replace(/witnesses/gi, "dudes I know");
txt = txt.replace(/allegedly/gi, "kinda probably");
txt = txt.replace(/new study/gi, "tumblr post");
txt = txt.replace(/rebuild/gi, "avenge");
txt = txt.replace(/space/gi, "spaaace");
txt = txt.replace(/google glass/gi, "virtual boy");
txt = txt.replace(/smartphone/gi, "pokédex");
txt = txt.replace(/electric/gi, "atomic");
txt = txt.replace(/senator/gi, "elf-lord");
@jiaaro
jiaaro / retirement calc.md
Last active December 3, 2022 18:51
A Simple, Easy-to-edit Retirement Saving Calculator (in python)

Retirement Calculator

  • ages are in years
  • contribution, and savings are in dollars
  • avg_annual_return is a ratio, so 1.07 is a 7% annual return

let's say I'm 25 years old, I am going to contribute $2000/yr in bonds (~5% return), and I've already invested $5700 in bonds

# add new database to addons before running this
# fill in the blank
# eg. DBNAME=HEROKU_POSTGRESQL_ROSE <- new Database
DBNAME=HEROKU_POSTGRESQL_ROSE
heroku maintenance:on
heroku scale web=0
heroku pgbackups:capture --expire
@jiaaro
jiaaro / getjson.js
Last active January 9, 2019 09:10
super simple implementation of getJSON
function getJSON(url, cb) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.status != 200 || xhr.readyState != 4) return;
cb(JSON.parse(xhr.responseText));
}
xhr.send();
};
@jiaaro
jiaaro / knn_solution.py
Created June 4, 2013 00:19
solution to the homework in "Machine Learning for Humans: K Nearest-Neighbor" at (http://www.jiaaro.com/KNN-for-humans/)
from collections import Counter
K = 3
dataset = [
# weight, color, # seeds, type
[303, 3, 1, "banana"],
[370, 1, 2, "apple"],
[298, 3, 1, "banana"],
[277, 3, 1, "banana"],
[377, 4, 2, "apple"],