3
votes

I'm trying to learn python after spending the last 15 or so years working only in Perl and only occasionally.

I can't understand how to handle the two different kinds of results from the parse method of Calendar.parse() from parsedatetime

Given this script:

#!/usr/bin/python

import parsedatetime.parsedatetime as pdt
import parsedatetime.parsedatetime_consts as pdc
import sys
import os

# create an instance of Constants class so we can override some of the defaults

c = pdc.Constants()

# create an instance of the Calendar class and pass in our Constants # object instead of letting it create a default

p = pdt.Calendar(c)

while True:
 reply = raw_input('Enter text:')
 if reply == 'stop': 
  break
 else:
  result = p.parse(reply)
  print result
  print

And this sample run:

Enter text:tomorrow
(time.struct_time(tm_year=2009, tm_mon=11, tm_mday=28, tm_hour=9, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=332, tm_isdst=-1), 1)

Enter text:11/28
((2009, 11, 28, 14, 42, 55, 4, 331, 0), 1)

I can't figure out how to get the output such that I can consisently use result like so:

print result[0].tm_mon, result[0].tm_mday

That won't work in the case where the input is "11/28" because the output is just a tuple and not a struct_time.

Probably a simple thing.. but not for this newbie. From my perspective the output of Calendar.parse() is unpredictable and hard to use. Any help appreciated. Tia.

2

2 Answers

16
votes

I know this is an old question but I ran into this yesterday and the answer here is incomplete (it will fail in the case that parse() returns a datetime).

From the parsedatetime docs:

parse() returns a tuple ( result, type ) where type specifies one of:

   0 = not parsed at all
   1 = parsed as a date (of type struct_time)
   2 = parsed as a time (of type struct_time)
   3 = parsed as a datetime (of type datetime.datetime)

Which is a little weird and maybe not the clearest way to do it, but it works and is pretty useful.

Here's a little chunk of code that will convert whatever it returns to a proper python datetime:

import parsedatetime.parsedatetime as pdt

def datetimeFromString( s ):

    c = pdt.Calendar()
    result, what = c.parse( s )

    dt = None

    # what was returned (see http://code-bear.com/code/parsedatetime/docs/)
    # 0 = failed to parse
    # 1 = date (with current time, as a struct_time)
    # 2 = time (with current date, as a struct_time)
    # 3 = datetime
    if what in (1,2):
        # result is struct_time
        dt = datetime.datetime( *result[:6] )
    elif what == 3:
        # result is a datetime
        dt = result

    if dt is None:
        # Failed to parse
        raise ValueError, ("Don't understand date '"+s+"'")

    return dt
2
votes

Use x = time.struct_time(result[0]) and you'll get a struct_time (so that you can check x.tm_mon and x.tm_mday) no matter whether that result[0] is a struct_time itself, or just a 9-tuple (I've never heard of parsedatetime so I don't know why it's inconsistent in its return type, but with this simple approach you can neutralize that inconsistency).