From 3ff564520bfdd7ccb9857da05a5255269b31043c Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Mon, 21 Nov 2011 15:28:55 +1100 Subject: [PATCH] accept textual dates in more places; Result now holds match span positions. --- README.rst | 22 ++++++++++++++++++++++ parse.py | 8 ++++++++ 2 files changed, 30 insertions(+) diff --git a/README.rst b/README.rst index 36cb025..3a30d53 100644 --- a/README.rst +++ b/README.rst @@ -127,6 +127,8 @@ Some notes for the date and time types: will be set to 00:00:00. - except in ISO 8601 the day and month digits may be 0-padded - the separator for the ta and tg formats may be "-" or "/" +- named months (abbreviations or full names) may be used in the ta and tg + formats - as per RFC 2822 the e-mail format may omit the day (and comma), and the seconds but nothing else - hours greater than 12 will be happily accepted @@ -140,10 +142,30 @@ Some notes for the date and time types: .. _`Format String Syntax`: http://docs.python.org/library/string.html#format-string-syntax .. _`Format Specification Mini-Language`: http://docs.python.org/library/string.html#format-specification-mini-language + +Result Objects +-------------- + +The result of a ``parse()`` operation is either ``None`` (no match) or a +``Result`` instance. + +The ``Result`` instance has three attributes: + +fixed + A tuple of the fixed-position, anonymous fields extracted from the input. +named + A dictionary of the named fields extracted from the input. +spans + A dictionary mapping the names and fixed position indices matched to a + 2-tuple slice range of where the match occurred in the input. + + ---- **Version history (in brief)**: +- 1.1.5 accept textual dates in more places; Result now holds match span + positions. - 1.1.4 fixes to some int type conversion; implemented "=" alignment; added date/time parsing with a variety of formats handled. - 1.1.3 type conversion is automatic based on specified field types. Also added diff --git a/parse.py b/parse.py index 0070d21..631aa9f 100644 --- a/parse.py +++ b/parse.py @@ -349,6 +349,7 @@ def date_convert(string, match, time_only=False): return d + class Parser(object): def __init__(self, format): self._fixed_args = [] @@ -899,16 +900,23 @@ class TestParse(unittest.TestCase): datetime(1997, 07, 16, 19, 20, 30, 500000, tzinfo=FixedTzOffset(60, '+01:00'))) aest_d = datetime(2011, 11, 21, 10, 21, 36, tzinfo=aest) + dt = datetime(2011, 11, 21, 10, 21, 36) + d = datetime(2011, 11, 21) # te RFC2822 e-mail format datetime y('a {:te} b', 'a Mon, 21 Nov 2011 10:21:36 +1000 b', aest_d) y('a {:te} b', 'a 21 Nov 2011 10:21:36 +1000 b', aest_d) + y('a {:te} b', 'a 21 Nov 2011 b', d) # ta US (month/day) format datetime y('a {:ta} b', 'a 11/21/2011 10:21:36 AM +1000 b', aest_d) y('a {:ta} b', 'a 11-21-2011 10:21:36 AM +1000 b', aest_d) + y('a {:ta} b', 'a 11/21/2011 10:21:36 +1000 b', aest_d) + y('a {:ta} b', 'a 11/21/2011 10:21:36 b', dt) + y('a {:ta} b', 'a 11-21-2011 b', d) y('a {:ta} b', 'a Nov-21-2011 10:21:36 AM +1000 b', aest_d) y('a {:ta} b', 'a November-21-2011 10:21:36 AM +1000 b', aest_d) + y('a {:ta} b', 'a November-21-2011 b', d) # tg global (day/month) format datetime y('a {:tg} b', 'a 21/11/2011 10:21:36 AM +1000 b', aest_d)