Source code for opatio.catalog.entry

from dataclasses import dataclass
from typing import List
import struct

from opatio.index.floatvectorindex import FloatVectorIndex
from opatio.misc.opatentity import OPATEntity

[docs] @dataclass class CardCatalogEntry(OPATEntity): """ Structure to hold the card catalog information for an OPAT file. Attributes ---------- index : FloatVectorIndex Index values of the table. byteStart : int Byte start position of the table. byteEnd : int Byte end position of the table. sha256 : bytes SHA-256 hash of the table data. Methods ------- ascii() -> str Generate an ASCII representation of the card catalog entry. copy() -> CardCatalogEntry Create a copy of the card catalog entry. Examples -------- >>> from opatio.index.floatvectorindex import FloatVectorIndex >>> entry = CardCatalogEntry( ... index=FloatVectorIndex([1.0, 2.0, 3.0]), ... byteStart=0, ... byteEnd=128, ... sha256=b'\x00' * 32 ... ) >>> print(entry) CardCatalogEntry(index=FloatVectorIndex([1.0, 2.0, 3.0]), byteStart=0, byteEnd=128, sha256=b'\x00' * 32) """ index: FloatVectorIndex byteStart: int byteEnd: int sha256: bytes def __bytes__(self) -> bytes: """ Convert the card catalog to bytes. Returns ------- bytes The card catalog as bytes. Raises ------ AssertionError If the length of the generated bytes does not match the expected size. Examples -------- >>> entry = CardCatalogEntry( ... index=FloatVectorIndex([1.0, 2.0]), ... byteStart=0, ... byteEnd=64, ... sha256=b'\x00' * 32 ... ) >>> bytes(entry) b'...' """ catalogBytes = struct.pack( f"<{len(self.index)}dQQ32s", *self.index, self.byteStart, self.byteEnd, self.sha256 ) assert len(catalogBytes) == 16 + len(self.index)*8 + 32, ( f"Card catalog must be {16 + len(self.index)*8 + 32} bytes. " f"Due to an unknown error the card catalog has {len(catalogBytes)} bytes" ) return catalogBytes def __repr__(self) -> str: """ Get the string representation of the card catalog. Returns ------- str The string representation. Examples -------- >>> entry = CardCatalogEntry( ... index=FloatVectorIndex([1.0, 2.0]), ... byteStart=0, ... byteEnd=64, ... sha256=b'\x00' * 32 ... ) >>> repr(entry) 'CardCatalogEntry(index=FloatVectorIndex([1.0, 2.0]), byteStart=0, byteEnd=64, sha256=b\'\\x00\' * 32)' """ return f"CardCatalogEntry(index={self.index}, byteStart={self.byteStart}, byteEnd={self.byteEnd}, sha256={self.sha256})"
[docs] def ascii(self) -> str: """ Generate an ASCII representation of the card catalog. Returns ------- str The ASCII representation of the card catalog. Examples -------- >>> entry = CardCatalogEntry( ... index=FloatVectorIndex([1.0, 2.0]), ... byteStart=0, ... byteEnd=64, ... sha256=b'\x00' * 32 ... ) >>> print(entry.ascii()) 1.00 | 2.00 | 0 64 b'\\x00\\x00\\x00\\x00'... """ reprString = "" for indexID, index in enumerate(self.index): reprString += f"{index:<8.6f} | " reprString += f"{self.byteStart:<15} {self.byteEnd:<15} {self.sha256[:16]}...\n" return reprString
[docs] def copy(self) -> "CardCatalogEntry": """ Create a copy of the card catalog entry. Returns ------- CardCatalogEntry A copy of the card catalog entry. Examples -------- >>> entry = CardCatalogEntry( ... index=FloatVectorIndex([1.0, 2.0]), ... byteStart=0, ... byteEnd=64, ... sha256=b'\x00' * 32 ... ) >>> entry_copy = entry.copy() """ return CardCatalogEntry( index=self.index.copy(), byteStart=self.byteStart, byteEnd=self.byteEnd, sha256=self.sha256 )