Skip to content

Instantly share code, notes, and snippets.

@joshkunz
joshkunz / bencode.lua
Last active August 29, 2015 14:21
bencode.lua a lua library for serializing and de-serializing bencoded (https://en.wikipedia.org/wiki/Bencode) values.
-- bencode.lua: A lua bencoding serializer and de-serializer
--
-- The MIT License (MIT)
--
-- Copyright (c) 2015 Josh Kunz
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@joshkunz
joshkunz / gist:d238cc9e624f83bb24fd
Created June 29, 2015 21:11
Continuted fraction square root function.
def msqrt(n):
x = 1
for i in xrange(100):
x = 1 + (float(n - 1) / (x + 1))
return x
@joshkunz
joshkunz / gist:05dafb42272ec26c4bc8
Created July 16, 2015 22:07
Prime factorization code
import math
def memoize(f):
memo = {}
def wrapper(val):
if val not in memo:
memo[val] = f(val)
return memo[val]
return wrapper
@joshkunz
joshkunz / t.go
Created September 4, 2015 02:36
Go routines with manual yielding.
package main;
import "fmt"
import "runtime"
func main() {
runtime.GOMAXPROCS(1)
a := make(chan bool)
b := make(chan bool)
@joshkunz
joshkunz / sealer_unsealer.py
Created October 2, 2015 04:34
A python implementation of the sealer/unsealer pattern from "E in a Walnut"
# Returns two classes, a "Sealer", and an "Unsealer".
# Lets call the Sealer "s" and the Unsealer "u".
# For any pair of s and u returned from makeBrandPair, s.seal(value)
# (where "value" can be any python object) returns a boxed version of
# value that cannot be extracted unless you have a reference to the
# unsealer u (not 100% sure on this, but it seems to be true).
# When you call u.unseal(box), the value originally sealed into the
# box is returned. A separate call to makeBrandPair will return
# a different sealer/unsealer pair that only work with eachother.
def makeBrandPair():
@joshkunz
joshkunz / _readme.md
Last active November 6, 2015 21:06
A simple protocol for sending synchronous binary messages between two hosts.

Channel

A simple channel implementation for python for prototyping and debugging. It is thread-safe.

API

Channel.listen(<host>, <port>, reuse=True, echo=True) -> iterator over client channel objects

Args:

  • ``: The IP address to listen on.
@joshkunz
joshkunz / scrape.js
Last active November 15, 2015 23:55
Script to scrape netflix ranking history found at https://www.netflix.com/MoviesYouveSeen . Sorry about the formatting, I just typed it in the dev-tools box. Outputs as csv in the form "Netflix movie id,date,title,rating". Ratings are 1-5, -3 means "not interested".
var elems = document.querySelectorAll("li.retableRow"); for (i = 0; i < elems.length; i++) { var item = elems[i]; var mid = item.getAttribute("data-movieid"); var date = item.querySelector(".col.date").textContent; var title = item.querySelector(".col.title a").textContent; var rating = item.querySelector(".col.rating div").getAttribute("data-your-rating"); console.log(mid + "," + date + "," + "\"" + title + "\"" + "," + rating); }
@joshkunz
joshkunz / upload.sh
Last active December 11, 2015 00:28
This is what I have of a shell based moodle upload system. The username should be passed as the first argument. This (with the exception of logging in) does *not* work.
#!/bin/bash
# Where the cookies are stored
COOKIES='cookies.tmp'
# Where the pages are temporarily stored
PAGE='page.tmp'
# value matching regex constant
VALUE_MATCHER='^ value="([^"]*)"'
@joshkunz
joshkunz / gist:4998910
Created February 20, 2013 19:59
The locking section of some caching code I wrote.
@classmethod
def resolve(cls, request, caching_client):
"""If this request isn't yet cached, cache it, otherwise return
a file containing the cached contents"""
hash = cls.hash(request)
is_caching = False
active_thread = None
# If there is no cached file
if not cls.hit(hash):
# obtain a lock for the active caches
@joshkunz
joshkunz / gist:5015438
Created February 22, 2013 18:14
Get all possible 3 tuple combinations of a-z2-9.
from itertools import product
import string
id_combos = product("".join((string.ascii_lowercase, string.digits[2:])), repeat=3)
ids = map("".join, id_combos)
print len(ids), "ids found."