Created
December 5, 2014 01:12
-
-
Save edufelipe/665359688fa7beabd70e to your computer and use it in GitHub Desktop.
Generate a Regular Expression containing all common timezones from pytz
This file contains 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 OrderedDict | |
def make_tree(strings): | |
"""Take a list of strings and returns a dict tree with chars as keys. | |
>>> make_tree(['cat', 'car', 'dog']) | |
{'c': {'a': {'r': {}, 't': {}}}, 'd': {'o': {'g': {}}}} | |
""" | |
root = branch = OrderedDict() | |
for s in strings: | |
s = s.replace('/', r'\\/') | |
for i in range(len(s)): | |
if s[i] not in branch: | |
branch[s[i]] = OrderedDict() | |
branch = branch[s[i]] | |
branch = root | |
return root | |
def make_regex(tree): | |
"""Return a regular expression that matches the tree minimal tree. | |
>>> mate_regex({'c': {'a': {'r': {}, 't': {}}}, 'd': {'o': {'g': {}}}}) | |
(?:ca(?:t|r)|dog) | |
""" | |
inner = '|'.join(k + make_regex(v) for k, v in tree.items()) | |
if len(tree) > 1: | |
return '(?:%s)' % inner | |
return inner | |
if __name__ == '__main__': | |
import pytz | |
print make_regex(make_tree(pytz.common_timezones)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Currently the output is