Re: DateConverter validator can't parse str(datetime.date) objects

Ian Bicking <[email protected]> Sun, 07 Sep 2008 11:17:48 -0500
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
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

-------------------------------------------------------------------------
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=/