Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / example.go
Last active August 27, 2015 02:50
Newlines in go.
type thing struct {
a, b int
c string
d uint
}
// this is valid go (in a function context)
v := thing{ a: 0,
b: 0,
@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 / 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 / tracker.lua
Last active January 25, 2016 09:32
Lua Tracker. A tracker in lua using the http://webscript.io interface. I believe it works, but I think there's a bug in the webscript system that's throwing 500s for url-encoded binary in GET requests basically making this useless.
function split(s, on)
local elems = {}
local buffer = ""
local i, c
for i = 1, #s do
c = string.sub(s, i,i)
if c == on then
table.insert(elems, buffer)
buffer = ""
else
@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 / fsck-all.sh
Last active August 29, 2015 14:19
One-Liner to check the integrity of all git repos in the current directory
find . -name '.git' -type d -prune \
| rev | cut -d '/' -f2- | rev | tr '\n' '\0' \
| xargs -0 -n1 -I% bash -c 'echo -n "%..."; cd "%"; git fsck 2>/dev/null 1>&2; if [ $? -eq "0" ]; then printf "\e[32mPASS\e[39m\n"; else printf "\e[31mFAIL\e[39m\n"; fi'
@joshkunz
joshkunz / ex.py
Created March 30, 2015 15:58
Python name mangling fun
class foo:
__local = 10
def local(self): return self.__local
def test(self, bar): return self.__local, bar.__local
class empty:
pass
ei= empty()
ei.__local = "hello from empty"
f = foo()