diff options
author | Stuart Bishop <stuart.bishop@canonical.com> | 2019-07-31 11:57:00 +0000 |
---|---|---|
committer | Stuart Bishop <stuart.bishop@canonical.com> | 2019-07-31 11:57:00 +0000 |
commit | 0537ddc6a7724f10b59c78c9ca8c7ac7b9bc11af (patch) | |
tree | 9b07fca15da3674b9549002637fe64424d1505ae | |
parent | 95c23a1f882a3d474362a906a388c5e85f59f23e (diff) | |
download | pytz-git-0537ddc6a7724f10b59c78c9ca8c7ac7b9bc11af.tar.gz |
Defer generating case-insensitive lookups
Addresses lp:1835784 performance issue
-rw-r--r-- | gen_tzinfo.py | 3 | ||||
-rw-r--r-- | src/pytz/__init__.py | 6 |
2 files changed, 8 insertions, 1 deletions
diff --git a/gen_tzinfo.py b/gen_tzinfo.py index d06fa0e..5c0066c 100644 --- a/gen_tzinfo.py +++ b/gen_tzinfo.py @@ -129,7 +129,8 @@ def add_allzones(filename): tz for tz in all_timezones if resource_exists(tz)) ''' print >> outf, 'all_timezones_set = LazySet(all_timezones)' - print >> outf, '_all_timezones_lower_to_standard = dict((tz.lower(), tz) for tz in all_timezones)' + # Per lp:1835784 we can't afford to do this at import time + # print >> outf, '_all_timezones_lower_to_standard = dict((tz.lower(), tz) for tz in all_timezones)' print >> outf, 'common_timezones = \\' pprint(cz, outf) diff --git a/src/pytz/__init__.py b/src/pytz/__init__.py index 5c05066..4d8f21d 100644 --- a/src/pytz/__init__.py +++ b/src/pytz/__init__.py @@ -188,8 +188,14 @@ def _unmunge_zone(zone): return zone.replace('_plus_', '+').replace('_minus_', '-') +_all_timezones_lower_to_standard = None + + def _case_insensitive_zone_lookup(zone): """case-insensitively matching timezone, else return zone unchanged""" + global _all_timezones_lower_to_standard + if _all_timezones_lower_to_standard is None: + _all_timezones_lower_to_standard = dict((tz.lower(), tz) for tz in all_timezones) # noqa return _all_timezones_lower_to_standard.get(zone.lower()) or zone # noqa |