hup.base.binary module

Binary object functions.

as_bytes(data: Union[bytes, bytearray, memoryview, str], encoding: Optional[str] = None) → bytes

Convert bytes-like object or str to bytes.

Parameters:data – Binary data given as bytes-like object or string
compress(data: Union[bytes, bytearray, memoryview, str], level: int = -1) → bytes

Compress binary data using the gzip standard.

Parameters:
  • data – Binary data given as bytes-like object or string.
  • level – Compression level ranging from -1 to 9, where the default value of -1 is a compromise between speed and compression. For level 0 the given binary data is deflated without attempted compression, 1 denotes the fastest compression with minimum compression capability and 9 the slowest compression with maximum compression capability.
Returns:

Binary data as bytes.

decode(data: Union[bytes, bytearray, memoryview, str], encoding: Optional[str] = None, compressed: bool = False) → bytes

Decode bytes-like object or str.

Parameters:
  • data – Binary data given as bytes-like object or string
  • encoding – Encodings specified in RFC 3548. Allowed values are: base16, base32, base64 and base85 or None for no encoding. By default no encoding is used.
Returns:

Binary data as bytes.

decompress(data: Union[bytes, bytearray, memoryview, str]) → bytes

Decompress gzip compressed binary data.

Parameters:data – Binary data given as bytes-like object or string.
Returns:Binary data as bytes.
encode(data: Union[bytes, bytearray, memoryview, str], encoding: Optional[str] = None) → bytes

Encode bytes-like object or str.

Parameters:
  • data – Binary data given as bytes-like object or string
  • encoding – Encodings specified in RFC 3548. Allowed values are: base16, base32, base64 and base85 or None for no encoding. By default no encoding is used.
Returns:

Binary data as bytes.

pack(obj: object, encoding: Optional[str] = None, compression: Optional[int] = None) → bytes

Compress and encode arbitrary object to bytes.

Parameters:
  • obj – Any object, that can be pickled
  • encoding – Encodings specified in RFC 3548. Allowed values are: ‘base16’, ‘base32’, ‘base64’ and ‘base85’ or None for no encoding. By default no encoding is used.
  • compression – Determines the compression level for zlib.compress(). By default no zlib compression is used. For an integer ranging from -1 to 9, a zlib compression with the respective compression level is used. Thereby -1 is the default zlib compromise between speed and compression, 0 deflates the given binary data without attempted compression, 1 is the fastest compression with minimum compression capability and 9 is the slowest compression with maximum compression capability.
Returns:

Compressed and encoded byte representation of given object hierachy.

unpack(data: Union[bytes, bytearray, memoryview, str], encoding: Optional[str] = None, compressed: bool = False) → Any

Decompress and decode object from binary data.

Parameters:
  • data – Binary data given as bytes-like object or string.
  • encoding – Encodings specified in RFC 3548. Allowed values are: ‘base16’, ‘base32’, ‘base64’ and ‘base85’ or None for no encoding. By default no encoding is used.
  • compressed – Boolean value which determines, if the returned binary data shall be decompressed by using zlib.decompress().
Returns:

Arbitry object, that can be pickled.