Skip to content

Instantly share code, notes, and snippets.

@numberoverzero
numberoverzero / worker-transferrable.html
Created April 29, 2014 01:18
WebWorker showing off transferrables
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
</head>
<body>
<div>Transferring an array</div>
<button id='bPopulate' onclick='populateArray()'>Populate Array</button>
<button id='bSend' onclick='sendArray()'>Send Array</button>
<div id='output'></div>
@numberoverzero
numberoverzero / volume.sh
Last active August 29, 2015 14:01
Control pulseaudio volume
#!/bin/bash
#=================
# commands:
# volume : get volume
# volume [0,100] : set volume, unmute
# volume [up|down] : step volume up or down by small pct, unmute
# volume mute : toggle mute
# volume muted : exit 0/print true if muted, else exit 0/print false
#=================
@numberoverzero
numberoverzero / chain.py
Last active August 29, 2015 14:01
Use functools.partial to nest function calls
"""
# First argument to the function should be the next
# object to invoke (next_handler below)
class EventHandler(object):
def __init__(self, name):
self.name = name
def handle(self, next_handler, event, data):
print("ENTER: Handler {} Event {}".format(self.name, event))
@numberoverzero
numberoverzero / chain_v2.py
Created June 10, 2014 21:41
Chain class with special-casing for __call__, which __getattr__ won't handle (same with __getattribute__)
"""
Why is __call__ a special case?
https://docs.python.org/3.4/reference/datamodel.html#object.__getattribute__
> Note: This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Special method lookup.
> In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the __getattribute__() method even of the object’s metaclass
"""
class Chain(object):
@numberoverzero
numberoverzero / multi_index.py
Last active August 29, 2015 14:06
Simple indexing into a list or iterable by name
def build_index(*fields):
'''each arg is (field_name, length) tuple'''
index = type('Index', (object,), {})
start = 0
for field, offset in fields:
s = slice(start, start + offset)
setattr(index, field, s)
start += offset
return index
@numberoverzero
numberoverzero / HomelessPack
Last active August 29, 2015 14:07
Approximate list of HomelessPack contents
SUMMARY
=============================
bartell's gift card
toothpaste
thermos
soap dish
chapstick
tictacs
shower gel
shaving cream
@numberoverzero
numberoverzero / Markdown Regex
Created October 31, 2014 06:05
Scratchpad for a simpler markdown-inspired language for compilation to a subset of regex
"[name] is [age:int] years old." -> "(?<name>[^ ]) is [(?<age>\d+) years old \."
----------------------------------------
Examples
"search [service]/[region] [metric]"
"search (?<service>[^ ^/]+)/(?<region>[^ ]+) (?<metric>[^ ]+)"
"stock (?<name>[^ ]) (?<op>((<|>|=)=?)) (?<value>[^ ]+)"
@numberoverzero
numberoverzero / gist:a9d4fd50b38b506dd552
Created December 4, 2014 10:25
Python Service Stack
API Scaffolding
Plan: Create a stack for re-use when rapidly building out APIs featuring authentication, authorization, throttling, versioning, structures. API specification should be language agnostic so SDK creation is reasonable in non-python languages.
Server Stack:
-- custom API specification --
API parser
marshmallow
-- application layer --
@numberoverzero
numberoverzero / index.py
Created February 10, 2015 21:22
generate a dict mapping items by the given field of each object
"""
Generate a mapping of a list of objects by the given attr.
"""
def index(objects, attr):
return {getattr(obj, attr): obj for obj in objects}
# Example
class Person(object):
@numberoverzero
numberoverzero / qsample.py
Last active August 29, 2015 14:15
Sample syntax for various query operations
# Single hash key, multiple filters
# =================================
# Pure condition syntax, no **kwargs overloading
# ----------------------------------------------
q = engine.query(Forum)
.key(Forum.name == "S3")
.filter(Forum.subscribers > MIN_SUBSCRIBERS,
Forum.views >= MIN_VIEWS)