tags:

views:

75

answers:

1

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  A: 

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).

Alex Martelli
Thank you. That got me over my hurdle.Perhaps I should have asked if parsedatetime is the preferred method of parsing human formatted dates (e.g. "next tuesday"). It's what I found, but being new to the python community judging the reputation of libraries is hard for me still.http://code.google.com/p/parsedatetime/http://code-bear.com/code/parsedatetime/
golliher