summaryrefslogtreecommitdiff
path: root/trunk/src/examples/datetimeParseActions.py
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-08-09 21:50:19 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-08-09 21:50:19 +0000
commit0b19bb71ba5a4afa84e673a8239935426fa0db23 (patch)
treee9abae9c616fdfdfebd9a8a0931d8f21824f30d2 /trunk/src/examples/datetimeParseActions.py
parentb2c3ade75384efe76b8774b607e17fe98fab92ef (diff)
downloadpyparsing_2.1.6.tar.gz
Remove incorrect tag directorypyparsing_2.1.6
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/tags/pyparsing_2.1.6@405 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
Diffstat (limited to 'trunk/src/examples/datetimeParseActions.py')
-rw-r--r--trunk/src/examples/datetimeParseActions.py43
1 files changed, 0 insertions, 43 deletions
diff --git a/trunk/src/examples/datetimeParseActions.py b/trunk/src/examples/datetimeParseActions.py
deleted file mode 100644
index 26d96a3..0000000
--- a/trunk/src/examples/datetimeParseActions.py
+++ /dev/null
@@ -1,43 +0,0 @@
-# parseActions.py
-#
-# A sample program a parser to match a date string of the form "YYYY/MM/DD",
-# and return it as a datetime, or raise an exception if not a valid date.
-#
-# Copyright 2012, Paul T. McGuire
-#
-from datetime import datetime
-from pyparsing import *
-
-# define an integer string, and a parse action to convert it
-# to an integer at parse time
-integer = Word(nums).setName("integer")
-def convertToInt(tokens):
- # no need to test for validity - we can't get here
- # unless tokens[0] contains all numeric digits
- return int(tokens[0])
-integer.setParseAction(convertToInt)
-# or can be written as one line as
-#integer = Word(nums).setParseAction(lambda t: int(t[0]))
-
-# define a pattern for a year/month/day date
-date_expr = integer('year') + '/' + integer('month') + '/' + integer('day')
-
-def convertToDatetime(s,loc,tokens):
- try:
- # note that the year, month, and day fields were already
- # converted to ints from strings by the parse action defined
- # on the integer expression above
- return datetime(tokens.year, tokens.month, tokens.day).date()
- except Exception as ve:
- errmsg = "'%d/%d/%d' is not a valid date, %s" % \
- (tokens.year, tokens.month, tokens.day, ve)
- raise ParseException(s, loc, errmsg)
-date_expr.setParseAction(convertToDatetime)
-
-
-date_expr.runTests("""\
- 2000/1/1
- 2000/13/1 # invalid month
- 1900/2/29 # 1900 was not a leap year
- 2000/2/29 # but 2000 was
- """) \ No newline at end of file