Skip to content

omnipy.components.raw.serializers

Tar-file serializers for raw string and bytes datasets.

CLASS DESCRIPTION
RawBytesDatasetToTarFileSerializer

Serialize raw bytes datasets to and from gzipped tar archives.

RawStrDatasetToTarFileSerializer

Serialize raw string datasets to and from gzipped tar archives.

RawBytesDatasetToTarFileSerializer

Bases: TarFileSerializer[StrictBytesDataset]


              flowchart BT
              omnipy.components.raw.serializers.RawBytesDatasetToTarFileSerializer[RawBytesDatasetToTarFileSerializer]
              omnipy.data.serializer.TarFileSerializer[TarFileSerializer]
              omnipy.data.serializer.Serializer[Serializer]

                              omnipy.data.serializer.TarFileSerializer --> omnipy.components.raw.serializers.RawBytesDatasetToTarFileSerializer
                                omnipy.data.serializer.Serializer --> omnipy.data.serializer.TarFileSerializer
                



              click omnipy.components.raw.serializers.RawBytesDatasetToTarFileSerializer href "" "omnipy.components.raw.serializers.RawBytesDatasetToTarFileSerializer"
              click omnipy.data.serializer.TarFileSerializer href "" "omnipy.data.serializer.TarFileSerializer"
              click omnipy.data.serializer.Serializer href "" "omnipy.data.serializer.Serializer"
            

Serialize raw bytes datasets to and from gzipped tar archives.

METHOD DESCRIPTION
deserialize

Deserialize a gzipped tar archive back into a dataset.

get_dataset_cls_for_new

Return the dataset class created during deserialization.

get_output_file_suffix

Return the file suffix used for serialized dataset members.

is_dataset_directly_supported

Return whether a dataset stores raw bytes.

serialize

Serialize a dataset into a gzipped tar archive.

Source code in src/omnipy/components/raw/serializers.py
class RawBytesDatasetToTarFileSerializer(TarFileSerializer[StrictBytesDataset]):
    """Serialize raw bytes datasets to and from gzipped tar archives."""
    @classmethod
    def is_dataset_directly_supported(cls, dataset: IsDataset) -> bool:
        """Return whether a dataset stores raw bytes."""

        type_variants = all_dataset_type_variants(dataset)
        return len(type_variants) > 0 and type_variants[0] is bytes

    @classmethod
    def get_dataset_cls_for_new(cls) -> Type[IsDataset]:
        # %% Original docstring (managed by expand_docstr_macros.py) %%
        # {{SERIALIZER_GET_DATASET_CLS_FOR_NEW_SUMMARY}}
        """Return the dataset class created during deserialization."""

        return StrictBytesDataset

    @classmethod
    def get_output_file_suffix(cls) -> str:
        # %% Original docstring (managed by expand_docstr_macros.py) %%
        # {{SERIALIZER_GET_OUTPUT_FILE_SUFFIX_SUMMARY}}
        """Return the file suffix used for serialized dataset members."""

        return 'bytes'

    @classmethod
    def serialize(cls, dataset: StrictBytesDataset) -> bytes | memoryview:
        # %% Original docstring (managed by expand_docstr_macros.py) %%
        # {{SERIALIZE_GZIPPED_TAR_SUMMARY}}
        """Serialize a dataset into a gzipped tar archive."""
        def raw_encode_func(content: bytes) -> bytes:
            return content

        return cls.create_tarfile_from_dataset(dataset, data_encode_func=raw_encode_func)

    @classmethod
    def deserialize(cls, serialized: bytes, any_file_suffix=False) -> StrictBytesDataset:
        # %% Original docstring (managed by expand_docstr_macros.py) %%
        # {{DESERIALIZE_GZIPPED_TAR_SUMMARY}}
        """Deserialize a gzipped tar archive back into a dataset."""

        dataset = cast(StrictBytesDataset, Dataset[Model[bytes]]())

        def raw_decode_func(file_stream: IO[bytes]) -> bytes:
            return file_stream.read()

        def python_dictify_object(data_file: str, obj_val: Any) -> dict:
            return {data_file: obj_val}

        cls.create_dataset_from_tarfile(
            dataset,
            serialized,
            data_decode_func=raw_decode_func,
            dictify_object_func=python_dictify_object,
            import_method='from_data',
            any_file_suffix=any_file_suffix,
        )  # noqa

        return dataset

deserialize classmethod

deserialize(serialized: bytes, any_file_suffix=False) -> StrictBytesDataset

Deserialize a gzipped tar archive back into a dataset.

Source code in src/omnipy/components/raw/serializers.py
@classmethod
def deserialize(cls, serialized: bytes, any_file_suffix=False) -> StrictBytesDataset:
    # %% Original docstring (managed by expand_docstr_macros.py) %%
    # {{DESERIALIZE_GZIPPED_TAR_SUMMARY}}
    """Deserialize a gzipped tar archive back into a dataset."""

    dataset = cast(StrictBytesDataset, Dataset[Model[bytes]]())

    def raw_decode_func(file_stream: IO[bytes]) -> bytes:
        return file_stream.read()

    def python_dictify_object(data_file: str, obj_val: Any) -> dict:
        return {data_file: obj_val}

    cls.create_dataset_from_tarfile(
        dataset,
        serialized,
        data_decode_func=raw_decode_func,
        dictify_object_func=python_dictify_object,
        import_method='from_data',
        any_file_suffix=any_file_suffix,
    )  # noqa

    return dataset

get_dataset_cls_for_new classmethod

get_dataset_cls_for_new() -> Type[IsDataset]

Return the dataset class created during deserialization.

Source code in src/omnipy/components/raw/serializers.py
@classmethod
def get_dataset_cls_for_new(cls) -> Type[IsDataset]:
    # %% Original docstring (managed by expand_docstr_macros.py) %%
    # {{SERIALIZER_GET_DATASET_CLS_FOR_NEW_SUMMARY}}
    """Return the dataset class created during deserialization."""

    return StrictBytesDataset

get_output_file_suffix classmethod

get_output_file_suffix() -> str

Return the file suffix used for serialized dataset members.

Source code in src/omnipy/components/raw/serializers.py
@classmethod
def get_output_file_suffix(cls) -> str:
    # %% Original docstring (managed by expand_docstr_macros.py) %%
    # {{SERIALIZER_GET_OUTPUT_FILE_SUFFIX_SUMMARY}}
    """Return the file suffix used for serialized dataset members."""

    return 'bytes'

is_dataset_directly_supported classmethod

is_dataset_directly_supported(dataset: IsDataset) -> bool

Return whether a dataset stores raw bytes.

Source code in src/omnipy/components/raw/serializers.py
@classmethod
def is_dataset_directly_supported(cls, dataset: IsDataset) -> bool:
    """Return whether a dataset stores raw bytes."""

    type_variants = all_dataset_type_variants(dataset)
    return len(type_variants) > 0 and type_variants[0] is bytes

serialize classmethod

serialize(dataset: StrictBytesDataset) -> bytes | memoryview

Serialize a dataset into a gzipped tar archive.

Source code in src/omnipy/components/raw/serializers.py
@classmethod
def serialize(cls, dataset: StrictBytesDataset) -> bytes | memoryview:
    # %% Original docstring (managed by expand_docstr_macros.py) %%
    # {{SERIALIZE_GZIPPED_TAR_SUMMARY}}
    """Serialize a dataset into a gzipped tar archive."""
    def raw_encode_func(content: bytes) -> bytes:
        return content

    return cls.create_tarfile_from_dataset(dataset, data_encode_func=raw_encode_func)

RawStrDatasetToTarFileSerializer

Bases: TarFileSerializer[StrictStrDataset]


              flowchart BT
              omnipy.components.raw.serializers.RawStrDatasetToTarFileSerializer[RawStrDatasetToTarFileSerializer]
              omnipy.data.serializer.TarFileSerializer[TarFileSerializer]
              omnipy.data.serializer.Serializer[Serializer]

                              omnipy.data.serializer.TarFileSerializer --> omnipy.components.raw.serializers.RawStrDatasetToTarFileSerializer
                                omnipy.data.serializer.Serializer --> omnipy.data.serializer.TarFileSerializer
                



              click omnipy.components.raw.serializers.RawStrDatasetToTarFileSerializer href "" "omnipy.components.raw.serializers.RawStrDatasetToTarFileSerializer"
              click omnipy.data.serializer.TarFileSerializer href "" "omnipy.data.serializer.TarFileSerializer"
              click omnipy.data.serializer.Serializer href "" "omnipy.data.serializer.Serializer"
            

Serialize raw string datasets to and from gzipped tar archives.

METHOD DESCRIPTION
deserialize

Deserialize a gzipped tar archive back into a dataset.

get_dataset_cls_for_new

Return the dataset class created during deserialization.

get_output_file_suffix

Return the file suffix used for serialized dataset members.

is_dataset_directly_supported

Return whether a dataset stores raw strings.

serialize

Serialize a dataset into a gzipped tar archive.

Source code in src/omnipy/components/raw/serializers.py
class RawStrDatasetToTarFileSerializer(TarFileSerializer[StrictStrDataset]):
    """Serialize raw string datasets to and from gzipped tar archives."""
    @classmethod
    def is_dataset_directly_supported(cls, dataset: IsDataset) -> bool:
        """Return whether a dataset stores raw strings."""

        type_variants = all_dataset_type_variants(dataset)
        return len(type_variants) > 0 and type_variants[0] is str

    @classmethod
    def get_dataset_cls_for_new(cls) -> Type[IsDataset]:
        # %% Original docstring (managed by expand_docstr_macros.py) %%
        # {{SERIALIZER_GET_DATASET_CLS_FOR_NEW_SUMMARY}}
        """Return the dataset class created during deserialization."""

        return StrictStrDataset

    @classmethod
    def get_output_file_suffix(cls) -> str:
        # %% Original docstring (managed by expand_docstr_macros.py) %%
        # {{SERIALIZER_GET_OUTPUT_FILE_SUFFIX_SUMMARY}}
        """Return the file suffix used for serialized dataset members."""

        return 'txt'

    @classmethod
    def serialize(cls, dataset: StrictStrDataset) -> bytes | memoryview:
        # %% Original docstring (managed by expand_docstr_macros.py) %%
        # {{SERIALIZE_GZIPPED_TAR_SUMMARY}}
        """Serialize a dataset into a gzipped tar archive."""
        def raw_encode_func(content: str) -> bytes:
            return content.encode('utf8')

        return cls.create_tarfile_from_dataset(dataset, data_encode_func=raw_encode_func)

    @classmethod
    def deserialize(cls, serialized: bytes, any_file_suffix=False) -> StrictStrDataset:
        # %% Original docstring (managed by expand_docstr_macros.py) %%
        # {{DESERIALIZE_GZIPPED_TAR_SUMMARY}}
        """Deserialize a gzipped tar archive back into a dataset."""

        dataset = StrictStrDataset()

        def raw_decode_func(file_stream: IO[bytes]) -> str:
            return file_stream.read().decode('utf8')

        def python_dictify_object(data_file: str, obj_val: Any) -> dict:
            return {data_file: obj_val}

        cls.create_dataset_from_tarfile(
            dataset,
            serialized,
            data_decode_func=raw_decode_func,
            dictify_object_func=python_dictify_object,
            import_method='from_data',
            any_file_suffix=any_file_suffix,
        )  # noqa

        return dataset

deserialize classmethod

deserialize(serialized: bytes, any_file_suffix=False) -> StrictStrDataset

Deserialize a gzipped tar archive back into a dataset.

Source code in src/omnipy/components/raw/serializers.py
@classmethod
def deserialize(cls, serialized: bytes, any_file_suffix=False) -> StrictStrDataset:
    # %% Original docstring (managed by expand_docstr_macros.py) %%
    # {{DESERIALIZE_GZIPPED_TAR_SUMMARY}}
    """Deserialize a gzipped tar archive back into a dataset."""

    dataset = StrictStrDataset()

    def raw_decode_func(file_stream: IO[bytes]) -> str:
        return file_stream.read().decode('utf8')

    def python_dictify_object(data_file: str, obj_val: Any) -> dict:
        return {data_file: obj_val}

    cls.create_dataset_from_tarfile(
        dataset,
        serialized,
        data_decode_func=raw_decode_func,
        dictify_object_func=python_dictify_object,
        import_method='from_data',
        any_file_suffix=any_file_suffix,
    )  # noqa

    return dataset

get_dataset_cls_for_new classmethod

get_dataset_cls_for_new() -> Type[IsDataset]

Return the dataset class created during deserialization.

Source code in src/omnipy/components/raw/serializers.py
@classmethod
def get_dataset_cls_for_new(cls) -> Type[IsDataset]:
    # %% Original docstring (managed by expand_docstr_macros.py) %%
    # {{SERIALIZER_GET_DATASET_CLS_FOR_NEW_SUMMARY}}
    """Return the dataset class created during deserialization."""

    return StrictStrDataset

get_output_file_suffix classmethod

get_output_file_suffix() -> str

Return the file suffix used for serialized dataset members.

Source code in src/omnipy/components/raw/serializers.py
@classmethod
def get_output_file_suffix(cls) -> str:
    # %% Original docstring (managed by expand_docstr_macros.py) %%
    # {{SERIALIZER_GET_OUTPUT_FILE_SUFFIX_SUMMARY}}
    """Return the file suffix used for serialized dataset members."""

    return 'txt'

is_dataset_directly_supported classmethod

is_dataset_directly_supported(dataset: IsDataset) -> bool

Return whether a dataset stores raw strings.

Source code in src/omnipy/components/raw/serializers.py
@classmethod
def is_dataset_directly_supported(cls, dataset: IsDataset) -> bool:
    """Return whether a dataset stores raw strings."""

    type_variants = all_dataset_type_variants(dataset)
    return len(type_variants) > 0 and type_variants[0] is str

serialize classmethod

serialize(dataset: StrictStrDataset) -> bytes | memoryview

Serialize a dataset into a gzipped tar archive.

Source code in src/omnipy/components/raw/serializers.py
@classmethod
def serialize(cls, dataset: StrictStrDataset) -> bytes | memoryview:
    # %% Original docstring (managed by expand_docstr_macros.py) %%
    # {{SERIALIZE_GZIPPED_TAR_SUMMARY}}
    """Serialize a dataset into a gzipped tar archive."""
    def raw_encode_func(content: str) -> bytes:
        return content.encode('utf8')

    return cls.create_tarfile_from_dataset(dataset, data_encode_func=raw_encode_func)