Skip to content

Commit

Permalink
README: add example code
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed Oct 27, 2024
1 parent 46a6b0e commit d4b8d46
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ HashTable / HashTableNT have an API similar to a dict:
- ``items()``, ``len()``
- ``read()``, ``write()``, ``size()``

Example code
------------

::

# HashTableNT mapping 256bit key [bytes] --> Chunk value [namedtuple]
Chunk = namedtuple("Chunk", ["refcount", "size"])
# 256bit (32Byte) key, 2x 32bit (4Byte) values
ht = HashTableNT(key_size=32, value_format="<II", value_type=Chunk)

key = b"x" * 32 # the key is usually from a cryptographic hash fn
value = Chunk(refcount=1, size=42)
ht[key] = value
assert ht[key] == value

for key, value in ht.items():
assert isinstance(key, bytes)
assert isinstance(value, Chunk)

file = "dump.bin" # giving an fd of a file opened in binary mode also works
ht.write(file)
ht = HashTableNT.read(file)

Want a demo?
------------

Expand Down

0 comments on commit d4b8d46

Please sign in to comment.