[Tutor] Convert values in a list back and forth from ints and time

Sander Sweers sander.sweers at gmail.com
Tue Jan 6 19:30:31 CET 2009


On Tue, Jan 6, 2009 at 04:38, bob gailer <bgailer at gmail.com> wrote:
> I also suggest splitting convertValue into two functions, one that takes
> strings and one that takes numbers. A lot easier to read and maintain.
>
> FWIW you could dispense with reverse in convertValue by testing the type of
> value for int or str.

Thanks for feedback and suggestions. Below is the function split in 2.

def convertToString(value, dateformat):
    print value
    if type(value) == int:
        return str(value)
    else:
        try:
            return strftime(dateformat, value)
        except TypeError:
            return value

def convertFromString(value, dateformat):
    try:
        return int(float(value))
    except ValueError:
        try:
            return strptime(value, dateformat)
        except:
            return value

Greets
Sander


More information about the Tutor mailing list