Skip to content

Instantly share code, notes, and snippets.

@diosmosis
diosmosis / exportsvg.py
Created March 31, 2011 03:55
Uses inkscape to export an svg file using an optional, external CSS file.
import os
import xml.dom.minidom
from optparse import OptionParser
# usage exportsvg whatever.svg --css=abc.css --output=whatever.png
# uses inkscape
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-c", "--css", dest="css", help="CSS stylesheet to use when exporting")
parser.add_option("-o", "--output", dest="output", help="specify the output file to use")
@diosmosis
diosmosis / sanitize_html.rb
Created July 8, 2011 15:10
HTML Sanitizer written in Ruby
require 'set'
# A SanitizationLevel holds information regarding an XML node's start tag.
# Levels are used to incrementally build sanitized HTML while dealing with
# incomplete nodes (ie, nodes that are never closed).
class SanitizationLevel
# Constructor.
def initialize(tag_name, sanitized, start_pos, end_pos, tagsize)
@tag_name = tag_name
@diosmosis
diosmosis / async.py
Created August 8, 2011 18:46
Asynchronous function calling with python and GTK.
import threading
from gi.repository import GObject
# calls f on another thread
def async_call(f, on_done):
"""
Starts a new thread that calls f and schedules on_done to be run (on the main
thread) when GTK is not busy.
Args:
@diosmosis
diosmosis / example.py
Created August 15, 2011 22:41
Python decorator that catches exceptions and logs a traceback that includes every local variable of each frame.
import os
from log_exceptions import log_exceptions
def throw_something(a1, a2):
raise Exception('Whoops!')
@log_exceptions(log_if = os.getenv('MYAPP_DEBUG') is not None)
def my_function(arg1, arg2):
throw_something(arg1 + 24, arg2 - 24)
@diosmosis
diosmosis / example.c
Created August 21, 2011 09:40
JNI Facade
#include <jni.h>
#include <mapper.h>
/* Maps the following fictitious Java classes.
class MyEntity {
MyEntity(int id, String name);
int getId();
void setId(int id);
@diosmosis
diosmosis / router.scala
Created December 13, 2011 23:32
Scala Experiment Iteration 0
import scala.collection.mutable.HashMap
/** Represents a segment in a router's tree of valid paths. Describes exactly what
* path segments are allowed to come after this one.
*/
class RouteTreeNode(pathSegment:String) {
/** The segment this node represents. */
private val segment = pathSegment
@diosmosis
diosmosis / router.scala
Created December 29, 2011 02:42
Scala Experiment Iteration 1
import scala.actors.Actor
import scala.actors.Actor._
import scala.collection.immutable.HashMap
import scala.collection.mutable.{HashMap => MutableHashMap}
/**
* The type passed to RouterActionInvokers.
*
* The 'message' member can be anything.
*/
@diosmosis
diosmosis / everything.scala
Created January 1, 2012 04:02
Scala Experiment Iteration 2
import scala.actors.Actor
import scala.actors.Actor._
import scala.collection.immutable.HashMap
import scala.collection.mutable.{HashMap => MutableHashMap}
import scala.util.Random
import scala.math.Ordering._
import scala.util.Sorting
/**
* The type passed to RouterActionInvokers.
@diosmosis
diosmosis / cakejam_air_example.cakejam.coffee
Created February 19, 2012 09:24
cakejam example (Adobe AIR target)
cakejam = require 'cakejam'
# aliases
jslib = cakejam.targets.jslib
aircert = cakejam.targets.aircert
airapp = cakejam.targets.airapp
# a JavaScript library that will hold all the code in one file
lightspeedjs = jslib 'lightspeedjs', __dirname,
sources: ['src/config.coffee',
@diosmosis
diosmosis / example_controller.coffee
Created February 19, 2012 10:39
Example snippets that use thewire
thewire = require 'thewire'
Layout = (data, more_data, body) ->
# ...
View1 = (pages) ->
# ...
View2 = (page) ->
# ...