SerializationContext

class asdf.extension.SerializationContext(version, extension_manager, url, blocks)[source]

Bases: object

Container for parameters of the current (de)serialization.

This class should not be instantiated directly and instead will be created by the AsdfFile object and provided to extension classes (like Converters) via method arguments.

Attributes Summary

extension_manager

Get the ExtensionManager for enabled extensions.

url

The URL (if any) of the file being read or written.

version

Get the ASDF Standard version.

Methods Summary

assign_blocks()

assign_object(obj)

find_available_block_index(data_callback[, key])

Find the index of an available ASDF block to write data.

generate_block_key()

Generate a BlockKey used for Converters that wish to use multiple blocks

get_array_compression(arr)

Get the compression type for the given array data.

get_array_compression_kwargs(arr)

get_array_storage(arr)

Get the block type for the given array data.

get_block_data_callback(index[, key])

Generate a callable that when called will read data from an ASDF block at the provided index.

set_array_compression(arr, compression, ...)

Set the compression to use for the given array data.

set_array_storage(arr, array_storage)

Set the block type to use for the given array data.

Attributes Documentation

extension_manager

Get the ExtensionManager for enabled extensions.

Returns:
asdf.extension.ExtensionManager
url

The URL (if any) of the file being read or written.

Used to compute relative locations of external files referenced by this ASDF file. The URL will not exist in some cases (e.g. when the file is written to an io.BytesIO).

Returns:
str or None
version

Get the ASDF Standard version.

Returns:
str

Methods Documentation

assign_blocks()[source]
assign_object(obj)[source]
find_available_block_index(data_callback, key=None)[source]

Find the index of an available ASDF block to write data.

This is typically used inside asdf.extension.Converter.to_yaml_tree.

Parameters:
data_callback: callable

Callable that when called will return data (ndarray) that will be written to a block.

keyBlockKey, optional

BlockKey generated using self.generate_block_key. Only needed for a Converter that uses multiple blocks.

Returns:
block_index: int

Index of the ASDF block where data returned from data_callback will be written.

generate_block_key()[source]

Generate a BlockKey used for Converters that wish to use multiple blocks

Returns:
keyBlockKey

A hashable object that will be associated with the serialized/deserialized object and can be used to access multiple blocks within a Converter

get_array_compression(arr)[source]

Get the compression type for the given array data.

Parameters:
arrnumpy.ndarray
Returns:
compressionstr or None
get_array_compression_kwargs(arr)[source]
get_array_storage(arr)[source]

Get the block type for the given array data.

Parameters:
arrnumpy.ndarray
get_block_data_callback(index, key=None)[source]

Generate a callable that when called will read data from an ASDF block at the provided index.

Parameters:
indexint

Index of ASDF block.

keyBlockKey, optional

BlockKey generated using self.generate_block_key. Only needed for a Converter that uses multiple blocks.

Returns:
callbackcallable

A callable that when called (with no arguments) returns the block data as a one dimensional array of uint8

set_array_compression(arr, compression, **compression_kwargs)[source]

Set the compression to use for the given array data.

Parameters:
arrnumpy.ndarray

The array to set. If multiple views of the array are in the tree, only the most recent compression setting will be used, since all views share a single block.

compressionstr or None

Must be one of:

  • '' or None: no compression

  • zlib: Use zlib compression

  • bzp2: Use bzip2 compression

  • lz4: Use lz4 compression

  • input: Use the same compression as in the file read. If there is no prior file, acts as None.

set_array_storage(arr, array_storage)[source]

Set the block type to use for the given array data.

Parameters:
arrnumpy.ndarray

The array to set. If multiple views of the array are in the tree, only the most recent block type setting will be used, since all views share a single block.

array_storagestr

Must be one of:

  • internal: The default. The array data will be stored in a binary block in the same ASDF file.

  • external: Store the data in a binary block in a separate ASDF file.

  • inline: Store the data as YAML inline in the tree.