Publications about Pyparsing

If you have comments or questions about these articles, or errata to report, please post them to this page's Discussion tab.

October 5, 2007 - Pyparsing E-book Available from O'Reilly

O'Reilly Publishing has released a 65 page e-book, Getting Started With Pyparsing by Paul McGuire, on its catalog of Short Cuts for the modest sum of US$9.99. Some of the topics covered are:
- Basic structure of a Pyparsing program
- The Zen of Pyparsing (FREE sample chapter online)
- "Hello, World!" revisited, with more elaborate grammar and results processing than has been previously published
- Parser for S-Expressions
- Extracting complex table data from a web page
- Parsing search strings, and writing a search engine in under 100 lines of code
All examples are accompanied by complete source code listings. Throughout the book, helpful tips and notes are covered in separate sidebar discussions, finishing with a short list of additional helpful resources. Lastly, an index adds to the reference value of this book. Download a copy today!

January, 2015 - Mastering Python Design Patterns

Mastering Python Design Patterns_Frontcover.jpg
Sakis Kasampalis' tour through the most common software design patterns includes using Pyparsing as part of his description of the Intepreter pattern. Hor his application, he imagines a home automation DSL of the form: command -> device -> arguments
word = Word(alphanums)
command = Group(OneOrMore(word))
token = Suppress("->")
device = Group(OneOrMore(word))
argument = Group(OneOrMore(word))
event = command + token + device + Optional(token + argument)
increase -> boiler temperature -> 3 degrees
close -> garage
turn off -> heating
decrease -> fridge temperature -> 2 degrees

December, 2010 - Python 2.6 Text Processing: Beginners Guide

Py26_Text_Processing_BG.jpgJeff McNeil includes in his book a nice intro to using pyparsing to process a number of examples, including:
- Simple arithmetic expression
- Using parse actions for parse-time type conversion
- BIND DNS data extractor from named.conf
- Abridged pyparsing API reference

Jeff also covers NLTK, Mako templates, working with JSON, outputting PDF with Reportlab, indexing with Nucular... For a "beginners guide" book, it sure seems to cover a lot of ground!

November, 2009 - Programming in Python 3: A Complete Introduction to the Python Language , 2nd Ed.

summerfield_python3_2nd_ed.jpgMark Summerfield includes an extensive description of Pyparsing in the 2nd edition of his book on using Python 3. The book covers:
- Developing in Python using procedural, objectoriented, and functional programming paradigms
- Creating custom packages and modules
- Writing and reading binary, text, and XML files, including optional compression, random access, and text and XML parsing
- Leveraging advanced data types, collections, control structures, and functions
- Spreading program workloads across multiple processes and threads
- Programming SQL databases and key—value DBM files
- Debugging techniques–and using Test Driven Development to avoid bugs in the first place
- Utilizing Python’s regular expression mini-language and module
- Parsing techniques, including how to use the third-party PyParsing and PLY modules
- Building usable, efficient, GUI-based applications
- Advanced programming techniques, including generators, function and class decorators, context managers, descriptors, abstract base classes, metaclasses, coroutines, and more
(Be sure to get the 2nd edition - the 1st edition omits the chapter on parsing.)

May, 2009 - Linux Magazine ( article on developing DSL's with pyparsing

What is it with DSL's and pyparsing? Maybe they are just a natural fit. Through the magic of Google, I found this archive copy of an article in the Spanish language version of Linux Magazine. Mucho gusto!

May 1, 2009 - Python Magazine article on developing DSL's with pyparsing

pymag_2009_04.jpgThe April issue of Python Magazine includes a feature article, "Create Your Own Domain Specific Language in Python With Imputil and Pyparsing," on using pyparsing to define your own domain-specific language (DSL) mixed right in with your Python code. Here is an example of Python, augmented using the imputil hook to support an inline state machine definition, in file trafficLight.pystate:
# define state machine
statemachine TrafficLight:
    Red -> Green
    Green -> Yellow
    Yellow -> Red
# define some class level constants
Red.carsCanGo = False
Yellow.carsCanGo = True
Green.carsCanGo = True
Red.delay = wait(20)
Yellow.delay = wait(3)
Green.delay = wait(15)
And here is how that module would look in a script that imports it:
import statemachine
import trafficLight
tl = trafficLight.Red()
for i in range(6):
    print tl, "GO" if tl.carsCanGo else "STOP"
    tl = tl.next_state()
Use this technique to enhance Python with your own domain syntax!

Note: the code in this article uses imputil, which is deprecated in Python 2.6 and will be removed in Python 2.7 and Python 3.0.

August 29, 2008 - Python Magazine article on advanced Pyparsing methods

pymag_2008_08.jpgThe August issue of Python Magazine includes an article on using pyparsing to parse text, with dynamic results names. The article steps through a series of examples, including an interesting example to parse this table of data:
| samp |  min |  max |  ave | sdev |
|  A1  |    7 |   11 |    9 |    1 |
|  B1  |   43 |   52 |   47 |    3 |
|  C1  |    7 |   10 |    8 |    1 |
|  A2  |   82 |   85 |   84 |    1 |
|  B2  |   98 |  112 |  106 |    3 |
|  C2  |    1 |    4 |    3 |    1 |
Using results names, we then modify the table format, and the same parser works with no changes at all!. The article finishes with an example of parsing the JSON data format, using dynamic results names to construct parse results that look like a demarshaled JSON object. That is, from this JSON object description:
{ "reference" : 
  { "article" : 
    { "title" : "Writing a Simple 
            Interpreter/Compiler with Pyparsing",
      "author": "Paul McGuire",
      "magazine" : "Python Magazine",
      "issue" : "May, 2008"
      "pages" : 6      } } }
the parsed results allows you to write code like this:
entry = parser.parseString(jsontext)
print entry["reference"]["article"]["pages"]
print entry.reference.article.issue

May 29, 2008 - Python Magazine Features Pyparsing

pymag_2008_05.jpgThe May issue of Python Magazine includes the article "Writing a Simple Interpreter/Compiler with Pyparsing," in which I step through the development of a BrainF*ck interpreter using pyparsing. For those unfamiliar with BF, here is "Hello, World!" in that scatonymous (a word I made up myself for the article) language:
The plot twist comes at the end when I convert the interpreter to a compiler. I had a lot of fun writing the article, and according to Doug Hellman in his blog, he had fun editing it!

January, 2006 - Building Recursive Descent Parsers with Python

This article from O'Reilly OnLAMP provides an overview of pyparsing, with sample programs including a chemical formula parser and molecular weight calculator, and a simple HTML scraper to collect the list of NIST NTP servers.