This can come in handy if you're working with large Python objects and you want to be certain that you're either unit testing everything you retrieve or certain that all the data you draw from a database is actually used in a report.
For example, you might have a SELECT fieldX, fieldY, fieldZ FROM ...
SQL query, but in the report you only use fieldX, fieldY
in your CSV export.
class TrackingDict(dict):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._accessed_keys = set()
def __getitem__(self, key):
self._accessed_keys.add(key)
return super().__getitem__(key)
@property
def accessed_keys(self):
return self._accessed_keys
@property
def never_accessed_keys(self):
return set(self.keys()) - self._accessed_keys
Example use case:
user = {
"name": "John Doe",
"age": 30,
"email": "[email protected]",
}
user = TrackingDict(user)
assert user["name"] == "John Doe"
print("Accessed keys:", user.accessed_keys)
print("Never accessed keys:", user.never_accessed_keys)
This will print
Accessed keys: {'name'}
Never accessed keys: {'email', 'age'}
This can be useful if you have, for example, a pytest
test that checks all the values of a dict object and you want to be sure that you're testing everything. For example:
assert not user.never_accessed_keys, f"You never checked {user.never_accessed_keys}"