Skip to content

Instantly share code, notes, and snippets.

View jacob414's full-sized avatar
💭
Retired but semi-active, hobby projects & activism

Jacob Oscarson jacob414

💭
Retired but semi-active, hobby projects & activism
View GitHub Profile
@jacob414
jacob414 / backbone-tutorial.js
Created November 18, 2010 12:54
Minimal example of data handling in Backbone.js
/* Scaled-down Backbone.js demonstration
* By Jacob Oscarson (http://twitter.com/jacob414), 2010
* MIT Licenced, see http://www.opensource.org/licenses/mit-license.php */
$(function() {
window.ulog = function(msg) { $('#log').append($('<div>'+msg+'</div>')); }
// Faking a little bit of Backbone.sync functionallity
Backbone.sync = function(method, model, succeeded) {
ulog('<strong>'+method + ":</strong> " + model.get('label'));
if(typeof model.cid != 'undefined') {
@jacob414
jacob414 / repr.coffee
Created December 9, 2010 11:17
Quick and dirty Javascript repr() function using CoffeeScript
repr = (o, depth=0, max=2) ->
if depth > max
'<..>'
else
switch typeof o
when 'string' then "\"#{o.replace /"/g, '\\"'}\""
when 'function' then 'function'
when 'object'
if o is null then 'null'
if _.isArray o
@jacob414
jacob414 / groupby.coffee
Created January 5, 2011 17:48
A grouping (see python's itertools.groupby()) function in CoffeeScript
# Edit 1, thx @satyr!
groupby = (iter, keyfn) ->
grouped = {}
(grouped[keyfn e] ?= []).push e for e in iter
grouped
@jacob414
jacob414 / usig.coffee
Created February 17, 2011 15:07
Micro global message bus in 4 LOC CoffeeScript
# Really, really tiny message bus
# Yes, I know it leaks.
usig._bus = {}
usig.connect = (msg, cb) -> (usig._bus[msg] ?= []).push(cb)
usig.signal = (msg, args...) ->
cb args... for cb in usig._bus[msg] if usig._bus[msg]
@jacob414
jacob414 / weinre-senchatouch.html
Created February 26, 2011 22:19
Weinre/Sencha Touch edge-case
<!-- Assumes Weinre running on localhost, and the file accessed via -->
<!-- another webserver also running on localhost. Accessing this file -->
<!-- locally (file://[..] protocol) doesn't provoke the issue. -->
<!-- Tried on OS X and Linux (recent Ubuntu) -->
<!DOCTYPE html>
<html>
<head>
<title>Weinre & Sencha Touch</title>
<script src="sencha-touch-1.0.1a.js"></script>
<script src="sencha-weinre.js"></script>
@jacob414
jacob414 / log-webkit-events.js
Created March 1, 2011 09:39
Logs as many events as possible
// Log as many events as possible, with event names grabbed from
// webkit sources:
// http://svn.webkit.org/repository/webkit/trunk/Source/WebCore/dom/EventNames.h
known = [
'abort',
'beforecopy',
'beforecut',
'beforeload',
'beforepaste',
@jacob414
jacob414 / adhoc_watcher.rb
Created March 29, 2011 20:15
Ruby FSEvents & rsync insanely easy to write directory mirror
# Just added a subprocess call to rsync to one of rb-fsevent's example
# scripts. Spookily easy.
require 'rubygems'
require 'rb-fsevent'
require 'open3'
include Open3
fsevent = FSEvent.new
@jacob414
jacob414 / main.coffee
Created May 27, 2011 14:55
MMMM, delicious! Green tea mixed with coffee!
class BaseScreen extends Ext.Panel
title: 'Base panel'
iconCls: 'info'
class Screen1 extends BaseScreen
title: 'First screen'
html: 'Content of one screen'
class Screen2 extends BaseScreen
title: 'Second screen'
@jacob414
jacob414 / func_subclass.py
Created September 21, 2011 13:02
Subclass via function in Python
def subclass(Cls, name=None):
class _(Cls):pass
_.__name__ = name or 'subclass'
return _
@jacob414
jacob414 / test_funcargs_combo.py
Created October 13, 2011 08:48
Minimal example of py.test funcargs where one parameter is static and the other varying.
import py
def pytest_generate_tests(metafunc):
fargs = {}
if 'static' in metafunc.funcargnames:
fargs['static'] = 'stays the same'
if 'different' in metafunc.funcargnames:
for variant in (1,2):