Subclassing Type¶
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
check()
, 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 samet()
function that was used to create it in the first place.Instances of
Type
have at
attribute that is automatically set to this value, so you can access it from one of the methods below asself.t
.- check(json_value)¶
Returns
True
if json_value is a member of this type’s value space andFalse
if it is not.>>> t("DateTime").check(u"2015-04-05T14:30") True >>> t("DateTime").check(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.
- 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, 'Decimal': teleport.core.DecimalType, 'Integer': teleport.core.IntegerType, 'JSON': teleport.core.JSONType, 'Map': teleport.core.MapType, 'Schema': teleport.core.SchemaType, 'String': teleport.core.StringType, 'Struct': teleport.core.StructType}