Expose zone comments

Bug #1324972 reported by Geo
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
pytz
Confirmed
Wishlist
Unassigned

Bug Description

Enhancement: Access to comments in zone.tab would be useful (for me), eg.
>>> toronto = pytz.timezone('America/Toronto')
>>> toronto.comments
 'Eastern Time - Ontario - most locations'

Stuart Bishop (stub)
Changed in pytz:
status: New → Confirmed
importance: Undecided → Wishlist
Revision history for this message
Stuart Bishop (stub) wrote :

These comments are used to clarify timezone selection within a country, and only exist for countries currently using multiple timezones.

Does this need to be exposed as something like country_timezone_comments['ca']['America/Toronto'] ? The current data says not, but it would be good to clarify with the timezone Theory file or the IANA mailing list.

Revision history for this message
Daniel Reed (nmlorg) wrote :

Hi! Sorry for the necromancy, but what do you think about something like this? The existing interface to `pytz.country_timezones` would remain unchanged, but consumers could manually iterate over `pytz.zone_tab` to get access to .coordinates and .comment:

```
>>> import pytz
>>> import pprint
>>> pprint.pprint(pytz.country_timezones['us'])
['America/New_York',
 'America/Detroit',
 'America/Kentucky/Louisville',
 'America/Kentucky/Monticello',
 'America/Indiana/Indianapolis',
 'America/Indiana/Vincennes',
 'America/Indiana/Winamac',
 'America/Indiana/Marengo',
 'America/Indiana/Petersburg',
 'America/Indiana/Vevay',
 'America/Chicago',
 'America/Indiana/Tell_City',
 'America/Indiana/Knox',
 'America/Menominee',
 'America/North_Dakota/Center',
 'America/North_Dakota/New_Salem',
 'America/North_Dakota/Beulah',
 'America/Denver',
 'America/Boise',
 'America/Phoenix',
 'America/Los_Angeles',
 'America/Anchorage',
 'America/Juneau',
 'America/Sitka',
 'America/Metlakatla',
 'America/Yakutat',
 'America/Nome',
 'America/Adak',
 'Pacific/Honolulu']
>>> [row for row in pytz.zone_tab if row.zone == 'America/Los_Angeles']
[Zone(code='US', coordinates=(34.05222222222222, -117.75722222222223), zone='America/Los_Angeles', comment='Pacific')]
>>> [row for row in pytz.zone_tab if row.zone == 'America/Los_Angeles'][0].comment
'Pacific'
```

(If something along these lines would be acceptable, I'd be happy to rework it without the collections module and/or my quick 6709 parser.)

Revision history for this message
Daniel Reed (nmlorg) wrote :
Revision history for this message
Stuart Bishop (stub) wrote :

This all looks acceptable.

I'm not fussed if the new API is only available with modern python, so we can stick with namedtuple and collections, but might need to drop the 'with' syntax to keep the file parseable.

I'm not thrilled about the iterator passed to LazyList keeeping an open file handle, even if never used. Defining the iterator as a class with the magic iterator methods should make it lazier, not attempting to open the file until it is needed. I certainly don't want the new code to fail on import if zone.tab is missing.

Revision history for this message
Daniel Reed (nmlorg) wrote :

I ended up implementing this in:

https://github.com/nmlorg/metabot/blob/c63dfce6e1e9556ef1b22133b8bfd3de97ebc9c1/metabot/util/tzutil.py

(It looks like I just changed `pieces = line.split(None, 4)` to `pieces = line.rstrip().split(None, 3)`.)

An example use is:

https://github.com/nmlorg/metabot/blob/c63dfce6e1e9556ef1b22133b8bfd3de97ebc9c1/metabot/util/adminui.py#L290

And as early as Python 2.7.15, at least, generator functions don't execute *any* statements until the first attempt to dereference them:

```
>>> def F():
... print "I'm running!"
... with open('/usr/share/zoneinfo/zone.tab') as fobj:
... print "I've opened zone.tab!"
... for line in fobj:
... print 'I read %r!' % line
... yield line
... print "I'm done reading!"
... print "I'm done executing!"
...
>>> ZONE_TAB = F()
>>> for _ in ZONE_TAB: pass
...
I'm running!
I've opened zone.tab!
I read '# tzdb timezone descriptions (deprecated version)\n'!
I read '#\n'!
 [...]
I read 'ZW\t-1750+03103\tAfrica/Harare\n'!
I'm done reading!
I'm done executing!
```

Specifically:

```
>>> ZONE_TAB = F()
>>> ZONE_TAB.next()
I'm running!
I've opened zone.tab!
I read '# tzdb timezone descriptions (deprecated version)\n'!
'# tzdb timezone descriptions (deprecated version)\n'
>>> ZONE_TAB.next()
I read '#\n'!
'#\n'
>>>
```

This seems to be intended behavior; from https://www.python.org/dev/peps/pep-0255/:

"When a generator function is called, [...] no code in the body of the function is executed. Instead a generator-iterator object is returned [...]

Each time the .next() method of a generator-iterator is invoked, the code in the body of the generator-function is executed until a yield or return statement (see below) is encountered, or until the end of the body is reached.

If a yield statement is encountered, the state of the function is frozen, and the value of expression_list is returned to .next()'s caller."

I actually just popped in and need to run again, but if that still sounds alright I'd be happy to put together an updated patch without "with".

Revision history for this message
Stuart Bishop (stub) wrote :

ok, so the simpler generators are fine and we can go with that. If you can supply a patch, merge proposal here or pull request on github that would be great. Otherwise I'll look at it for the 2019.3 release.

Revision history for this message
Daniel Reed (nmlorg) wrote :

Unfortunately, __future__.with_statement wasn't added until Python 2.5, and yielding from within a try/finally block wasn't legal in 2.4, but I've put another implementation together as https://github.com/stub42/pytz/pull/29.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.