API Reference

Type Classes

What is called type in the specification is represented in this implementation by subclasses of Type. Type instances, conveniently, are instances of those classes.

class teleport.Type

Conceptually, an instance of this class is a value space, a set of JSON values. As such, the only method defined by the Teleport specification is contains(), everything else is extentions made by this implementation.

When a type needs to access the t() function from one of its methods (for recursive serialization, for example), it should use the same t() function that was used to create it in the first place.

Instances of Type have a t attribute that is automatically set to this value, so you can access it from one of the methods below as self.t.

contains(json_value)

Returns True if json_value is a member of this type’s value space and False if it is not. You don’t have to override this method if you implement from_json().

>>> t("DateTime").contains(u"2015-04-05T14:30")
True
>>> t("DateTime").contains(u"2007-04-05T14:30 DROP TABLE users;")
False
from_json(json_value)

Convert JSON value to native value. Raises Undefined if json_value is not a member of this type. By default, this method returns the JSON value unchanged.

>>> t("DateTime").from_json(u"2015-04-05T14:30")
datetime.datetime(2015, 4, 5, 14, 30)
to_json(native_value)

Convert valid native value to JSON value. By default, this method returns the native value unchanged, assuming that it is already in the format expected by the json module.

>>> t("DateTime").to_json(datetime.datetime(2015, 4, 5, 14, 30))
u"2015-04-05T14:30"
class teleport.ConcreteType(t)

Subclass this to tell Teleport that your custom type is concrete.

class teleport.GenericType(t, param)

Subclass this to tell Teleport that your custom type is generic.

process_param(param)

Takes the type parameter in its JSON form and raises Undefined if it is invalid. This method is called when the type is instantiated. By default, it sets self.param to param. It may be useful to set other properties, they may be accessed later by from_json() or other methods.

teleport.CORE_TYPES

A dict mapping type names to subclasses of Type. When this class gets instantiated, the type map is initiated with types taken from this dict.

>>> from teleport import CORE_TYPES
>>> CORE_TYPES
{'Array': teleport.core.ArrayType,
 'Boolean': teleport.core.BooleanType,
 'DateTime': teleport.core.DateTimeType,
 'Float': teleport.core.FloatType,
 'Integer': teleport.core.IntegerType,
 'JSON': teleport.core.JSONType,
 'Map': teleport.core.MapType,
 'Schema': teleport.core.SchemaType,
 'String': teleport.core.StringType,
 'Struct': teleport.core.StructType}

The t Function

The Teleport specification defines a mathematical function t, which maps type definitions to type instances. In Teleport, this mapping is performed by instances of TypeMap. These instances behave like functions and the t() function that you import from the teleport module is actually an instance of this class:

>>> from teleport import t
>>> t
<teleport.core.TypeMap at 0x7ffcb08e7c10>
class teleport.TypeMap
__call__(schema)

When you call the t() function, you are actually calling this method. You will rarely want to override this directly.

Parameters:schema – a JSON value
Returns:a Type instance
get_custom_type(name)

Override this method to enable dynamic type search. It gets called if the requested type is neither a core type nor a type added by register(). In that case, this is the last resort before Undefined is thrown.

Parameters:name – a string
Returns:a subclass of Type or None
register(name)

Used as a decorator to add a type to the type map.

@t.register("Truth")
class TruthType(ConcreteType):

    def contains(self, value):
        return value is True

Exceptions

class teleport.Undefined