Hide navigation sidebar
Hide table of contents sidebar
Skip to content
ASDF Projects Tutorials Community
Back to top
View this page
Toggle table of contents sidebar

# SerializationContext

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

  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_save_base`(arr) | Returns the `save_base` option for `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_save_base`(arr, save_base) | Set the `save_base` option for `arr`.
  `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()
  assign_object(obj)
  find_available_block_index(data_callback, key=None)

    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.

      key BlockKey, 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()

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

    Returns:
      key BlockKey

        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)

    Get the compression type for the given array data.

    Parameters:
      arr numpy.ndarray
    Returns:
      compression str or None
  get_array_compression_kwargs(arr)
  get_array_save_base(arr)

    Returns the `save_base` option for `arr`. When `arr` is written to a file, if `save_base` is `True` the base array for `arr` will be saved.

    Parameters:
      arr numpy.ndarray
    Returns:
      save_base bool
  get_array_storage(arr)

    Get the block type for the given array data.

    Parameters:
      arr numpy.ndarray
  get_block_data_callback(index, key=None)

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

    Parameters:
      index int

        Index of ASDF block.

      key BlockKey, optional

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

    Returns:
      callback callable

        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)

    Set the compression to use for the given array data.

    Parameters:
      arr numpy.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.

      compression str 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_save_base(arr, save_base)

    Set the `save_base` option for `arr`. When `arr` is written to a file, if `save_base` is `True` the base array for `arr` will be saved.

    Note that similar to other array options this setting is linked to the base array if `arr` is a view.

    Parameters:
      arr numpy.ndarray
      save_base bool or None

        if `None` the `default_array_save_base` value from asdf config will be used

  set_array_storage(arr, array_storage)

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

    Parameters:
      arr numpy.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_storage str

        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.