Consider: import shlex shlex.split('$(which sh)')
['$(which', 'sh)']
Is this behavior correct? It seems that I should
either get one token, or the list
['$','(','which','sh',')'],
but certainly breaking it the way it does is
erroneous.
Can anyone explain why the string is being split
that way? 4 1441
bill wrote: Consider: import shlex shlex.split('$(which sh)') ['$(which', 'sh)']
Is this behavior correct? It seems that I should either get one token, or the list ['$','(','which','sh',')'], but certainly breaking it the way it does is erroneous.
Can anyone explain why the string is being split that way?
This may help. http://www.python.org/dev/doc/devel/...ule-shlex.html
This works on Python 2.4: import shlex sh = shlex.shlex('$(which sh)') sh.get_token()
'$' sh.get_token()
'(' sh.get_token()
'which' sh.get_token()
'sh' sh.get_token()
')' sh.get_token()
etc...
Python 2.2 and maybe lower: import shlex import StringIO s = StringIO.StringIO('$(which sh)') sh = shlex.shlex(s) sh.get_token()
'$' sh.get_token()
'(' sh.get_token()
'which' sh.get_token()
'sh' sh.get_token()
')' sh.get_token()
etc...
Hth,
M.E.Farmer
Its gets worse: from shlex import StringIO from shlex import shlex t = shlex(StringIO("2>&1")) while True:
.... b = t.read_token()
.... if not b: break
.... print b
....
2
&
1 <----------- where's the '>' !? import shlex print shlex.split("2>&1")
['2>&1']
It strikes me that split should be behaving exactly the same way as
read_token, but that may be a misunderstanding on my part of what split
is doing.
However, it is totally bizarre that read_token discards the '>' symbol
in the string! I don't know much about lexical analysis, but it
strikes me that discarding characters is a bad thing.
bill wrote: Its gets worse: from shlex import StringIO from shlex import shlex t = shlex(StringIO("2>&1")) while True: ... b = t.read_token() ... if not b: break ... print b ... 2 & 1 <----------- where's the '>' !? import shlex print shlex.split("2>&1") ['2>&1']
It strikes me that split should be behaving exactly the same way as read_token, but that may be a misunderstanding on my part of what
split is doing.
However, it is totally bizarre that read_token discards the '>'
symbol in the string! I don't know much about lexical analysis, but it strikes me that discarding characters is a bad thing. From the docs:
split(s[, comments])
Split the string s using shell-like syntax. If comments is False
(the default), the parsing of comments in the given string will be
disabled (setting the commenters member of the shlex instance to the
empty string). This function operates in POSIX mode. New in version
2.3.
Maybe looking at string methods split might help. "$(which sh)".split()
['($(which', 'sh)']
From the docs:
read_token()
Read a raw token. Ignore the pushback stack, and do not interpret
source requests. (This is not ordinarily a useful entry point, and is
documented here only for the sake of completeness.)
# Just like in my first post from StringIO import StringIO from shlex import shlex t = shlex(StringIO("2>&1")) t.get_token()
'2' t.get_token()
'>' t.get_token()
'&' t.get_token()
'1' t.get_token()
''
# Your way t = shlex(StringIO("2>&1")) t.read_token()
'2' t.read_token()
'&' t.read_token()
'1' t.read_token()
''
Hth,
M.E.Farmer
In article <11**********************@g49g2000cwa.googlegroups .com>,
"bill" <bi**********@gmail.com> wrote: Consider: import shlex shlex.split('$(which sh)') ['$(which', 'sh)']
Is this behavior correct? It seems that I should either get one token, or the list ['$','(','which','sh',')'], but certainly breaking it the way it does is erroneous.
Can anyone explain why the string is being split that way?
Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
Type "help", "copyright", "credits" or "license" for more information. import shlex print shlex.__doc__
A lexical analyzer class for simple shell-like syntaxes.
This has a little potential to mislead. Bourne shell
syntax is naturally "shell-like", but it is not "simple" -
as grammars go, it's a notorious mess. In theory, someone
could certainly write Python code to accurately parse Bourne
shell statements, but that doesn't appear to have been the
intention here. The "Parsing Rules" section of the documentation
describes what you can expect, and right off hand I don't see
how the result you got was erroneous.
Donn Cave, do**@u.washington.edu This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Severus Snape |
last post by:
If you open a window with resizable=no, Opera doesn't comply.
|
by: Gerhard Esterhuizen |
last post by:
Hi,
I am observing unexpected behaviour, in the form of a corrupted class
member access, from a simple C++ program that accesses an attribute
declared in a virtual base class via a chain of...
|
by: Netocrat |
last post by:
The code at the bottom illustrates the nature of a situation discussed on
comp.lang.asm.x86. Basically an unsigned integer is being used as a
negative index in a loop, and it works as though the...
|
by: Mark Findlay |
last post by:
Hello Experts!
I am attempting to use the OleDbCommand.ExecuteScaler() function within my
ASP.NET C# web page to perform a simple validation, but receive the
following error:
"Incorrect...
|
by: yuliy |
last post by:
hello,
is this code has undefined behavour?
int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n";
Why?
|
by: simnav |
last post by:
In the following code something strange happens ! If I keep pressed any
of ALT+Arrow, keys, they are extracted two times from buffer then getch
seems to stop; if I release and press again ALT+arrow...
|
by: TraceyAnnison |
last post by:
I wonder if you can help me - I'm new to this, and working in a project that has already been configured to work with Axis.
We have a Java project built with the Spring framework, in order to...
|
by: PaulCarmo |
last post by:
Hello,
I'm validating a form containing various types of data. One of the fields to be validated is an amount which must be between 0.01 and 999999.99.
The code for this field validation in the...
|
by: Sheth Raxit |
last post by:
Machine 1 :
bash-3.00$ uname -a
SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R
bash-3.00$ gcc -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |