the max repository

ize

This page has been moved to maxrepo.info 

 

Ize is a python module providing a set of function decorators.

The main functionalities that it offers are :

Decorators helpers allow to "post-decorate" whole modules or functions sets, so that type checking and traces (for exemple) can be be easyly disabled removed (to export) or added to extern module.

Download now !

Here are a brief description of these points

Operator support

The operize module defines a set of classes and interfaces to add operators on function.

>>> from ize import *
>>> @arithize_d
... def f(x):
... x+10
...
>>> @arithize_d
... def g(x):
... return x+1
...
>>> @arithize_d
... def f(x):
... return 10*x
...
>>> h=f+g
>>> h(1)
12
>>> f[g](2)
30

Typechecking

 

The typechecker module allows function type checking in docstring.

It's a front end to the typecheck module and uses TPG as parser. At the moment theses modules must be installed separatelly.

>>> from ize.typechecker import *
>>> @tize
... def f(x):
... ":: int -> int"
... return x+10
...

This defines f as taking an int and returning an int.

>>> f(4)
14
>>> f(4.0)
Traceback (most recent call last):
File "", line 1, in ?
File "/home/Max/python/ize/typechecker.py", line 248, in checked_func
raise exs[0]
typecheck.TypeCheckError: Argument x: for 4.0, expected , got

More complicated checks could be performed.

>>> @tize
... def g(x):
... ":: (~A & (int | float)) -> ~A"
... return x+10
...

This defines g as taking an int or a float and returning the same. A is a parametric type.

>>> g(5)
15
>>> g(4.0)
14.0
>>> g("a")
Traceback (most recent call last):
File "", line 1, in ?
File "/home/Max/python/ize/typechecker.py", line 248, in checked_func
raise exs[0]
typecheck.TypeCheckError: Argument x: for a, expected And(Or(, ), TypeVariable(A)), got

Trace

The tracer module allows function tracing.

The default TracerManager displays traces only when a exception is not caught.

Partial calls

The curry module allows partial calls of function
>>> from ize import *
>>> @cize
... def f(x,y):
... return x+y
...
>>> g=f(10)
>>> g
@f(10, ?)
>>> g(5)
15

g is the "partial" call of f with x=10.

Decorator and metaclass support

Decorator

The decorize module provides a Decorator class that defines add operator as compound.
>>> from ize import *
>>> @dize
... def totize(f):
... def dec(*a, **k):
... print "toto"
... return f(*a, **k)
... return dec
...
>>> @dize
... def titize(f):
... def dec(*a, **k):
... print "titi"
... return f(*a, **k)
... return dec
...
>>> d=totize+titize
>>> @d
... def f(x):
... return x+10
...
>>> f(4)
toto
titi
14

Metaclass

The meta module defines common metaclass and helper.
>>> from ize import *
>>> from ize.typechecker import *

>>> def template(t,v):
... class A_t_v:
... __metaclass__=classname_m("A_%s_%s" % (t,v))+decorize_m(tize)+format_doc_m(dict(t=t, v=v))
... def f(self,x):
... ":: $, %(t)s -> %(t)s"
... return x+v
... return A_t_v
...
>>> A1=template("int", 10)

>>> a.f.__doc__
':: $, int -> int'

>>> A1=template("int", 10) >>> a=A1()
>>> a.f(5)
15
>>> a.f(5.0)
Traceback (most recent call last):
File "", line 1, in ?
File "/home/Max/python/ize/typechecker.py", line 248, in checked_func
raise exs[0]
typecheck.TypeCheckError: Argument x: for 5.0, expected , got

 

 

 

 

 


Dernière modification le 17 May 2006 : ize