Last active
April 15, 2022 07:49
-
-
Save shanehh/481c47cab0295b3e525ed09790d2f21d to your computer and use it in GitHub Desktop.
A dict, which remembers missing keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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