Making a tiny script language using python: I need a string processing lib

  • Thread starter Sullivan WxPyQtKinter
  • Start date
S

Sullivan WxPyQtKinter

I do not know if there is any lib specially designed to process the
strings in scipt language.
for example:
I hope to process the string"print a,b,c,d,e "in the form"command
argumentlist" and return:
{'command'='print',
'argumentlist'=['a','b','c','d','e']}
Are there any lib to implement this?


Ideally , the lib should have a function like below:

def extract(commandstring, splitformat)

eg:
extract("see#you@tonight","what#whom@when")
return: {"what":"see","whom":"you","when":"tonight"}

extract("1;2;3;4;5","list[;]")
return: {"list":['1','2','3','4','5']}

extract("print a,b,c,d,e","command arglist[,]")
return: {"command":"print","arglist":['a','b','c','d','e']}

extract("fruit:apple~orgrange~pear~grape#name:tom;sam;dim;ham"
,"class1:instance1[~]#class2:instance2[;]")
return: {"class1":"fruit",
instance1:['apple','orange','pear','grape'],
"class2":"name",
"instance2":['tom','sam','dim','ham']
}



####### # # # #
# # # # #
# # # # #
# ####### #
# # # # #
# # # # #
# # # # #
 
J

James Stroud

Sullivan said:
I do not know if there is any lib specially designed to process the
strings in scipt language.
for example:
I hope to process the string"print a,b,c,d,e "in the form"command
argumentlist" and return:
{'command'='print',
'argumentlist'=['a','b','c','d','e']}
Are there any lib to implement this?


Ideally , the lib should have a function like below:

def extract(commandstring, splitformat)

eg:
extract("see#you@tonight","what#whom@when")
return: {"what":"see","whom":"you","when":"tonight"}

extract("1;2;3;4;5","list[;]")
return: {"list":['1','2','3','4','5']}

extract("print a,b,c,d,e","command arglist[,]")
return: {"command":"print","arglist":['a','b','c','d','e']}

extract("fruit:apple~orgrange~pear~grape#name:tom;sam;dim;ham"
,"class1:instance1[~]#class2:instance2[;]")
return: {"class1":"fruit",
instance1:['apple','orange','pear','grape'],
"class2":"name",
"instance2":['tom','sam','dim','ham']
}

Your best bets are simpleparse (http://simpleparse.sourceforge.net/) or
ply (http://www.dabeaz.com/ply/ply.html). Both have a little learning
curve but are well worth the time investment if you will be doing things
like you describe.

James
 
P

Paul McGuire

Sullivan WxPyQtKinter said:
I do not know if there is any lib specially designed to process the
strings in scipt language.
for example:
I hope to process the string"print a,b,c,d,e "in the form"command
argumentlist" and return:
{'command'='print',
'argumentlist'=['a','b','c','d','e']}
Are there any lib to implement this?


Ideally , the lib should have a function like below:

def extract(commandstring, splitformat)

eg:
extract("see#you@tonight","what#whom@when")
return: {"what":"see","whom":"you","when":"tonight"}

extract("1;2;3;4;5","list[;]")
return: {"list":['1','2','3','4','5']}

extract("print a,b,c,d,e","command arglist[,]")
return: {"command":"print","arglist":['a','b','c','d','e']}

extract("fruit:apple~orgrange~pear~grape#name:tom;sam;dim;ham"
,"class1:instance1[~]#class2:instance2[;]")
return: {"class1":"fruit",
instance1:['apple','orange','pear','grape'],
"class2":"name",
"instance2":['tom','sam','dim','ham']
}



####### # # # #
# # # # #
# # # # #
# ####### #
# # # # #
# # # # #
# # # # #
I gave two presentations at PyCon on using pyparsing, one was for a simple
adventure game that implemented a command parser. You can find the
presentations and sample code at
http://www.geocities.com/ptmcg/python/index.html.

-- Paul
 
C

Christos Georgiou

I do not know if there is any lib specially designed to process the
strings in scipt language.
for example:
I hope to process the string"print a,b,c,d,e "in the form"command
argumentlist" and return:
{'command'='print',
'argumentlist'=['a','b','c','d','e']}

Have you checked the shlex module in the standard library? It might be
useful.
 
S

Sullivan WxPyQtKinter

I have gone over the shlex and cmd and so, but none of them are
satisfactory. However, I tried to program this this function on my own
and found it pretty easy.

Thank you so much for the suggestion, after all.
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top