mx.DateTime.Parser.DateFromString('crap') 
Author Message
 mx.DateTime.Parser.DateFromString('crap')

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.

I've got to parse a file where some lines contain months in the form
"Jul 2003", lots of lines contain no date at all (DateFromString returns
the current date again), and the first line with something that's
neither empty nor a valid month should end the processing.

Working on Windows I haven't got strptime.

No better way than checking explicitly for the content of the last line?

Thanks for any help,
Koczian

--
Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D-86135 Augsburg

Tel.: (0821) 598-2400, Fax : (0821) 598-2410



Mon, 26 Dec 2005 20:51:13 GMT  
 mx.DateTime.Parser.DateFromString('crap')

Quote:

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

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

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

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

('euro', 'us', 'altus', 'iso', 'altiso', 'lit', 'altlit', 'eurlit')
Quote:
>>> 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



Mon, 26 Dec 2005 23:38:33 GMT  
 mx.DateTime.Parser.DateFromString('crap')
[snip]

Quote:
>Working on Windows I haven't got strptime.

But you can still get it...

  http://py.vaults.ca/apyllo.py?find=strptime

--
Magnus Lie Hetland                "In this house we obey the laws of
http://hetland.org                 thermodynamics!"    Homer Simpson



Tue, 27 Dec 2005 18:19:38 GMT  
 mx.DateTime.Parser.DateFromString('crap')
Quote:


> 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



Sat, 31 Dec 2005 02:21:45 GMT  
 mx.DateTime.Parser.DateFromString('crap')

Quote:

> 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



Sat, 31 Dec 2005 23:18:05 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Create pywintypes.Time from an mx.DateTime.DateTime

2. mx.DateTime, mx.ODBC.Windows, and MS SQL Server - Fractional truncation error

3. xmlrpclib and mx.DateTime

4. testing mx.DateTime fails on import (Py 2.1.1)

5. Fixed mx.DateTime or alternative?

6. Fixed mx.DateTime or alternative?

7. mx.DateTime troubles

8. Pure python alternative to mx.DateTime?

9. mx.DateTime, win32, python 2.1 troubles

10. Don's World / Crap posting

11. 'pstruct' structure parser and python

12. 'Recursive descent parser'

 

 
Powered by phpBB® Forum Software