struct_time type testing 2.1-2.2

Jeff Bauer jbauer at rubic.com
Thu Feb 28 14:13:02 EST 2002


Hi Robin.

Actually someone at ReportLabs put _SeqTypes in my
NormalDate class <0.3 wink>, not that I have any
objection.

My latest patch submitted to ReportLabs used something
like this:

    _TimeType = type(time.localtime(time.time()))

    ...

    if tn in (TupleType, ListType) or tn is _TimeType:
        <snip>

The _TimeType will work whether time is a tuple (<= Python2.1)
or a struct_time (Python.2.2) type.

Martin von Loewis also posted some good suggestions, although
his idea to use the code below won't work (he wouldn't have
known, without looking at ReportLab's source) because the
input type could also be a string, int, or an instance of
NormalDate.

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

I think the problem is type checking is inherently bad
practice which I violated for some user convenience
in NormalDate.

inelegant-ly y'rs,

Jeff Bauer
Rubicon Research

Robin writes:
> Jeff Bauer kindly tells me that Python 2.2 has a different return type
> for time.localtime(...). 
> 
> In his module he had a test on an argument type of the form
> 
>     _SeqTypes = (ListType,TupleType)
> 
> 
>     tn = type(arg)
>     if tn in _SeqTypes:
>       self.normalDate = int("%04d%02d%02d" % normalDate[:3])
> 
> and in 2.2 we seem to need 
> 
>     if tn in _SeqTypes or tn is time.struct_time:
> 
> is there a cleverer way than
> 
>     if tn in _SeqTypes or tn is getatr(time,'struct_time',None):
> 
> to get a cross version test?




More information about the Python-list mailing list