best way to parse a function-call-like string?
mh at pixar.com
mh at pixar.com
Thu Feb 26 16:42:06 EST 2009
Robert Kern <robert.kern at gmail.com> wrote:
> On 2009-02-26 15:29, mh at pixar.com wrote:
> Use the compiler module to generate an AST and walk it. That's the real
> way. For me, that would also be the quick way because I am familiar with
> the API, but it may take you a little bit of time to get used to it.
ah, that's just what I was hoping for...
in the meantime, here's my quick and dirty:
import csv
def fakeparse(s):
print "======="
argl=[]
ix=s.find('(')
if ix == -1:
func=s
argl=[]
else:
func=s[0:ix]
argstr=s[ix+1:]
argstr=argstr[:argstr.rfind(')')]
print argstr
for argl in csv.reader([argstr], quotechar="'"):
pass
print s
print func
print argl
return func,argl
print fakeparse("junkpkg.f1")
print fakeparse("junkpkg.f1()")
print fakeparse("junkpkg.f1('aaa')")
print fakeparse("junkpkg.f1('aaa','bbb')")
print fakeparse("junkpkg.f1('aaa','bbb','ccc')")
print fakeparse("junkpkg.f1('aaa','xx,yy')")
--
Mark Harrison
Pixar Animation Studios
More information about the Python-list
mailing list