Thread: [Pyparsing] Check for tabs
Brought to you by:
ptmcg
From: Hanchel C. <han...@br...> - 2013-10-28 20:52:37
|
Hello! I'm new to pyparsing and relatively new to Python as well. I'm really astounded by the multitude of functionality that pyparsing has. My question is pretty basic: I have a string 'name\tdate\t\tlocation'. What would I do to ensure that between the 'name' and 'date' there is exactly one tab and between 'date' and 'location' there is exactly two tabs? Thanks, Hanchel |
From: Diez B. R. <de...@we...> - 2013-10-28 20:58:01
|
> > I'm new to pyparsing and relatively new to Python as well. I'm really astounded by the multitude of functionality that pyparsing has. > My question is pretty basic: > I have a string 'name\tdate\t\tlocation'. > What would I do to ensure that between the 'name' and 'date' there is exactly one tab and between 'date' and 'location' there is exactly two tabs? If that's all you have, I would forego pyparsing and use single string-functions like name, date, _, location = line.split("\t") plus of course some error-handling. Diez |
From: Hanchel C. <han...@br...> - 2013-10-28 22:41:23
|
Regardless of "all [I] have," I'd like to know if pyparser can check for a specific number of tabs between alphanumeric strings. If there are not two tabs between the 2nd and 3rd word, I'd like to error out. Is pyparsing truly overkill for this task? Thanks, Hanchel -----Original Message----- From: Diez B. Roggisch [mailto:de...@we...] Sent: Monday, October 28, 2013 1:58 PM To: Hanchel Cheng Cc: pyp...@li... Subject: Re: [Pyparsing] Check for tabs > > I'm new to pyparsing and relatively new to Python as well. I'm really astounded by the multitude of functionality that pyparsing has. > My question is pretty basic: > I have a string 'name\tdate\t\tlocation'. > What would I do to ensure that between the 'name' and 'date' there is exactly one tab and between 'date' and 'location' there is exactly two tabs? If that's all you have, I would forego pyparsing and use single string-functions like name, date, _, location = line.split("\t") plus of course some error-handling. Diez |
From: Mario R. O. <nim...@gm...> - 2013-10-29 01:05:58
|
I'm by no means an expert, but a couple of years ago I did finish what I like to call a medium difficulty parser. Yes you can definetly do that with pyparse and regex Dtb/Gby ======= Mario R. Osorio "... Begin with the end in mind ..." http://www.google.com/profiles/nimbiotics On Mon, Oct 28, 2013 at 6:41 PM, Hanchel Cheng <han...@br...>wrote: > Regardless of "all [I] have," I'd like to know if pyparser can check for a > specific number of tabs between alphanumeric strings. If there are not two > tabs between the 2nd and 3rd word, I'd like to error out. Is pyparsing > truly overkill for this task? > > Thanks, > Hanchel > > -----Original Message----- > From: Diez B. Roggisch [mailto:de...@we...] > Sent: Monday, October 28, 2013 1:58 PM > To: Hanchel Cheng > Cc: pyp...@li... > Subject: Re: [Pyparsing] Check for tabs > > > > > I'm new to pyparsing and relatively new to Python as well. I'm really > astounded by the multitude of functionality that pyparsing has. > > My question is pretty basic: > > I have a string 'name\tdate\t\tlocation'. > > What would I do to ensure that between the 'name' and 'date' there is > exactly one tab and between 'date' and 'location' there is exactly two tabs? > > If that's all you have, I would forego pyparsing and use single > string-functions like > > name, date, _, location = line.split("\t") > > plus of course some error-handling. > > Diez > > > > ------------------------------------------------------------------------------ > Android is increasing in popularity, but the open development platform that > developers love is also attractive to malware creators. Download this white > paper to learn more about secure code signing practices that can help keep > Android apps secure. > http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk > _______________________________________________ > Pyparsing-users mailing list > Pyp...@li... > https://lists.sourceforge.net/lists/listinfo/pyparsing-users > |
From: Diez B. R. <de...@we...> - 2013-10-29 07:09:15
|
On Oct 29, 2013, at 12:41 AM, Hanchel Cheng <han...@br...> wrote: > Regardless of "all [I] have," I'd like to know if pyparser can check for a specific number of tabs between alphanumeric strings. If there are not two tabs between the 2nd and 3rd word, I'd like to error out. Is pyparsing truly overkill for this task? I think by now you have your answer: yes, you can do it with pyparsing, but IMHO it's overkill, if that's all you ask it to do. Probably even using a regex would be more opaque then necessary. I've use pyparsing happily quite a few times to e.g. parse CSS or small DSLs. But for this kind of thing, I'd use string-methods. Diez |
From: d* <d*@y23.org> - 2013-10-29 15:07:58
|
Hi, I see nothing wrong with using pyparsing. There are actually many ways to solve the problem here. If you expect to be using pyparsing more in the future and expect to have multiple users maintaining the code I'd keep it simple and just stick to one paradigm and stay in the pyparsing realm. I'm no expert at pyparsing yet, and find myself still continuing to learn it as I go. I've become a fan of pair programming as well where two of us are learning pyparsing at the same time. There is nothing wrong with regex. I use it where its needed, but in general, I have found I don't mix it with the pyparsing code modules. And by doing so I've managed to get several different 'grammars' now robustly working. I don't see the overkill argument. When one walks into a factory and looks at the machinery for example. Is overkill that a machine the size of a truck cuts the same pattern 2000 times a day to an endless supply of steel? I doubt it. The work probably was done in the past by less refined machines and more than likely you are looking at the latest and newest production model for that type of work. I see it as, "Why do extra work, when you can have code that has been tested and end up doing less work?" Good luck on which ever method(s) you choose to implement. And don't forget to have some fun while you are doing it :) David > -------Original Message------- > From: Diez B. Roggisch <de...@we...> > To: Hanchel Cheng <han...@br...> > Cc: pyp...@li... <pyp...@li...> > Subject: Re: [Pyparsing] Check for tabs > Sent: Oct 29 '13 03:09 > > > On Oct 29, 2013, at 12:41 AM, Hanchel Cheng <han...@br...> wrote: > > > Regardless of "all [I] have," I'd like to know if pyparser can check for a specific number of tabs between alphanumeric strings. If there are not two tabs between the 2nd and 3rd word, I'd like to error out. Is pyparsing truly overkill for this task? > > I think by now you have your answer: yes, you can do it with pyparsing, but IMHO it's overkill, if that's all you ask it to do. Probably even using a regex would be more opaque then necessary. > > I've use pyparsing happily quite a few times to e.g. parse CSS or small DSLs. But for this kind of thing, I'd use string-methods. > > Diez > ------------------------------------------------------------------------------ > Android is increasing in popularity, but the open development platform that > developers love is also attractive to malware creators. Download this white > paper to learn more about secure code signing practices that can help keep > Android apps secure. > http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk > _______________________________________________ > Pyparsing-users mailing list > Pyp...@li... > https://lists.sourceforge.net/lists/listinfo/pyparsing-users > |
From: Mark L. <bre...@ya...> - 2013-10-29 15:18:51
|
IMHO sheer unadulterated rubbish. What you're saying is that when you want to crack a nut you skip the sledge hammer stage and use a steam roller. Here string methods are perfectly adequate for the task so use them Kindest regards. Mark Lawrence. On Tuesday, 29 October 2013, 15:08, d* <d*@y23.org> wrote: Hi, >I see nothing wrong with using pyparsing. There are actually many ways to solve the problem here. If you expect to be using pyparsing more in the future and expect to have multiple users maintaining the code I'd keep it simple and just stick to one paradigm and stay in the pyparsing realm. > >I'm no expert at pyparsing yet, and find myself still continuing to learn it as I go. I've become a fan of pair programming as well where two of us are learning pyparsing at the same time. > >There is nothing wrong with regex. I use it where its needed, but in general, I have found I don't mix it with the pyparsing code modules. > >And by doing so I've managed to get several different 'grammars' now robustly working. > >I don't see the overkill argument. When one walks into a factory and looks at the machinery for example. Is overkill that a machine the size of a truck cuts the same pattern 2000 times a day to an endless supply of steel? I doubt it. The work probably was done in the past by less refined machines and more than likely you are looking at the latest and newest production model for that type of work. I see it as, "Why do extra work, when you can have code that has been tested and end up doing less work?" > >Good luck on which ever method(s) you choose to implement. And don't forget to have some fun while you are doing it :) >David > > >> -------Original Message------- >> From: Diez B. Roggisch <de...@we...> >> To: Hanchel Cheng <han...@br...> >> Cc: pyp...@li... <pyp...@li...> >> Subject: Re: [Pyparsing] Check for tabs >> Sent: Oct 29 '13 03:09 >> >> >> On Oct 29, 2013, at 12:41 AM, Hanchel Cheng <han...@br...> wrote: >> >> > Regardless of "all [I] have," I'd like to know if pyparser can check for a specific number of tabs between alphanumeric strings. If there are not two tabs between the 2nd and 3rd word, I'd like to error out. Is pyparsing truly overkill for this task? >> >> I think by now you have your answer: yes, you can do it with pyparsing, but IMHO it's overkill, if that's all you ask it to do. Probably even using a regex would be more opaque then necessary. >> >> I've use pyparsing happily quite a few times to e.g. parse CSS or small DSLs. But for this kind of thing, I'd use string-methods. >> >> Diez >> ------------------------------------------------------------------------------ >> Android is increasing in popularity, but the open development platform that >> developers love is also attractive to malware creators. Download this white >> paper to learn more about secure code signing practices that can help keep >> Android apps secure. >> http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk >> _______________________________________________ >> Pyparsing-users mailing list >> Pyp...@li... >> https://lists.sourceforge.net/lists/listinfo/pyparsing-users >> > >------------------------------------------------------------------------------ >Android is increasing in popularity, but the open development platform that >developers love is also attractive to malware creators. Download this white >paper to learn more about secure code signing practices that can help keep >Android apps secure. >http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk >_______________________________________________ >Pyparsing-users mailing list >Pyp...@li... >https://lists.sourceforge.net/lists/listinfo/pyparsing-users > > > |
From: Mario R. O. <nim...@gm...> - 2013-10-29 15:46:42
|
Mark, Only two mistakes were made here: 1. In lue of more detailed background, you *assumed* Roggisch is trying to use pyparsing just to look for "\t"'s, and 2. The rest of us stupids here (me included of course) *assumed*Roggisch just asked a very specific question pertaining a more complex issue from which he might have wanted to keep us away either because he doesn't need help on anything else or because this is the first such issue he finds where he needs help. See the two mistakes? neither do I ... The one and only mistake here is that everyone assumed what each wanted to assume Now, in my particular case, I can give you a reason for that (which I do not pretend to use as an excuse): "I prefer to think everyone has a certain level of knowledge, though not necessarily of experience, otherwise you wouldn't be here but probably asking your teacher at school". That is of course, another assumption but, hey! I'm just one more stupid human being trusting other human beings!!. The only *sheer unadulterated rubbish* here have been your comments. Please be more of a boy!* * Dtb/Gby ======= Mario R. Osorio "... Begin with the end in mind ..." http://www.google.com/profiles/nimbiotics On Tue, Oct 29, 2013 at 11:18 AM, Mark Lawrence <bre...@ya...>wrote: > IMHO sheer unadulterated rubbish. What you're saying is that when you > want to crack a nut you skip the sledge hammer stage and use a steam > roller. Here string methods are perfectly adequate for the task so use them > > > Kindest regards. > > Mark Lawrence. > > > > On Tuesday, 29 October 2013, 15:08, d* <d*@y23.org> wrote: > > Hi, > >I see nothing wrong with using pyparsing. There are actually many ways > to solve the problem here. If you expect to be using pyparsing more in the > future and expect to have multiple users maintaining the code I'd keep it > simple and just stick to one paradigm and stay in the pyparsing realm. > > > >I'm no expert at pyparsing yet, and find myself still continuing to learn > it as I go. I've become a fan of pair programming as well where two of us > are learning pyparsing at the same time. > > > >There is nothing wrong with regex. I use it where its needed, but in > general, I have found I don't mix it with the pyparsing code modules. > > > >And by doing so I've managed to get several different 'grammars' now > robustly working. > > > >I don't see the overkill argument. When one walks into a factory and > looks at the machinery for example. Is overkill that a machine the size of > a truck cuts the same pattern 2000 times a day to an endless supply of > steel? I doubt it. The work probably was done in the past by less refined > machines and more than likely you are looking at the latest and newest > production model for that type of work. I see it as, "Why do extra work, > when you can have code that has been tested and end up doing less work?" > > > >Good luck on which ever method(s) you choose to implement. And don't > forget to have some fun while you are doing it :) > >David > > > > > >> -------Original Message------- > >> From: Diez B. Roggisch <de...@we...> > >> To: Hanchel Cheng <han...@br...> > >> Cc: pyp...@li... < > pyp...@li...> > >> Subject: Re: [Pyparsing] Check for tabs > >> Sent: Oct 29 '13 03:09 > >> > >> > >> On Oct 29, 2013, at 12:41 AM, Hanchel Cheng <han...@br...> > wrote: > >> > >> > Regardless of "all [I] have," I'd like to know if pyparser can check > for a specific number of tabs between alphanumeric strings. If there are > not two tabs between the 2nd and 3rd word, I'd like to error out. Is > pyparsing truly overkill for this task? > >> > >> I think by now you have your answer: yes, you can do it with > pyparsing, but IMHO it's overkill, if that's all you ask it to do. Probably > even using a regex would be more opaque then necessary. > >> > >> I've use pyparsing happily quite a few times to e.g. parse CSS or > small DSLs. But for this kind of thing, I'd use string-methods. > >> > >> Diez > >> > ------------------------------------------------------------------------------ > >> Android is increasing in popularity, but the open development platform > that > >> developers love is also attractive to malware creators. Download this > white > >> paper to learn more about secure code signing practices that can help > keep > >> Android apps secure. > >> > http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk > >> _______________________________________________ > >> Pyparsing-users mailing list > >> Pyp...@li... > >> https://lists.sourceforge.net/lists/listinfo/pyparsing-users > >> > > > > >------------------------------------------------------------------------------ > >Android is increasing in popularity, but the open development platform > that > >developers love is also attractive to malware creators. Download this > white > >paper to learn more about secure code signing practices that can help keep > >Android apps secure. > > > http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk > >_______________________________________________ > >Pyparsing-users mailing list > >Pyp...@li... > >https://lists.sourceforge.net/lists/listinfo/pyparsing-users > > > > > > > > ------------------------------------------------------------------------------ > Android is increasing in popularity, but the open development platform that > developers love is also attractive to malware creators. Download this white > paper to learn more about secure code signing practices that can help keep > Android apps secure. > http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk > _______________________________________________ > Pyparsing-users mailing list > Pyp...@li... > https://lists.sourceforge.net/lists/listinfo/pyparsing-users > |
From: Mark L. <bre...@ya...> - 2013-10-29 16:24:07
|
On 29/10/2013 15:46, Mario R. Osorio wrote: > Mark, > > Only two mistakes were made here: > > 1. In lue of more detailed background, you *assumed* Roggisch is trying > to use pyparsing just to look for "\t"'s, and > 2. The rest of us stupids here (me included of course) > *assumed*Roggisch just asked a very specific question pertaining a > more complex > issue from which he might have wanted to keep us away either because he > doesn't need help on anything else or because this is the first such issue > he finds where he needs help. > > See the two mistakes? neither do I ... The one and only mistake here is > that everyone assumed what each wanted to assume > > Now, in my particular case, I can give you a reason for that (which I do > not pretend to use as an excuse): "I prefer to think everyone has a certain > level of knowledge, though not necessarily of experience, otherwise you > wouldn't be here but probably asking your teacher at school". That is of > course, another assumption but, hey! I'm just one more stupid human being > trusting other human beings!!. > > > The only *sheer unadulterated rubbish* here have been your comments. Please > be more of a boy!* > * > What are you on about? The OP was Hanchel Cheng, not Diez B. Roggisch. The OP said "My question is pretty basic: I have a string 'name\tdate\t\tlocation'. What would I do to ensure that between the 'name' and 'date' there is exactly one tab and between 'date' and 'location' there is exactly two tabs?" What is there to assume about that? Diez replied "I would forego pyparsing and use single string-functions" and later "yes, you can do it with pyparsing, but IMHO it's overkill". D* then wrote his piece to which I replied, obviously very much agreeing with Diez. Have I missed something obvious? -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence |
From: <pt...@au...> - 2013-10-29 18:50:13
|
This might be a good time to invoke some level of moderation on the discussion. There is already confusion over who said what, who assumed what, and so on. The original post definitely asked a question in an area in which pyparsing is a sledgehammer to swat a fly. In the case of pyparsing, some people do start with flies as simple test cases, or choose to publish those in online support forums as a distilled-down example of some larger problem. (If the latter, I *really* appreciate it, when someone can post a short specific issue instead of a 60-line parser with the issue buried somewhere in it.) As it happens, this particular example points up a bit of a pitfall in pyparsing, that of looking for TABs in your parser - the call to parseWithTabs is easily overlooked. I don't think it is at all out of line to suggest non-pyparsing solutions to particular posts here, and I've found that investigating alternative solutions is always of value: sometimes I find a better way, and sometimes I get better insights into the original selected option. But this *is* a pyparsing mailing list, not a general Python support list, so if there is a bias to pyparsing-based solutions, I think that could be forgiven. -- Paul (I *would* prefer though that we avoid discourteous language - nobody is being paid to respond to these emails, I think most are posted in good faith and with good intentions, and usually with some amount of personal investment. I think there is room to disagree and still maintain civility and respect.) |
From: Paul M. <pt...@au...> - 2013-10-29 04:49:19
|
One of pyparsing's default behaviors is to skip over whitespace, so when whitespace is part of your parser, you sometimes have to take some extra steps. In the case of <TAB>s, you need to override pyparsing's default of replacing tabs in the input string with spaces. Otherwise, the input string first gets run through str.expandtabs before being parsed, and the tab expressions in your grammar will never match. Here's how: word = Word(alphas) tab = White('\t', exact=1) data = word + tab + word + tab + tab + word # this step is important data.parseWithTabs() s = 'name\tdate\t\tlocation' data.parseString(s).asList() ['name', '\t', 'date', '\t', '\t', 'location'] If you leave out this call, you'll get a ParseException because your input string will have been expanded to 'name date location', with no tabs. -- Paul -----Original Message----- From: Hanchel Cheng [mailto:han...@br...] Sent: Monday, October 28, 2013 3:52 PM To: pyp...@li... Subject: [Pyparsing] Check for tabs Hello! I'm new to pyparsing and relatively new to Python as well. I'm really astounded by the multitude of functionality that pyparsing has. My question is pretty basic: I have a string 'name\tdate\t\tlocation'. What would I do to ensure that between the 'name' and 'date' there is exactly one tab and between 'date' and 'location' there is exactly two tabs? Thanks, Hanchel ---------------------------------------------------------------------------- -- Android is increasing in popularity, but the open development platform that developers love is also attractive to malware creators. Download this white paper to learn more about secure code signing practices that can help keep Android apps secure. http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk _______________________________________________ Pyparsing-users mailing list Pyp...@li... https://lists.sourceforge.net/lists/listinfo/pyparsing-users |