Utilities that help you write declarative code: instead of saying how something should be done, you describe what the results look like (examples).
Pytyp can help you:
Note
The ideas behind this library are described in the paper Algebraic ABCs.
The library has been almost completely rewritten for the 2.0 release. Public APIs have changed. You may need to fix your code when updating.
To install from PyPI:
easy_install pytyp
Warning
This project is Python 3 only.
For source see Google Code; for support email Andrew Cooke.
Testing type specifications (pytyp.spec.abcs):
>>> isinstance([1,2,None,3], Seq(Opt(int)))
True
>>> isinstance([1,2,None,3.0], Seq(Opt(int)))
False
Verifying function arguments (pytyp.spec.check):
>>> def myfunction(a:int, b:str) -> int:
... return len(a * b)
>>> myfunction(2, 'foo')
6
>>> myfunction('oops', 'banana')
Traceback (most recent call last):
...
TypeError: Type str inconsistent with 'oops'.
Loading JSON data into Python classes:
>>> class Example():
... def __init__(self, foo):
... self.foo = foo
... def __repr__(self):
... return '<Example({0})>'.format(self.foo)
>>> class Container():
... def __init__(self, *examples:[Example]):
... self.examples = examples
... def __repr__(self):
... return '<Container({0})>'.format(','.join(map(repr, self.examples)))
>>> loads = make_loads(Container)
>>> loads('[{"foo":"abc"}, {"foo":"xyz"}]')
<Container(<Example(abc)>,<Example(xyz)>)>
This is very inobtrusive - the type expected is given to make_loads() and “chained” through type annotations in the constructors - but relies extensively on pytp. For example, the annotation of *examples is a type specification (equivalent to Seq(Example)), and the implementation uses’s pytyp’s support for iteration and dynamic dispatch by type. This gives the command surprising flexibility. For example, the required class can be given as set of alternatives (eg. Alt(Container,Foo,Bar)) and the “right” solution will be returned, depending on the input data.