0

I have a service in Python which processes some dictionaries. I wish to find and store a unique identifier for one of these dictionaries (when a condition is met).

Then a different service can process a list of dictionaries. This list contains the unique dictionary. If the unique identifier is found and verified, then the dictionary will be processed accordingly.

What is the best way to do achieve this. I thought maybe adding a new field to the dictionary something like

my_dict["unique_dict"] = True

but is there a better way?

KZiovas
  • 3,491
  • 3
  • 26
  • 47

1 Answers1

0

I found another way. One could to hash the dictionary like this for example:

import hashlib
import json

hasher = hashlib.sha1()
unhashed = {
    "msg": "blablabla",
    "measurements": [23, 567, 90],
    "calculation": 70.34,
}
encoded = json.dumps(unhashed, sort_keys=True).encode()
hasher.update(encoded)
hashed = hasher.hexdigest()
print(hashed)

and then store that hashed number in Redis, for example, or in a database or in cache or something.

Then in the second service the list of dictionaries will be hashed one by one. If one is found to have a hashed value that matches the stored value, it will then be processed accordingly.

KZiovas
  • 3,491
  • 3
  • 26
  • 47