Re: mx.DateTime.Parser.DateFromString('crap')

Discussion in 'Python' started by David Bolen, Jul 10, 2003.

  1. David Bolen

    David Bolen Guest

    Koczian <-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=myformats)

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


    -- David
    David Bolen, Jul 10, 2003
    #1
    1. Advertising

  2. David Bolen <> wrote in message news:<>...
    > Koczian <-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=myformats)

    > 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
    Sibylle Koczian, Jul 14, 2003
    #2
    1. Advertising

  3. David Bolen

    David Bolen Guest

    -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
    David Bolen, Jul 15, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    703
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    529
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    386
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    333
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    752
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page