[Baypiggies] custom date parser
Anna Ravenscroft
annaraven at gmail.com
Wed Sep 3 19:17:52 CEST 2008
The dateutil module has a great fuzzy parser that lets you do all this.
On Wed, Sep 3, 2008 at 10:12 AM, Aaron Maxwell <amax at redsymbol.net> wrote:
> Hi all,
>
> Below is a function that parses a date string in the form "YYYY-MM-DD"
> and returns a datetime.date object, or None if it's bad input and
> cannot be converted. However, it does a couple of special tricks.
> The data in certain cases is known to have a value for the day or
> month that is not in the valid range; e.g., it may be 2007-11-31
> (November traditionally only has 30 days), or 2002-14-23. In this
> situation, I want to keep the most signficant good field(s) and set
> the lessors to 1, then return the date object from that - so the
> results of the above would be date(2007, 11, 1) or date(2002, 1, 1)
> respectively.
>
> The function below does this. It uses a triply-nested try/except
> block, and I can't shake the feeling that there is a shorter and
> clearer implementation. Any thoughts?
>
> Of course, one approach would be to manually check that the month and
> day field before passing them to datetime.date. I would rather reuse
> the validation code in the date class, though, for obvious reasons.
>
> Thanks in advance,
> Aaron
>
> {{{
> import datetime
> def parse_datefield(raw_pubdate):
> '''
> Parse a datefield
> Takes in a date string in the format YYYY-MM-DD.
> Returns a datetime.date object.
> '''
> # ... imagine validation/error checking code here ...
> parts = map(int, raw_pubdate.split('-'))
> try:
> d = datetime.date(*parts)
> except ValueError:
> # day out of range?
> parts[-1] = 1
> try:
> d = datetime.date(*parts)
> except ValueError:
> # month out of range?
> parts[-2] = 1
> try:
> d = datetime.date(*parts)
> except ValueError:
> # give up
> d = None
> return d
> }}}
>
> --
> Aaron Maxwell
> http://redsymbol.net
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
--
cordially,
Anna
--
Walking through the water. Trying to get across.
Just like everybody else.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20080903/8362ceb0/attachment.htm>
More information about the Baypiggies
mailing list