CustomType¶
- class asdf.CustomType[source]¶
Bases:
ExtensionType
Base class for all user-defined types.
Attributes Summary
bool
: Indicates whether dynamically generated subclasses can be serializedbool
: Indicates whether modules specified byrequires
are available.str
orlist
: The name of the type.str
: The organization responsible for the type.list
: Python packages that are required to instantiate the object.str
: The standard the type is defined in.set
: Versions that explicitly compatible with this extension class.list
: List of types that this extension class can convert to/from YAML.dict
: Mapping JSON Schema keywords to validation functions for jsonschema.str
,tuple
,asdf.versioning.AsdfVersion
, orasdf.versioning.AsdfSpec
:str
: The YAML tag to use for the type.Methods Summary
from_tree
(tree, ctx)Converts basic types representing YAML trees into custom types.
from_tree_tagged
(tree, ctx)Converts from tagged tree into custom type.
incompatible_version
(version)Indicates if given version is known to be incompatible with this type.
make_yaml_tag
(name[, versioned])Given the name of a type, returns a string representing its YAML tag.
names
()Returns the name(s) represented by this tag type as a list.
tag_base
()Returns the base of the YAML tag for types represented by this class.
to_tree
(node, ctx)Converts instances of custom types into YAML representations.
to_tree_tagged
(node, ctx)Converts instances of custom types into tagged objects.
Attributes Documentation
- handle_dynamic_subclasses = False¶
bool
: Indicates whether dynamically generated subclasses can be serializedFlag indicating whether this type is capable of serializing subclasses of any of the types listed in
types
that are generated dynamically.
- has_required_modules = True¶
bool
: Indicates whether modules specified byrequires
are available.NOTE: This value is automatically generated. Do not set it in subclasses as it will be overwritten.
- name = None¶
str
orlist
: The name of the type.
- organization = 'stsci.edu'¶
str
: The organization responsible for the type.
- requires = []¶
list
: Python packages that are required to instantiate the object.
- standard = 'asdf'¶
str
: The standard the type is defined in.
- supported_versions = []¶
set
: Versions that explicitly compatible with this extension class.If provided, indicates explicit compatibility with the given set of versions. Other versions of the same schema that are not included in this set will not be converted to custom types with this class.
- types = []¶
list
: List of types that this extension class can convert to/from YAML.Custom Python types that, when found in the tree, will be converted into basic types for YAML output. Can be either strings referring to the types or the types themselves.
- validators = {}¶
dict
: Mapping JSON Schema keywords to validation functions for jsonschema.Useful if the type defines extra types of validation that can be performed.
- version = AsdfVersion('1.0.0')¶
str
,tuple
,asdf.versioning.AsdfVersion
, orasdf.versioning.AsdfSpec
:The version of the type.
- yaml_tag = None¶
str
: The YAML tag to use for the type.If not provided, it will be automatically generated from name, organization, standard and version.
Methods Documentation
- classmethod from_tree(tree, ctx)¶
Converts basic types representing YAML trees into custom types.
This method should be overridden by custom extension classes in order to define how custom types are deserialized from the YAML representation back into their original types. Typically the method will return an instance of the original custom type. It is also permitted to return a generator, which yields a partially constructed result, then completes construction once the generator is drained. This is useful when constructing objects that contain reference cycles.
This method is called as part of the process of reading an ASDF file in order to construct an
asdf.AsdfFile
object. Whenever a YAML subtree is encountered that has a tag that corresponds to theyaml_tag
property of this class, this method will be used to deserialize that tree back into an instance of the original custom type.- Parameters:
- tree
object
representing YAML tree An instance of a basic Python type (possibly nested) that corresponds to a YAML subtree.
- ctx
asdf.AsdfFile
An instance of the
asdf.AsdfFile
object that is being constructed.
- tree
- Returns:
- An instance of the custom type represented by this extension class,
- or a generator that yields that instance.
- classmethod from_tree_tagged(tree, ctx)¶
Converts from tagged tree into custom type.
It is more common for extension classes to override
from_tree
instead of this method. This method should only be overridden if it is necessary to access the_tag
property of theTagged
object directly.- Parameters:
- tree
asdf.tagged.Tagged
object representing YAML tree - ctx
asdf.AsdfFile
An instance of the
asdf.AsdfFile
object that is being constructed.
- tree
- Returns:
- An instance of the custom type represented by this extension class.
- classmethod incompatible_version(version)¶
Indicates if given version is known to be incompatible with this type.
If this tag class explicitly identifies compatible versions then this checks whether a given version is compatible or not (see
supported_versions
). Otherwise, all versions are assumed to be compatible.Child classes can override this method to affect how version compatibility for this type is determined.
- Parameters:
- version
str
orAsdfVersion
The version to test for compatibility.
- version
- classmethod make_yaml_tag(name, versioned=True)¶
Given the name of a type, returns a string representing its YAML tag.
- Parameters:
- namestr
The name of the type. In most cases this will correspond to the
name
attribute of the tag type. However, it is passed as a parameter since some tag types represent multiple custom types.- versionedbool
If
True
, the tag will be versioned. Otherwise, a YAML tag without a version will be returned.
- Returns:
str
representing the YAML tag
- classmethod names()¶
Returns the name(s) represented by this tag type as a list.
While some tag types represent only a single custom type, others represent multiple types. In the latter case, the
name
attribute of the extension is actually a list, not simply a string. This method normalizes the value ofname
by returning a list in all cases.- Returns:
list
of names represented by this tag type
- classmethod tag_base()¶
Returns the base of the YAML tag for types represented by this class.
This method returns the portion of the tag that represents the standard and the organization of any type represented by this class.
- Returns:
str
representing the base of the YAML tag
- classmethod to_tree(node, ctx)¶
Converts instances of custom types into YAML representations.
This method should be overridden by custom extension classes in order to define how custom types are serialized into YAML. The method must return a single Python object corresponding to one of the basic YAML types (dict, list, str, or number). However, the types can be nested and combined in order to represent more complex custom types.
This method is called as part of the process of writing an
asdf.AsdfFile
object. Whenever a custom type (or a subclass of that type) that is listed in thetypes
attribute of this class is encountered, this method will be used to serialize that type.The name
to_tree
refers to the act of converting a custom type into part of a YAML object tree.- Parameters:
- node
object
Instance of a custom type to be serialized. Will be an instance (or an instance of a subclass) of one of the types listed in the
types
attribute of this class.- ctx
asdf.AsdfFile
An instance of the
asdf.AsdfFile
object that is being written out.
- node
- Returns:
- A basic YAML type (
dict
,list
,str
,int
,float
, or complex
) representing the properties of the custom type to be- serialized. These types can be nested in order to represent more
- complex custom types.
- A basic YAML type (
- classmethod to_tree_tagged(node, ctx)¶
Converts instances of custom types into tagged objects.
It is more common for custom tag types to override
to_tree
instead of this method. This method should be overridden if it is necessary to modify the YAML tag that will be used to tag this object.- Parameters:
- node
object
Instance of a custom type to be serialized. Will be an instance (or an instance of a subclass) of one of the types listed in the
types
attribute of this class.- ctx
asdf.AsdfFile
An instance of the
asdf.AsdfFile
object that is being written out.
- node
- Returns:
- An instance of
asdf.tagged.Tagged
.
- An instance of