Skip to content

Enums

CompressionType

Bases: IntEnum

Compression algorithms for ZipFile

BZIP2 class-attribute instance-attribute

BZIP2 = 12

The numeric constant for the BZIP2 compression method. This requires the bz2 module.

DEFLATED class-attribute instance-attribute

DEFLATED = 8

The numeric constant for the usual ZIP compression method. This requires the zlib module.

LZMA class-attribute instance-attribute

LZMA = 14

The numeric constant for the LZMA compression method. This requires the lzma module.

STORED class-attribute instance-attribute

STORED = 0

The numeric constant for an uncompressed archive member.

get classmethod

get(key: str | int | None = None, default: Literal['stored', 'deflated', 'bzip2', 'lzma'] | str | int = 'stored') -> CompressionType

Get the CompressionType by its name or number. Return the default if the key is missing or invalid.

Parameters:

Name Type Description Default
key str | int

The key to retrieve.

None
default Literal['stored', 'deflated', 'bzip2', 'lzma'] | str | int

The default value to return if the key is missing or invalid.

'stored'

Returns:

Type Description
CompressionType

The CompressionType corresponding to the key.

Source code in src/archivefile/_enums.py
@classmethod
def get(
    cls,
    key: str | int | None = None,
    default: Literal["stored", "deflated", "bzip2", "lzma"] | str | int = "stored",
) -> CompressionType:
    """
    Get the `CompressionType` by its name or number.
    Return the default if the key is missing or invalid.

    Parameters
    ----------
    key : str | int, optional
        The key to retrieve.
    default : Literal["stored", "deflated", "bzip2", "lzma"] | str | int, optional
        The default value to return if the key is missing or invalid.

    Returns
    -------
    CompressionType
        The `CompressionType` corresponding to the key.
    """
    try:
        match key:
            case str():
                return cls[key.upper()]

            case int():
                return cls(key)

            case _:
                return cls[default.upper()] if isinstance(default, str) else cls(default)
    except (KeyError, ValueError):
        return cls[default.upper()] if isinstance(default, str) else cls(default)