Skip to content

Instantly share code, notes, and snippets.

@mtomwing
Created February 1, 2013 02:54
Show Gist options
  • Save mtomwing/4688806 to your computer and use it in GitHub Desktop.
Save mtomwing/4688806 to your computer and use it in GitHub Desktop.
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