pyrocko.guts¶
Lightweight declarative YAML and XML data binding for Python.
Functions
|
Clone guts object tree. |
|
Serialize to YAML. |
|
Serialize to XML. |
|
Get all elements matching a given ypath specification. |
|
Generator yielding elements matching a given ypath specification. |
|
Set elements matching a given ypath specification. |
|
Get dict of guts object attributes. |
Classes
|
Placeholder for any object. |
|
Placeholder for |
|
Placeholder for |
|
Any out of a set of different types. |
|
Placeholder for |
|
Placeholder for a UTC timestamp which (de)serializes as a date string. |
Base class for default value factories. |
|
|
Placeholder for |
|
Placeholder for |
|
Placeholder for |
|
Placeholder for |
|
Any |
|
Placeholder for |
|
Base class for Guts objects. |
|
Default value factory for |
|
Base class for simple str-serializable Guts objects. |
|
Placeholder for |
|
Any |
|
Any |
|
Any |
|
Base class for Guts type definitions. |
|
Placeholder for a UTC timestamp. |
|
Placeholder for |
|
Placeholder for |
|
Any |
Exceptions
Raised when invalid arguments would be used in an object's initialization. |
|
Raised when an object is invalid according to its definition. |
|
This exception is raised for invalid ypath specifications. |
- class TBase(default=None, optional=False, xmlstyle='element', xmltagname=None, xmlns=None, help=None, position=None)[source]¶
Bases:
object
Base class for Guts type definitions.
- Parameters:
default – Default value or
DefaultMaker
object (seeObject.D()
) to be used to generate a default.optional (bool) – If
True
, the attribute is optional and may be set toNone
.xmlstyle (str) – Controls how the attribute is serialized in XML. :Choices:
'element'
,'attribute'
,'content'
. Only scalar andSObject
values may be serialized as'attribute'
. Only one attribute in a class may be serialized as'content'
.xmltagname (str) – XML tag name to be used. By default, the attribute name converted to camel-case is used.
xmlns (str) – XML namespace to be used for this attribute.
help (rst formatted
str
) – Description of the attribute used in documentation and online help.position (int) – Position index to be used when the attribute should be output in non-default order.
- exception ValidationError[source]¶
Bases:
ValueError
Raised when an object is invalid according to its definition.
- exception ArgumentError[source]¶
Bases:
ValueError
Raised when invalid arguments would be used in an object’s initialization.
- class ObjectDefaultMaker(cls, args, kwargs)[source]¶
Bases:
DefaultMaker
Default value factory for
Object
derived classes.
- class Object(**kwargs)[source]¶
Bases:
object
Base class for Guts objects.
- Variables:
dummy_for – (class variable) If set, this indicates that the containing class is a dummy for another type. This can be used to hold native Python objects and non-Guts based objects as children of a Guts object.
dummy_for_description – (class variable) Overrides the name shown in the “dummy for …” documentation strings.
- classmethod D(*args, **kwargs)[source]¶
Get a default value factory for this class, configured with specified arguments.
- Returns:
Factory for default values.
- Return type:
ObjectDefaultMaker
object
- validate(regularize=False, depth=-1)[source]¶
Validate this object.
Raises
ValidationError
when the object is invalid.- Parameters:
depth (int) – Maximum depth to descend into child objects.
- regularize(depth=-1)[source]¶
Regularize this object.
Regularization tries to convert child objects of invalid types to the expected types.
Raises
ValidationError
when the object is invalid and cannot be regularized.- Parameters:
depth (int) – Maximum depth to descend into child objects.
- dump(stream=None, filename=None, header=False)[source]¶
Serialize to YAML.
If neither
stream
norfilename
is set, a string containing the serialized data is returned.
- dump_xml(stream=None, filename=None, header=False, ns_ignore=False)[source]¶
Serialize to XML.
If neither
stream
norfilename
is set, a string containing the serialized data is returned.
- to_dict(obj)[source]¶
Get dict of guts object attributes.
- Parameters:
obj – :py:class`Object` object
- class SObject(**kwargs)[source]¶
Bases:
Object
Base class for simple str-serializable Guts objects.
Derived classes must support (de)serialization as in
X(str(x))
.
- class Duration(...) float [source]¶
Bases:
Object
Placeholder for
float
time duration [s] with human-readable (de)serialization.Examples:
'1s'
-> 1 second'1m'
-> 1 minute'1h'
-> 1 hour'1d'
-> 1 day'1y'
-> about 1 year = 365*24*3600 seconds
- class Timestamp(...) pyrocko.util.get_time_float [source]¶
Bases:
Object
Placeholder for a UTC timestamp.
- class DateTimestamp(...) pyrocko.util.get_time_float [source]¶
Bases:
Object
Placeholder for a UTC timestamp which (de)serializes as a date string.
- class StringUnion(...) str [source]¶
Bases:
Object
Any
str
matching any of a set of constraints.- Variables:
members – List of constraints, e.g.
StringChoice
,StringPattern
, … (list
ofTBase
derived objects).
- clone(x, pool=None)[source]¶
Clone guts object tree.
Traverses guts object tree and recursively clones all guts attributes, falling back to
copy.deepcopy()
for non-guts objects. Objects deriving fromObject
are instantiated using their respective init function. Multiply referenced objects in the source tree are multiply referenced also in the destination tree.This function can be used to clone guts objects ignoring any contained run-time state, i.e. any of their attributes not defined as a guts property.
- exception YPathError[source]¶
Bases:
Exception
This exception is raised for invalid ypath specifications.
- iter_elements(obj, ypath)[source]¶
Generator yielding elements matching a given ypath specification.
- Parameters:
obj – guts
Object
instanceypath – Dot-separated object path (e.g. ‘root.child.child’). To access list objects use slice notatation (e.g. ‘root.child[:].child[1:3].child[1]’).
Raises
YPathError
on failure.
- get_elements(obj, ypath)[source]¶
Get all elements matching a given ypath specification.
- Parameters:
obj – guts
Object
instanceypath – Dot-separated object path (e.g. ‘root.child.child’). To access list objects use slice notatation (e.g. ‘root.child[:].child[1:3].child[1]’).
Raises
YPathError
on failure.
- set_elements(obj, ypath, value, validate=False, regularize=False)[source]¶
Set elements matching a given ypath specification.
- Parameters:
obj – guts
Object
instanceypath – Dot-separated object path (e.g. ‘root.child.child’). To access list objects use slice notatation (e.g. ‘root.child[:].child[1:3].child[1]’).
value – All matching elements will be set to value.
validate – Whether to validate affected subtrees.
regularize – Whether to regularize affected subtrees.
Raises
YPathError
on failure.
- dump(*args, **kwargs)[source]¶
Serialize to YAML.
If neither
stream
norfilename
is set, a string containing the serialized data is returned.