Skip to main content

namedtuple with validation

Project description


$ pip install typedtuple

basic definition

from typedtuple import TypedTuple

# definition dictionary is converted to voluptuous.Schema(definition_dict, required=True)
Vector = TypedTuple('Vector', {'x': float, 'y': float, 'z': float})
v0 = Vector(x=10.0, y=20.0, z=30.0)
print v0.x  # 10.0
v1 = Vector(x=10, y=20)  # raises exception

using voluptuous schema

from voluptuous import Schema

# can use voluptuous.Schema as definition
Vector = TypedTuple('Vector', Schema({'x': float, 'y': float, 'z': float}, required=True))

using @schema decorator

from math import sqrt
from typedtuple import schema

@schema({'x': float, 'y': float, 'z': float})
class Vector(object):
    def length(self):
        return sqrt(pow(self.x, 2.0) + pow(self.y, 2.0) + pow(self.z, 2.0))

v = Vector(x=1.0, y=2.0, z=2.0)
print v.length  # 3.0

using voluptuous functions

from voluptuous import Coerce
from typedtuple import schema

@schema({'x': Coerce(float), 'y': Coerce(float)})
class Vector(object):

v0 = Vector(x=10, y='20.0')
print v0.x  # 10.0
print v0.y  # 20.0

using OrderedDict

from collections import OrderedDict
from typedtuple import schema

@schema(OrderedDict((('x', float), ('y', float), ('z', float))))
class Vector(object):

print Vector._fields  # ('x', 'y', 'z')


TypedTuple is tuple. so…

from collections import OrderedDict
from typedtuple import TypedTuple

Vector0 = TypedTuple('Vector0', OrderedDict((('x', float), ('y', float))))
Vector1 = TypedTuple('Vector1', OrderedDict((('y', float), ('x', float))))

v0 = Vector0(x=10.0, y=20.0)
v1 = Vector1(x=20.0, y=10.0)
v0 == v1  # True


use nose for test


Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for typedtuple, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size typedtuple-0.0.2.tar.gz (2.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page