Skip to content

Instantly share code, notes, and snippets.

View sma's full-sized avatar

Stefan Matthias Aust sma

  • I.C.N.H GmbH
  • Kiel
View GitHub Profile
@sma
sma / pyspec.py
Created August 3, 2009 18:08
pyspec compiler
#!/usr/bin/env python
import re
def pyspec(source):
def translate(lines):
def name(n):
return re.sub("_+", "_", re.sub(r"[\s\W]+", "_", n)).strip("_")
def on_describe(m):
r"""(\s*)describe\s+("[^"]*"|'[^']*'|[^:]+?)\s*:"""
# shell script to copy styled text from textmate to keynote
# input: selected text or document, output: discard, key: ctrl+alt+cmd+C
ruby -e'
require "jcode"
$KCODE="U"
require "#{ENV["TM_BUNDLE_SUPPORT"]}/lib/doctohtml.rb"
require "#{ENV["TM_SUPPORT_PATH"]}/lib/progress.rb"
unit = ENV.has_key?("TM_SELECTED_TEXT") ? "selection" : "document"
TextMate.call_with_progress(:message => "Creating HTML version of #{unit}…") do
print document_to_html( STDIN.read, :include_css => true )
@sma
sma / example.py
Created August 23, 2009 09:40
a simple key/value store
wiki = KVStore("/tmp/wiki.kvstore")
with wiki.write_transaction:
wiki["home1"] = "This is the home page 1"
wiki["home2"] = "This is the home page 2"
with wiki.read_transaction:
for name in wiki:
print name, "=", wiki[name]
@sma
sma / array2d.py
Created September 30, 2009 12:08
class ListOfListArray:
def __init__(self, width, height, default=None):
self.width, self.height = width, height
self.values = [[default] * width for i in range(height)]
def __getitem__(self, (x, y)):
return self.values[y][x]
def grow_height(self, dy, default=None):
self.height += dy
@sma
sma / Python.java
Created October 7, 2009 12:15
minimal python byte code interpreter to run the usual "fib" benchmark
package sma.python;
import java.util.HashMap;
import java.util.Map;
/**
* A minimal python byte code interpreter to run the usual "fib" benchmark.
*
* <pre>
* def fib(n):
@sma
sma / gist:217505
Created October 24, 2009 12:39
compute probabilities of Savage Worlds deaths
import random
def d(n):
r = random.randint(1, n)
return r if r < n else r + d(n)
def dw(n):
return max(d(n), d(6))
def old_rules(n):
@sma
sma / gist:236075
Created November 16, 2009 15:53
Cucumber steps in different programming languages
>>>> Gherkin <<<<
As a developer
In Order to find the best scripting solution
So that I can compare different implementation languages for Cucumber
Given I have 1h of time
When I write something about Cucumber
Then others should like that
@sma
sma / parse_kv.py
Created December 20, 2009 20:40
parse a simple key-value data structure
example = """
start
{
position1 "-1022.963745,793.409607,8.966581"
position2 "-1024.971558,70.542549,416.005066"
name "Start"
time 0.0
active 1
give
{
@sma
sma / ruby.py
Created December 25, 2009 17:56
enough Python code to simulate a run a simple Ruby fragment in AST form
class RbClass:
def __init__(self):
self.methods = {}
def define_method(self, name, method):
self.methods[name] = method
def lookup(self, name):
return self.methods[name]
@sma
sma / basic.py
Created December 27, 2009 16:50
A simple BASIC interpreter that can run the old HAMURABI game
from __future__ import division
from math import trunc
from random import random
import re
TOKENS = re.compile(r'(?<=REM).*|\.?\d+|\w+\$?|[():;=+\-*/]|<[=>]?|>=?|"[^"]*"')
class Basic(object):
def __init__(self, filename):
self.tokens = []