gnome.persist.extend_colander¶
Extend colander’s basic types for serialization/deserialization of gnome specific types
Classes¶
A type representing a Python |
|
A Tuple subclass that provides defaults from child nodes. |
|
A subclass of |
|
A subclass of |
|
A subclass of |
|
A subclass of |
|
Add a type to serialize/deserialize timedelta objects |
|
A type which represents a variable-length sequence of nodes, |
Module Contents¶
- class gnome.persist.extend_colander.LocalDateTime(*args, **kwargs)¶
Bases:
colander.DateTime
A type representing a Python
datetime.datetime
object.This type serializes python
datetime.datetime
objects to a ISO8601 string format. The format includes the date, the time, and the timezone of the datetime.The constructor accepts an argument named
default_tzinfo
which should be a Pythontzinfo
object. Ifdefault_tzinfo
is not specified the default tzinfo will be equivalent to UTC (Zulu time). Thedefault_tzinfo
tzinfo object is used to convert ‘naive’ datetimes to a timezone-aware representation during serialization. Ifdefault_tzinfo
is explicitly set toNone
then no default tzinfo will be applied to naive datetimes.You can adjust the error message reported by this class by changing its
err_template
attribute in a subclass on an instance of this class. By default, theerr_template
attribute is the stringInvalid date
. This string is used as the interpolation subject of a dictionary composed ofval
anderr
.val
anderr
are the unvalidatable value and the exception caused trying to convert the value, respectively. These may be used in an overridden err_template as${val}
and${err}
respectively as necessary, e.g._('${val} cannot be parsed as an iso8601 date: ${err}')
.For convenience, this type is also willing to coerce
datetime.date
objects to a DateTime ISO string representation during serialization. It does so by using midnight of the day as the time, and uses thedefault_tzinfo
to give the serialization a timezone.Likewise, for convenience, during deserialization, this type will convert
YYYY-MM-DD
ISO8601 values to a datetime object. It does so by using midnight of the day as the time, and uses thedefault_tzinfo
to give the serialization a timezone.If the
colander.null
value is passed to the serialize method of this class, thecolander.null
value will be returned.The subnodes of the
colander.SchemaNode
that wraps this type are ignored.- strip_timezone(_datetime)¶
- serialize(node, appstruct)¶
Serialize a DateTime object
returns an iso formatted string
- deserialize(node, cstruct)¶
- class gnome.persist.extend_colander.DefaultTuple¶
Bases:
colander.Tuple
A Tuple subclass that provides defaults from child nodes.
Required because Tuple returns colander.null by default when
appstruct
is not provided, instead of creating a Tuple of default values.- serialize(node, appstruct)¶
- class gnome.persist.extend_colander.NumpyFixedLen¶
Bases:
colander.Tuple
A subclass of
colander.Tuple
that converts itself to a Tuple and back to a numpy array. This is used to define schemas for Numpy arrays that have a fixed size like WorldPoint, 3D velocity of SimpleMover.- serialize(node, appstruct)¶
Serialize a fixed length numpy array
- deserialize(node, cstruct)¶
- class gnome.persist.extend_colander.NumpyArray¶
Bases:
colander.List
A subclass of
colander.List
that converts itself to a more general numpy array of greater than length 1.- serialize(node, appstruct)¶
Serialize a numpy array
- deserialize(node, cstruct)¶
- class gnome.persist.extend_colander.DatetimeValue2dArray(accept_scalar=False)¶
Bases:
colander.Sequence
A subclass of
colander.Sequence
that converts itself to a numpy array usinggnome.basic_types.datetime_value_2d
as the data type.- todo: serialize/deserialize must happen for each element - not very
efficient.
- serialize(node, appstruct)¶
Serialize a 2D Datetime value array
- deserialize(node, cstruct)¶
Along with the normal
node
andcstruct
arguments, this method accepts an additional optional keyword argument:accept_scalar
. This keyword argument can be used to override the constructor value of the same name.If
accept_scalar
isTrue
and thecstruct
does not have an__iter__
method or is a mapping type, the value will be turned into a single element list.If
accept_scalar
isFalse
and thecstruct
does not have an__iter__
method or is a mapping type, ancolander.Invalid
error will be raised during serialization and deserialization.The default of
accept_scalar
isNone
, which means respect the defaultaccept_scalar
value attached to this instance via its constructor.
- class gnome.persist.extend_colander.DatetimeValue1dArray(accept_scalar=False)¶
Bases:
colander.Sequence
A subclass of
colander.Sequence
that converts itself to a numpy array usinggnome.basic_types.datetime_value_2d
as the data type.- serialize(node, appstruct)¶
Along with the normal
node
andappstruct
arguments, this method accepts an additional optional keyword argument:accept_scalar
. This keyword argument can be used to override the constructor value of the same name.If
accept_scalar
isTrue
and theappstruct
does not have an__iter__
method or is a mapping type, the value will be turned into a single element list.If
accept_scalar
isFalse
and theappstruct
does not have an__iter__
method or is a mapping type, ancolander.Invalid
error will be raised during serialization and deserialization.The default of
accept_scalar
isNone
, which means respect the defaultaccept_scalar
value attached to this instance via its constructor.
- deserialize(node, cstruct)¶
Along with the normal
node
andcstruct
arguments, this method accepts an additional optional keyword argument:accept_scalar
. This keyword argument can be used to override the constructor value of the same name.If
accept_scalar
isTrue
and thecstruct
does not have an__iter__
method or is a mapping type, the value will be turned into a single element list.If
accept_scalar
isFalse
and thecstruct
does not have an__iter__
method or is a mapping type, ancolander.Invalid
error will be raised during serialization and deserialization.The default of
accept_scalar
isNone
, which means respect the defaultaccept_scalar
value attached to this instance via its constructor.
- class gnome.persist.extend_colander.TimeDelta¶
Bases:
colander.Float
Add a type to serialize/deserialize timedelta objects
- serialize(node, appstruct)¶
- deserialize(*args, **kwargs)¶
- class gnome.persist.extend_colander.OrderedCollectionType(accept_scalar=False)¶
Bases:
colander.Sequence
A type which represents a variable-length sequence of nodes, all of which must be of the same type.
The type of the first subnode of the
colander.SchemaNode
that wraps this type is considered the sequence type.The optional
accept_scalar
argument to this type’s constructor indicates what should happen if the value found during serialization or deserialization does not have an__iter__
method or is a mapping type.If
accept_scalar
isTrue
and the value does not have an__iter__
method or is a mapping type, the value will be turned into a single element list.If
accept_scalar
isFalse
and the value does not have an__iter__
method or is a mapping type, ancolander.Invalid
error will be raised during serialization and deserialization.The default value of
accept_scalar
isFalse
.If the
colander.null
value is passed to the serialize method of this class, thecolander.null
value is returned.