Discussion:
[Pyparsing] Check for tabs
Hanchel Cheng
2013-10-28 20:52:12 UTC
Permalink
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
Diez B. Roggisch
2013-10-28 20:57:53 UTC
Permalink
Post by Hanchel Cheng
I'm new to pyparsing and relatively new to Python as well. I'm really astounded by the multitude of functionality that pyparsing has.
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
Hanchel Cheng
2013-10-28 22:41:05 UTC
Permalink
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:***@web.de]
Sent: Monday, October 28, 2013 1:58 PM
To: Hanchel Cheng
Cc: pyparsing-***@lists.sourceforge.net
Subject: Re: [Pyparsing] Check for tabs
Post by Hanchel Cheng
I'm new to pyparsing and relatively new to Python as well. I'm really astounded by the multitude of functionality that pyparsing has.
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
Mario R. Osorio
2013-10-29 01:05:29 UTC
Permalink
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
Post by Hanchel Cheng
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-----
Sent: Monday, October 28, 2013 1:58 PM
To: Hanchel Cheng
Subject: Re: [Pyparsing] Check for tabs
Post by Hanchel Cheng
I'm new to pyparsing and relatively new to Python as well. I'm really
astounded by the multitude of functionality that pyparsing has.
Post by Hanchel Cheng
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
https://lists.sourceforge.net/lists/listinfo/pyparsing-users
Diez B. Roggisch
2013-10-29 07:09:03 UTC
Permalink
Post by Hanchel Cheng
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
d*
2013-10-29 14:41:11 UTC
Permalink
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-------
Subject: Re: [Pyparsing] Check for tabs
Sent: Oct 29 '13 03:09
Post by Hanchel Cheng
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
https://lists.sourceforge.net/lists/listinfo/pyparsing-users
Mark Lawrence
2013-10-29 15:18:42 UTC
Permalink
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-------
  Subject: Re: [Pyparsing] Check for tabs
  Sent: Oct 29 '13 03:09
 
 
 
  > 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
  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
https://lists.sourceforge.net/lists/listinfo/pyparsing-users
Mario R. Osorio
2013-10-29 15:46:15 UTC
Permalink
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
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.
Hi,
Post by d*
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.
Post by d*
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.
Post by d*
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.
Post by d*
And by doing so I've managed to get several different 'grammars' now
robustly working.
Post by d*
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?"
Post by d*
Good luck on which ever method(s) you choose to implement. And don't
forget to have some fun while you are doing it :)
Post by d*
David
-------Original Message-------
Subject: Re: [Pyparsing] Check for tabs
Sent: Oct 29 '13 03:09
Post by Hanchel Cheng
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?
Post by d*
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.
Post by d*
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.
Post by d*
Diez
------------------------------------------------------------------------------
Post by d*
Android is increasing in popularity, but the open development platform
that
Post by d*
developers love is also attractive to malware creators. Download this
white
Post by d*
paper to learn more about secure code signing practices that can help
keep
Post by d*
Android apps secure.
http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk
Post by d*
_______________________________________________
Pyparsing-users mailing list
https://lists.sourceforge.net/lists/listinfo/pyparsing-users
------------------------------------------------------------------------------
Android is increasing in popularity, but the open development platform
that
Post by d*
developers love is also attractive to malware creators. Download this
white
Post by d*
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
Post by d*
_______________________________________________
Pyparsing-users mailing list
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
https://lists.sourceforge.net/lists/listinfo/pyparsing-users
Mark Lawrence
2013-10-29 16:24:15 UTC
Permalink
Post by Mario R. Osorio
Mark,
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
Paul McGuire
2013-10-29 04:33:38 UTC
Permalink
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:***@broadcom.com]
Sent: Monday, October 28, 2013 3:52 PM
To: pyparsing-***@lists.sourceforge.net
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
p***@austin.rr.com
2013-10-29 18:50:06 UTC
Permalink
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.)
Loading...