shlex parsing

K

Karim

Hello All,

I would like to parse this TCL command line with shlex:

'-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'

And I want to get the splitted list:

['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG']

Then I will gather in tuple 2 by 2 the arguments.

I tried to the shlec properties attributes 'quotes', 'whitespace', etc...

But I make 'choux blanc'.

If somebody has complex experiences with this module I am in.

Cheers
Karim
 
W

Web Dreamer

Karim a écrit ce mercredi 27 juillet 2011 21:30 dans said:
Hello All,

I would like to parse this TCL command line with shlex:

'-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'

And I want to get the splitted list:

['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG']

Then I will gather in tuple 2 by 2 the arguments.


Do this:
s = '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'

Now if you don't enclose [get_rule A1 B2] in cotes, you will pull your hair off!
So:
s = s.replace('[','"[')
s = s.replace(']',']"')
Now:
'-option1 "[get_rule A1 B2]" -option2 $VAR -option3 TAG'

Lets continue:['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG']

Now to get your tupple of two by two arguments:
argtuple = tuple([(option, value) for option,value in zip(optionlist[0::2],optionlist[1::2])])
argtuple
(('-option1', '[get_rule A1 B2]'), ('-option2', '$VAR'), ('-option3', 'TAG'))


whole code:
import shlex
s = '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'
s = s.replace('[','"[')
s = s.replace(']',']"')
optionlist = shlex.split(s)
argtuple = tuple([(option, value) for option,value in zip(optionlist[0::2],optionlist[1::2]))
 
K

Karim

Hello You have you feet on earth Web Dreamer!

Very clever!
Beautiful hack!

Many Thanks

Karim

Karim a écrit ce mercredi 27 juillet 2011 21:30 dans said:
Hello All,

I would like to parse this TCL command line with shlex:

'-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'

And I want to get the splitted list:

['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG']

Then I will gather in tuple 2 by 2 the arguments.

Do this:
s = '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'
Now if you don't enclose [get_rule A1 B2] in cotes, you will pull your hair off!
So:
s = s.replace('[','"[')
s = s.replace(']',']"') Now:
s
'-option1 "[get_rule A1 B2]" -option2 $VAR -option3 TAG'

Lets continue:['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG']

Now to get your tupple of two by two arguments:
argtuple = tuple([(option, value) for option,value in zip(optionlist[0::2],optionlist[1::2])])
argtuple
(('-option1', '[get_rule A1 B2]'), ('-option2', '$VAR'), ('-option3', 'TAG'))


whole code:
import shlex
s = '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'
s = s.replace('[','"[')
s = s.replace(']',']"')
optionlist = shlex.split(s)
argtuple = tuple([(option, value) for option,value in zip(optionlist[0::2],optionlist[1::2]))
 
K

Karim

Just a little modification:
tuple([(option, value) for option,value in
zip(optionlist[0::2],optionlist[1::2])]) ==
tuple(zip(optionlist[0::2],optionlist[1::2]))
True

Indeed:

tuple(zip(optionlist[0::2],optionlist[1::2]))

shorter than:

tuple([(option, value) for option,value in
zip(optionlist[0::2],optionlist[1::2])])


Karim

PS: I am from Grenoble, which place in France are from?

I would like to parse this TCL command line with shlex:

'-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'
s = s.replace('[','"[')
s = s.replace(']',']"')
Note that this approach won't work if you have nested brackets or braces.
That would require a real parser.
 
W

Web Dreamer

Nobody a écrit ce jeudi 28 juillet 2011 18:37 dans
I would like to parse this TCL command line with shlex:

'-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'
s = s.replace('[','"[')
s = s.replace(']',']"')

Note that this approach won't work if you have nested brackets or braces.
That would require a real parser.

True,
I tried with the shlex class, but adding '[]' to a shlexobject.quotes
doesn't work.
Indeed, shlex expects an opening and closing quote to be the same, and [ is
different from ] so shlex therefore expects an opening [ to be closed with [
and not ]

My solution indeed only works as long as there are not any nested brackets.
 
W

Web Dreamer

Karim a écrit ce jeudi 28 juillet 2011 19:51 dans
Just a little modification:
tuple([(option, value) for option,value in
zip(optionlist[0::2],optionlist[1::2])]) ==
tuple(zip(optionlist[0::2],optionlist[1::2]))
True

Indeed:

tuple(zip(optionlist[0::2],optionlist[1::2]))

shorter than:

tuple([(option, value) for option,value in
zip(optionlist[0::2],optionlist[1::2])])

Nice Thanks :)
 
W

Web Dreamer

Karim a écrit ce jeudi 28 juillet 2011 19:51 dans
Just a little modification:
tuple([(option, value) for option,value in
zip(optionlist[0::2],optionlist[1::2])]) ==
tuple(zip(optionlist[0::2],optionlist[1::2]))
True

Indeed:

tuple(zip(optionlist[0::2],optionlist[1::2]))

shorter than:

tuple([(option, value) for option,value in
zip(optionlist[0::2],optionlist[1::2])])

Nice Thanks :)
PS: I am from Grenoble, which place in France are from?

Paris.

Cheers :)
 
K

Karim

Nobody a écrit ce jeudi 28 juillet 2011 18:37 dans
I would like to parse this TCL command line with shlex:

'-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'
s = s.replace('[','"[')
s = s.replace(']',']"')
Note that this approach won't work if you have nested brackets or braces.
That would require a real parser.
True,
I tried with the shlex class, but adding '[]' to a shlexobject.quotes
doesn't work.
Indeed, shlex expects an opening and closing quote to be the same, and [ is
different from ] so shlex therefore expects an opening [ to be closed with [
and not ]

My solution indeed only works as long as there are not any nested brackets.
Yeah I saw that shlex object behavior is slighty different from
shlex.split()
fonction. the char minus by default is taken as a individual word and in
split
it is not. get_token() method return '-' alone and you have to set
object attributes
wordchars += '-' to have the same behavior as shlex.split().

Cheers
Karim
 
W

Web Dreamer

Karim a écrit ce vendredi 29 juillet 2011 11:37 dans
Yeah I saw that shlex object behavior is slighty different from
shlex.split()
fonction. the char minus by default is taken as a individual word and in
split
it is not. get_token() method return '-' alone and you have to set
object attributes
wordchars += '-' to have the same behavior as shlex.split().

To have the "almost" same behaviour as shlex.split() you only need to set
the shlex whitespace_split property to True.
but it doesn't solve the bracket problem.

Using shlex object:
import shlex
s = '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'
s = s.replace('[','"[')
s = s.replace(']',']"')
optshlex = shlex.shlex(s)
optshlex.whitespace_split = True
options = [option for option in optshlex]
options
['-option1', '"[get_rule A1 B2]"', '-option2', '$VAR', '-option3', 'TAG']

However there still is a diference with shlex.split():['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG']

It's the fact that '[get_rule A1 B2]' get's added quotes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top