Skip to content

Instantly share code, notes, and snippets.

View psobot's full-sized avatar

Peter Sobot psobot

View GitHub Profile
@psobot
psobot / plot.py
Created December 9, 2012 06:19
CoreAudio CAShow -> Graphviz Renderer
"""
1) Be someone crazy working with Apple's CoreAudio.
2) Put "CAShow(graph)" somewhere in your Objective-C code.
3) Copy the resulting output from your XCode Console window.
4) Do `pbpaste | python plot.py && open out.png`
5) See a very basic rendering of the crazy audio graph you've made.
"""
import re
import sys
@psobot
psobot / emitter.py
Created January 27, 2013 22:33
An absurdly simple, non-blocking emitter for Square's Cube data collection framework.
"""
Simple, asynchronous, nonblocking UDP emitter for Cube metrics.
"""
import json
import socket
from datetime import datetime
class Emitter(object):

So you want to use Node.js…

Current status: incomplete draft.

Speed

  • Computation: V8 very fast; significantly faster than most interpreted languages. The one very big exception is PyPy, which is in the same ballpark
@psobot
psobot / bad_scoping.py
Created March 12, 2013 21:27
Python Scoping Pitfalls
class MyClass(object):
def __init__(self):
self.foo = "I'm the correct variable!"
def do_something(self):
# Whoops, I forgot to write this as "self.foo".
print foo
if __name__ == "__main__":
foo = "Herp derp, I'm the wrong variable."
@psobot
psobot / json_list_incrby.lua
Created October 1, 2013 14:03
Atomic Redis INCRBY on a specific field of all JSON-encoded elements of a Redis list.
-- Let's say you've got a Redis list that's composed of JSON values:
-- -- {"a": 5.2, "b": "something blah blah"}
-- -- {"a": 4.3, "b": "something blah blah"}
-- -- {"a": 125.2, "b": "something blah blah"}
-- And let's say that you want to increment all of the "a" fields by n.
-- Call the below script with:
-- -- KEYS = [key_of_list_to_update]
-- -- ARGV = [json_key_to_incr, value_to_add]
local num = redis.call('llen', KEYS[1])
@psobot
psobot / bufferedreadqueue.py
Created November 9, 2012 18:05
Buffered Read Queue for Multiprocessing in Python
import Queue
import multiprocessing
import threading
class BufferedReadQueue(Queue.Queue):
def __init__(self, lim=None):
self.raw = multiprocessing.Queue(lim)
self.__listener = threading.Thread(target=self.listen)
self.__listener.setDaemon(True)
@psobot
psobot / jquery.punchout.js
Created December 16, 2012 19:03
Punching out text with HTML5 canvas
// Some really hacky code being used in my next blog redesign.
// by Peter Sobot (psobot.com) on December 16, 2012
;(function ( $, window, document, undefined ) {
var pluginName = 'punchout',
defaults = {
};
function Plugin( element, options ) {
this.element = element;
@psobot
psobot / NSDate+Extensions.swift
Created June 23, 2015 04:08
Swift NSDate Comparison Extension
// NSDate doesn't include overrides for standard comparison operators in Swift.
// This extension adds <, >, <=, >=, and ==, using NSDate's built-in `compare` method.
// MIT licensed.
func <=(lhs: NSDate, rhs: NSDate) -> Bool {
let res = lhs.compare(rhs)
return res == .OrderedAscending || res == .OrderedSame
}
func >=(lhs: NSDate, rhs: NSDate) -> Bool {
let res = lhs.compare(rhs)
@psobot
psobot / fmod.py
Created July 8, 2012 18:35
Binary File Change Monitor in Python
"""
Detect exactly which bytes have changed in a file.
Useful for binary reverse engineering:
- Start script
- Save file from program, changing one attribute
- See which byte(s) have changed
"""
import os
import traceback
@psobot
psobot / Context.sublime-menu
Created March 21, 2017 19:14
Rails "Copy Test Runner Command to Clipboard" for Sublime Text 2
[
{ "command": "get_single_test_run", "caption": "Copy Test Runner Command to Clipboard" },
{ "caption": "-", "id": "end" }
]