Coverage for src/blob_dict/blob/__init__.py: 0%
30 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-27 22:31 -0700
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-27 22:31 -0700
1from __future__ import annotations
3from base64 import b64decode, b64encode
4from typing import override
7class BytesBlob:
8 def __init__(self, blob: bytes) -> None:
9 super().__init__()
11 self._blob_bytes: bytes = blob
13 def as_blob(self, blob_class: type[BytesBlob]) -> BytesBlob:
14 return blob_class(self._blob_bytes)
16 def as_bytes(self) -> bytes:
17 return self._blob_bytes
19 @staticmethod
20 def from_b64_str(blob: str) -> BytesBlob:
21 return BytesBlob(b64decode(blob))
23 def as_b64_str(self) -> str:
24 return b64encode(self._blob_bytes).decode("ascii")
26 def __repr__(self) -> str:
27 return f"{self.__class__.__name__}({self.as_bytes().__repr__()})"
30class StrBlob(BytesBlob):
31 def __init__(self, blob: bytes | str) -> None:
32 if isinstance(blob, str):
33 blob = blob.encode()
35 super().__init__(blob)
37 def as_str(self) -> str:
38 return self._blob_bytes.decode()
40 def __str__(self) -> str:
41 return self.as_str()
43 @override
44 def __repr__(self) -> str:
45 return f"{self.__class__.__name__}({self.as_str().__repr__()})"