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 / gist:30413
Created November 30, 2008 09:43
example code for a Python AST interpreter written in Python
# sequence of statements to translate:
def fib(n):
if n < 2:
return 1
return fib(n - 1) + fib(n - 2)
print fib(20)
# ------------------------------------------------------------------------------------------------
@sma
sma / repl-client.py
Created December 14, 2008 11:16
remote Python console
#!/usr/bin/env python
from xmlrpclib import ServerProxy
client = ServerProxy('http://localhost:8877')
while True:
try:
line = raw_input(">>> ")
if line:
@sma
sma / gist:38336
Created December 20, 2008 14:39
scan a Python file for Ruby-like string replacements and replace them with Python-like replacements
import re, sys, tokenize
RE = re.compile(r'#\{([^}]+)\}')
def transform(s):
args = []
def sub(m): args.append(m.group(1)); return "%s"
s = RE.sub(sub, s)
return "(%s %% (%s,))" % (s, ", ".join(args)) if args else s
@sma
sma / xref.py
Created December 24, 2008 10:44
xref a number of python files (only functions)
# xref a number of python files (only functions)
import re, sys
from collections import defaultdict
DEF_RE = re.compile(r"\s*def\s+(\w+)\s*\(")
CALL_RE = re.compile(r"\b(\w+)\(")
repo = defaultdict(lambda:defaultdict(dict))
@sma
sma / parse.py
Created December 31, 2008 11:03
example of parsing a yaml-like key-value data structure
# encoding: utf-8
s = u"""
title: An example
author: Stefan
date: 2008-12-02 12:03:00
text:
This is the blog entry and
a longer text which can have
paragraphs.
@sma
sma / indented-text.py
Created January 3, 2009 11:48
example of parsing indented text using an indents stack
# example of parsing indented text using an indents stack
def parse(lines):
output = []
indents = []
def set_indent(i, begin, end):
indents.append((i, end))
output.append(begin)
@sma
sma / aml.py
Created February 21, 2009 19:11
# node = '(' NAME {attribute} {node} ')'
# attribute = NAME '=' VALUE
import re
class Node(object):
def __init__(self, name, attributes=None, children=None):
self.name, self.attributes, self.children = name, attributes or {}, children or []
def __repr__(self):
import re
class Scanner(object):
def __init__(self, *rules):
self.rules = rules
self.re = re.compile("|".join("(" + r[0] + ")" for r in rules))
def scan(self, s):
for m in self.re.finditer(s):
try:

Clojure now supports mutual recursive local functions using letfn:

(defn ring [n] 
  (letfn [(a [n] (if (zero? n) n (b (dec n)))) 
          (b [n] (if (zero? n) n (c (dec n)))) 
          (c [n] (if (zero? n) n (a (dec n))))] 
     (c n))) 
(ring 1000)

Using trampoline, you can even make this stackless:

state = 0
for line in lines:
if match(r"^\s*Scenario:\s*(.*)$", line):
current_scenario = new Scenario(match.r(1))
current_feature.append_scenario(current_scenario)
state = 1
if match(r"^\s*Given (.*)$", line):
if state != 1: raise Error("unexpected Given")
current_scenario.append_given_step(match.r(1).strip())
if match(r"^\s*When (.*)$", line):