Re: DateConverter validator can't parse str(datetime.date) objects
"Matthew Wilson" <[email protected]> Sun, 7 Sep 2008 12:55:31 -0400
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
Thanks for the quick response. I think maybe the problem is that my web framework (turbogears 1.0) doesn't seem to use from_python to serialize the data back out. Instead, it seems to use a str. I'll start looking around there. On Sun, Sep 7, 2008 at 12:17 PM, Ian Bicking <[email protected]> wrote: > Matthew Wilson wrote: >> >> I'm using the formencodel.validators.DateConverter class to convert >> strings collected from an HTML web form into datetime.date objects. >> >> On the first page load, I draw the form blank. Then on the second >> page, I populate the form with the values passed in. >> >> Here's the problem: >> >> DateConverter().to_python(...) returns a datetime.date object. When I >> convert datetime.date to a string, it renders itself like this into a >> string that can't be parsed by the DateConverter. >> >> This strikes me as really weird. >> >> Here's some code that demonstrates my problem: >> >>>>> dc = formencode.validators.DateConverter() >>>>> dc.to_python('09-06-2008') # today >> >> datetime.date(2008, 9, 6) >>>>> >>>>> today = dc.to_python('09-06-2008') >>>>> today >> >> datetime.date(2008, 9, 6) >>>>> >>>>> str(today) >> >> '2008-09-06' >>>>> >>>>> dc.to_python(str(today)) > > Validators do round-trips through the two methods to_python and from_python. > So dc.to_python(dc.from_python(today)) should work. > > What values the DateConverter accepts relates to what values it expects to > produce. str() doesn't know about where the value is going to go, so it > can't be context-sensitive like this. > > -- > Ian Bicking : [email protected] : http://blog.ianbicking.org > -- Matthew Wilson [email protected] http://tplus1.com 216-470-6058 ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/