diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2012-10-02 04:55:56 +0000 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2012-10-02 04:55:56 +0000 |
commit | a7f9dda0668bfce4fba51df1bf2976b4a93a8bd5 (patch) | |
tree | 57ea8bcf2e66532a36c833a7bc57cff9d5d0e4dd /src/examples/builtin_parse_action_demo.py | |
parent | f5d2b716ffb57b65660a7ee0bbf04332dfb29620 (diff) | |
download | pyparsing-git-a7f9dda0668bfce4fba51df1bf2976b4a93a8bd5.tar.gz |
Add example files to SVN
Diffstat (limited to 'src/examples/builtin_parse_action_demo.py')
-rw-r--r-- | src/examples/builtin_parse_action_demo.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/examples/builtin_parse_action_demo.py b/src/examples/builtin_parse_action_demo.py new file mode 100644 index 0000000..ef42640 --- /dev/null +++ b/src/examples/builtin_parse_action_demo.py @@ -0,0 +1,29 @@ +#
+# builtin_parse_action_demo.py
+# Copyright, 2012 - Paul McGuire
+#
+# Simple example of using builtin functions as parse actions.
+#
+
+from pyparsing import *
+
+integer = Word(nums).setParseAction(lambda t : int(t[0]))
+
+# make an expression that will match a list of ints (which
+# will be converted to actual ints by the parse action attached
+# to integer)
+nums = OneOrMore(integer)
+
+
+test = "2 54 34 2 211 66 43 2 0"
+print test
+
+# try each of these builtins as parse actions
+for fn in (sum, max, min, len, sorted, reversed, list, tuple, set, any, all):
+ fn_name = fn.__name__
+ if fn is reversed:
+ # reversed returns an iterator, we really want to show the list of items
+ fn = lambda x : list(reversed(x))
+
+ # show how each builtin works as a free-standing parse action
+ print fn_name, nums.setParseAction(fn).parseString(test)
|