struct_time type testing 2.1-2.2

Martin von Loewis loewis at informatik.hu-berlin.de
Thu Feb 28 13:07:12 EST 2002


Robin Becker <robin at jessikat.fsnet.co.uk> writes:

> is there a cleverer way than
> 
>     if tn in _SeqTypes or tn is getatr(time,'struct_time',None):
> 
> to get a cross version test?

I'm not sure it's very elegant, but what about

      if tn.__name__ in ['list','tuple','time.struct_time']

I actually prefer

try:
   from time import struct_time
except ImportError:
   _TimeTypes = (ListType, TupleType)
else:
   _TimeTypes = (ListType, TupleType, struct_time)


if tn in _TimeTypes

> Presumably we could extend _SeqTypes as that needs to be done only once,
> but ideally a simple check like
> 
>         arg.supports_index(3)
> 
> would be more informative than defining these rather arbitrary named
> type lists.

For that, I'd just write

  try:
    self.normalDate = int("%04d%02d%02d" % normalDate[:3])
  except TypeError:
    # else code

Regards,
Martin



More information about the Python-list mailing list