RFC: Assignment as expression (pre-PEP)

Paul McGuire ptmcg at austin.rr.com
Sun Apr 8 04:55:04 CEST 2007


On Apr 5, 4:08 pm, "darkl... at timehorse.com" <TimeHo... at gmail.com>
wrote:
>  I love event-based parsers so I have no problem with that
> approach in general.

You might find a pyparsing version of this to be to your liking.  It
is possible in the parser events (or "parse actions" as pyparsing
calls them) to perform operations such as capitalization, string
replacement, or string-to-integer conversion.  To assign names to
specific fields, one defines results names using setResultsName.  A
grammar for your file name might look something like (I'm just
guessing from your code):

from pyparsing import *
def parseName2(name):
    """Parse filenames of the form:
 
programmeTitle.year.series.episodeNum.episodeName.capper.ext
    """
    capper = oneOf("JJ JeffreyJacobs").replaceWith("Jeffrey C.
Jacobs").setResultsName("capper")
    ext = Word(alphanums).setResultsName("ext")
    year = Word(nums,exact=4).setResultsName("year")
    capitalizeAll = lambda tokens : map(str.capitalize, tokens)
    title = Combine( OneOrMore( ~year +
Word(alphas) ).setParseAction( capitalizeAll ), joinString=" " ) \
        .setResultsName("programme")
    seriesAndEpnum = Combine( OneOrMore( ~Literal("-") +
Word(alphas) ).setParseAction( capitalizeAll ),
                        joinString=" ").setResultsName("series") + \
                     Word(nums).setResultsName("episodeNum")
    epname = Combine( OneOrMore( ~capper +
Word(alphas) ).setParseAction( capitalizeAll ), joinString=" " ) \
        .setResultsName("episodeName")
    fileName = title + "." + year + "." + seriesAndEpnum + "." +
epname + "." + capper + "." + ext
    parts = fileName.parseString(name)
    return parts.programme, parts.series, parts.episodeName,
parts.episodeNum, parst.year, parts.capper, parts.ext

In this example, the parse actions are capitalizeAll (easily
implemented with a simple lambda), and replaceWith (which is included
with pyparsing).

-- Paul





More information about the Python-list mailing list