diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2019-10-31 21:10:28 -0700 |
---|---|---|
committer | Paul McGuire <ptmcg@users.noreply.github.com> | 2019-10-31 23:10:28 -0500 |
commit | 53d1b4a6f48a53c4c4ec4ac7031362b691c0366d (patch) | |
tree | 088ad3cf3561b78a00af4fb2fd474f4a2b8ca70c /examples/datetimeParseActions.py | |
parent | 41752aa52cc97c710474bb2972cceab057b52ad4 (diff) | |
download | pyparsing-git-53d1b4a6f48a53c4c4ec4ac7031362b691c0366d.tar.gz |
Blacken the project (#141)
Diffstat (limited to 'examples/datetimeParseActions.py')
-rw-r--r-- | examples/datetimeParseActions.py | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/examples/datetimeParseActions.py b/examples/datetimeParseActions.py index e5ae2b9..f7c4fc9 100644 --- a/examples/datetimeParseActions.py +++ b/examples/datetimeParseActions.py @@ -12,32 +12,44 @@ from pyparsing import pyparsing_common as ppc # define an integer string, and a parse action to convert it
# to an integer at parse time
integer = pp.Word(pp.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]))
+# 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')
+date_expr = integer("year") + "/" + integer("month") + "/" + integer("day")
date_expr.ignore(pp.pythonStyleComment)
-def convertToDatetime(s,loc,tokens):
+
+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 = "'%s/%s/%s' is not a valid date, %s" % \
- (tokens.year, tokens.month, tokens.day, ve)
+ errmsg = "'%s/%s/%s' is not a valid date, %s" % (
+ tokens.year,
+ tokens.month,
+ tokens.day,
+ ve,
+ )
raise pp.ParseException(s, loc, errmsg)
+
+
date_expr.setParseAction(convertToDatetime)
-date_expr.runTests("""\
+date_expr.runTests(
+ """\
2000/1/1
# invalid month
@@ -48,14 +60,16 @@ date_expr.runTests("""\ # but 2000 was
2000/2/29
- """)
+ """
+)
# if dates conform to ISO8601, use definitions in pyparsing_common
date_expr = ppc.iso8601_date.setParseAction(ppc.convertToDate())
date_expr.ignore(pp.pythonStyleComment)
-date_expr.runTests("""\
+date_expr.runTests(
+ """\
2000-01-01
# invalid month
@@ -66,4 +80,5 @@ date_expr.runTests("""\ # but 2000 was
2000-02-29
- """)
+ """
+)
|