Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: mx.DateTime.Parser.DateFromString('crap') (http://www.velocityreviews.com/forums/t319493-re-mx-datetime-parser-datefromstring-crap.html)

David Bolen 07-10-2003 03:38 PM

Re: mx.DateTime.Parser.DateFromString('crap')
 
Koczian <Sibylle.Koczian@Bibliothek.Uni-Augsburg.de> writes:

> This doesn't raise an exception but returns the current date. I know
> the comment in the notes for the Parser submodule: "The Parser
> submodule ... will try very hard to come up with a reasonable output
> given a valid input." But this is no valid input, so I'd prefer an
> exception or at least not a valid DateTime object.


Double check that you're using a recent version of the egenix base
package (probably 2.0.3 or later). The parser module functions such
as DateFromString were augmented to accept an optional list of parsers
to try, which allows you to override the default. The default does
include an "unknown" parser which will default to the current date as
in prior versions, but if you exclude that you'll get a ValueError
exception if none of the other parsers match.

For example:

>>> import mx.DateTime
>>> print mx.DateTime.Parser._date_formats

('euro', 'us', 'altus', 'iso', 'altiso', 'lit', 'altlit', 'eurlit', 'unknown')
>>> print mx.DateTime.Parser.DateFromString('crap')

2003-07-10 00:00:00.00
>>> myformats=mx.DateTime.Parser._date_formats[:-1]
>>> print myformats

('euro', 'us', 'altus', 'iso', 'altiso', 'lit', 'altlit', 'eurlit')
>>> print mx.DateTime.Parser.DateFromString('crap',formats=m yformats)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
<...snip...>
ValueError: unknown date format: "crap"


-- David

Sibylle Koczian 07-14-2003 06:21 PM

Re: mx.DateTime.Parser.DateFromString('crap')
 
David Bolen <db3l@fitlinxx.com> wrote in message news:<uznjmqtmu.fsf@fitlinxx.com>...
> Koczian <Sibylle.Koczian@Bibliothek.Uni-Augsburg.de> writes:
>
> Double check that you're using a recent version of the egenix base
> package (probably 2.0.3 or later). The parser module functions such
> as DateFromString were augmented to accept an optional list of parsers
> to try, which allows you to override the default. The default does
> include an "unknown" parser which will default to the current date as
> in prior versions, but if you exclude that you'll get a ValueError
> exception if none of the other parsers match.
>
> For example:
>
> >>> import mx.DateTime
> >>> print mx.DateTime.Parser._date_formats

> ('euro', 'us', 'altus', 'iso', 'altiso', 'lit', 'altlit', 'eurlit', 'unknown')
> >>> print mx.DateTime.Parser.DateFromString('crap')

> 2003-07-10 00:00:00.00
> >>> myformats=mx.DateTime.Parser._date_formats[:-1]
> >>> print myformats

> ('euro', 'us', 'altus', 'iso', 'altiso', 'lit', 'altlit', 'eurlit')
> >>> print mx.DateTime.Parser.DateFromString('crap',formats=m yformats)

> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> <...snip...>
> ValueError: unknown date format: "crap"
>

Exactly. Found this out after posting, of course, except that I
overlooked the "_date_formats" and typed in everything myself.

Thank you for not pointing out that it's all in the doc ... next time
I'll look more carefully.

Koczian

David Bolen 07-15-2003 03:18 PM

Re: mx.DateTime.Parser.DateFromString('crap')
 
Sibylle.Koczian@Bibliothek.Uni-Augsburg.de (Sibylle Koczian) writes:

> Exactly. Found this out after posting, of course, except that I
> overlooked the "_date_formats" and typed in everything myself.


I debated doing that myself. Using _date_formats is sort of cheating
since by being named with the leading underscore it's been marked as
an internal use only variable. But in the scheme of things I'd
probably accept the risk of needing to track it changing over time,
versus keeping a current list of parsers up to date.

-- David


All times are GMT. The time now is 02:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.