| 1 |
from Base.Metadata import export, constmethod |
| 2 |
from Base.BaseClass import BaseClass |
| 3 |
from typing import Any, Final, overload, Dict |
| 4 |
|
| 5 |
|
| 6 |
@export( |
| 7 |
Constructor=True, |
| 8 |
Reference=True, |
| 9 |
) |
| 10 |
class StringHasher(BaseClass): |
| 11 |
""" |
| 12 |
This is the StringHasher class |
| 13 |
|
| 14 |
Author: Zheng, Lei ([email protected]) |
| 15 |
Licence: LGPL |
| 16 |
""" |
| 17 |
|
| 18 |
@overload |
| 19 |
def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 20 |
... |
| 21 |
|
| 22 |
def getID(self, arg: Any, base64: bool = False) -> Any: |
| 23 |
""" |
| 24 |
getID(txt|id, base64=False) -> StringID |
| 25 |
|
| 26 |
If the input is text, return a StringID object that is unique within this hasher. This |
| 27 |
StringID object is reference counted. The hasher may only save hash ID's that are used. |
| 28 |
|
| 29 |
If the input is an integer, then the hasher will try to find the StringID object stored |
| 30 |
with the same integer value. |
| 31 |
|
| 32 |
base64: indicate if the input 'txt' is base64 encoded binary data |
| 33 |
""" |
| 34 |
... |
| 35 |
|
| 36 |
@overload |
| 37 |
def getID(self, txt: str, base64: bool = False) -> Any: ... |
| 38 |
|
| 39 |
@overload |
| 40 |
def getID(self, id: int, base64: bool = False) -> Any: ... |
| 41 |
|
| 42 |
@constmethod |
| 43 |
def isSame(self, other: "StringHasher") -> bool: |
| 44 |
""" |
| 45 |
Check if two hasher are the same |
| 46 |
""" |
| 47 |
... |
| 48 |
|
| 49 |
Count: Final[int] = 0 |
| 50 |
"""Return count of used hashes""" |
| 51 |
|
| 52 |
Size: Final[int] = 0 |
| 53 |
"""Return the size of the hashes""" |
| 54 |
|
| 55 |
SaveAll: bool = False |
| 56 |
"""Whether to save all string hashes regardless of its use count""" |
| 57 |
|
| 58 |
Threshold: int = 0 |
| 59 |
"""Data length exceed this threshold will be hashed before storing""" |
| 60 |
|
| 61 |
Table: Final[Dict[int, str]] = {} |
| 62 |
"""Return the entire string table as Int->String dictionary""" |