from functools import update_wrapper def genseq(x, y, Ns): def n_ary_f(x, *args): return x if not args else f(x, n_ary_f(*args)) update_wrapper(n_ary_f, f) return n_ary_f def seq(x, y): return ('seq', x, y) >>> help(seq) Help on function n_ary_f in module __main__: n_ary_f(x, *args)
@decorator def trace(f): indent = ' ' def _f(*args): signature = '%s(%s)' % (f.__name__, ', '.join(map(repr, args))) print '%s--> %s' % (trace.level*indent, signature) trace.level += 1 try: print '%s<-- %s === %s' % ((trace.level-1)*indent, signature, result) finally: trace.level -= 1 return result trace.level = 0 return _f [/python] [python] G = grammar(r""" Exp => Term [+-] Exp | Term Term => Factor [*/] Term | Factor Factor => Funcall | Var | Num | [(] Exp[)] Funcall => Var [(] Exp[)] Exps => Exp [,] Exps | Exp Var => [a-zA-Z_]\w* Num => [-+]?[.][0-9]*) """)
JSON = grammar(""" object => { } | { members } members => pair, members | pair pair => string: value array => [[][]] | [[] elements []] elements => value, elements | value value => string | number | object | array | true | false | null string => "[^"]*" number => int frac exp | int frac | int exp | int int => ->[1-9][0-9]* frac => [.][0-9]+ exp => [eE][-+]?[0-9]+ """, whitespace='\s*')
def inverse(f, delta=1/128.):
def f_1(y):
x = 0
while f(x) < y:
x += delta
return x if (f(x)-y < y-f(x-delta)) else x-delta
return f_1
def square(x): return x * x
print sqrt(100)
print sqrt(99)
print sqrt(100000000)
[/python]