Created
February 1, 2013 02:54
-
-
Save mtomwing/4688806 to your computer and use it in GitHub Desktop.
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
class RegexDict(dict): | |
def __get_regex_keys(self, pattern): | |
return (key for key in self.keys() if pattern.match(key)) | |
def __getitem__(self, key): | |
if hasattr(key, 'match'): | |
return [self[matched_key] for matched_key in self.__get_regex_keys(key)] | |
else: | |
return super(RegexDict, self).__getitem__(key) | |
def __setitem__(self, key, value): | |
if hasattr(key, 'match'): | |
for matched_key in self.__get_regex_keys(key): | |
self[matched_key] = value | |
else: | |
super(RegexDict, self).__setitem__(key, value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment