Re: [Pyparsing] Proposed notational shortcut for setResultsName
Brought to you by:
ptmcg
From: Paul M. <pa...@al...> - 2007-05-25 06:20:20
|
I don't want this to be part of the constructor. The point of setResultsName is to take a generic pattern (something like integer), and use it in several different places in the grammar, and the tokens returned from each place have a different name. So a better example than my personal info example might be a parser for a statistical summary report: realNum = Combine(Optional("-") + Word(nums)+ "." + Word(nums)) stats = "AVE =" + realNum["average"] + "STD. DEV. =" + realNum["stdDevn"] + \ "MIN =" + realNum["min"] + "MAX =" + realNum["max"] Please don't focus on the fact that [] is implemented using __getitem__, or that [] implies that we are "getting" anything - does this notation seem like a reasonable shortcut, in place of the following? stats = "AVE =" + realNum.setResultsName("average") + "STD. DEV. =" + realNum.setResultsName("stdDevn") + \ "MIN =" + realNum.setResultsName("min") + "MAX =" + realNum.setResultsName("max") Or, if you really want to think of this as a "getting" kind of operation, you could interpret this notation as indicating that realNum["average"] is getting for us a special form of realNum that names its returned tokens "average". For that matter, it also mirrors the dict-style format for retrieval of the data: results = stats.parseString( inputData ) print results["average"] -- Paul -----Original Message----- From: pyp...@li... [mailto:pyp...@li...] On Behalf Of Corrin Lakeland Sent: Thursday, May 24, 2007 6:45 PM To: pyp...@li... Subject: Re: [Pyparsing] Proposed notational shortcut for setResultsName A tiny modification of Ralph's suggestion makes the most sense to me userdata = Word(alphas, res = "name") + Word(nums+"-", res = "socsecno") -----Original Message----- From: pyp...@li... [mailto:pyp...@li...] On Behalf Of Ralph Corderoy Sent: Friday, May 25, 2007 11:39 AM To: Paul McGuire Cc: pyp...@li... Subject: Re: [Pyparsing] Proposed notational shortcut for setResultsName Hi Paul, > So how about adding a shortcut for setResultsName, using getitem? With > this short cut, this code: > > userdata = Word(alphas).setResultsName("name") + > Word(nums+"-").setResultsName("socsecno") > > could be written as: > > userdata = Word(alphas)["name"] + Word(nums+"-")["socsecno"] It just seems odd to use getitem to "set" something, i.e. the results name. What about having "_" as an attribute function instead so it isn't too obtrusive? userdata = Word(alphas)._("name") + Word(nums+"-")._("socsecno") Or userdata = Word(alphas, n = "name") + Word(nums+"-", n = "socsecno") Cheers, Ralph. ------------------------------------------------------------------------ - This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/ _______________________________________________ Pyparsing-users mailing list Pyp...@li... https://lists.sourceforge.net/lists/listinfo/pyparsing-users ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/ _______________________________________________ Pyparsing-users mailing list Pyp...@li... https://lists.sourceforge.net/lists/listinfo/pyparsing-users |