Skip to content

Instantly share code, notes, and snippets.

@shanehh
Last active April 15, 2022 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanehh/481c47cab0295b3e525ed09790d2f21d to your computer and use it in GitHub Desktop.
Save shanehh/481c47cab0295b3e525ed09790d2f21d to your computer and use it in GitHub Desktop.
A dict, which remembers missing keys
from collections import UserDict
class RememberMissingKeysDict(UserDict):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.missing_keys = set()
def get(self, k, default_v=None):
if super().get(k) is None:
self.missing_keys.add(k)
return super().get(k, default_v)
if __name__ == "__main__":
d = RememberMissingKeysDict({"a": 1, "b": 2})
d.get("c")
d.get("d")
assert d.missing_keys == {"c", "d"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment