Thread: Re: [Pyparsing] Proposed notational shortcut for setResultsName
Brought to you by:
ptmcg
From: Corrin L. <Cor...@da...> - 2007-05-24 23:44:52
|
A tiny modification of Ralph's suggestion makes the most sense to me =20 userdata =3D Word(alphas, res =3D "name") + Word(nums+"-", res =3D "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: >=20 > userdata =3D Word(alphas).setResultsName("name") + > Word(nums+"-").setResultsName("socsecno") >=20 > could be written as: >=20 > userdata =3D 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 =3D Word(alphas)._("name") + Word(nums+"-")._("socsecno") Or userdata =3D Word(alphas, n =3D "name") + Word(nums+"-", n =3D = "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 |
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 |
From: Ralph C. <ra...@in...> - 2007-05-25 11:12:13
|
Hi Paul, > I don't want this to be part of the constructor. OK, I see your point. > Please don't focus on the fact that [] is implemented using > __getitem__, or that [] implies that we are "getting" anything I was just trying to point out that overloading should try and stick to similar meanings. People used to reading Python have a brain that's hard wired to think [] is indexing for a read, just like they think + is for adding, focused on it or not. What did you think of the realNum._('min') idea? Only two more characters and what it does looks like how it reads, i.e. it's calling an attribute function. I can read the bracket notation, I just think it would muddy the Pythoness of PyParsing. Cheers, Ralph. |
From: Jean-Paul C. <ex...@di...> - 2007-05-25 13:41:58
|
On Fri, 25 May 2007 01:20:14 -0500, Paul McGuire <pa...@al...> wrote: > [snip] > >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"] > The advantage of setResultsName over __getitem__ is what it suggests to a reader solely through its name. Throwing that away makes result naming a more expensive feature, since it becomes exceedingly likely to confuse a reader who isn't already familiar with the API. Perhaps worse, it's much more difficult to search for the API documentation for __getitem__ than it is to search for the API documentation for setResultsName. The suggested `_' method is just as bad, as far as obviousness goes, but at least it wins out in that searching for its documentation is easier. Ultimately, setResultsName doesn't bother me at all. Typing out identifiers isn't anywhere near the biggest expenditure of my time. I'd much rather have a clearly named method than an overloaded operator. Jean-Paul |