tcl list to python list?

Cameron Laird claird at lairds.us
Sun Sep 17 17:53:07 CEST 2006


In article <CE4Pg.4266$vD2.1873 at tornado.texas.rr.com>,
Paul McGuire <ptmcg at austin.rr._bogus_.com> wrote:
><jerry.levan at gmail.com> wrote in message 
>news:1158459542.693284.98090 at h48g2000cwc.googlegroups.com...
>> Hi,
>>
>> I have a file that contains a "tcl" list stored as a string.  The list
>> members are
>> sql commands ex:
>> { begin { select * from foo
>>                where baz='whatever'}
>>  {select * from gooble } end
>>  { insert into bar values('Tom', 25) } }
>>
>> I would like to parse the tcl list into a python list...
>>
>> Any suggestions ( I am running Tkinter...)
>>
>> Jerry
>>
>
>This is very similar to the Python list parser that comes with pyparsing. 
>Adapted to your syntax, this looks something like:
>
>tcl = """sql commands ex:
> { begin { select * from foo
>                where baz='whatever'}
>  {select * from gooble } end
>  { insert into bar values('Tom', 25) } }"""
>
>from pyparsing import *
>
>tcllist = Forward()
>element = quotedString | Word(alphas,alphanums+"_") | \
>          Combine(Word(nums) + "." + Optional(Word(nums)) ) | Word(nums) | \
>          oneOf( list(r"(),.+=`~!@#$%^&*-|\?/><;:") ) | Group( '{' + tcllist 
>+ '}' )
>tcllist << OneOrMore( element )
>
>import pprint
>pprint.pprint( tcllist.parseString(tcl).asList() )
>
>Giving:
>
>['sql',
> 'commands',
> 'ex',
> ':',
> ['{',
>  'begin',
>  ['{', 'select', '*', 'from', 'foo', 'where', 'baz', '=', "'whatever'", 
>'}'],
>  ['{', 'select', '*', 'from', 'gooble', '}'],
>  'end',
>  ['{', 'insert', 'into', 'bar', 'values', '(', "'Tom'", ',', '25', ')', 
>'}'],
>  '}']]
>
>The pyparsing home page is at pyparsing.wikispaces.com.
>
>-- Paul
>
>

Noooooooooooooooooo!

I'll be more precise:  pyparsing is quite wonderful, and I'm
all in favor of clever demonstrations of its capabilities.  I
don't think this is one, though; in fact, although I haven't
yet paid enough attention to the original question to provide
an example, I'm reasonably certain that the code offered above
mishandles at least some data (Tcl syntax is different from
what outsiders expect).

Paddy's advice elsewhere in this thread is almost entirely
correct.  Anyone who has imported Tkinter has all of Tcl avail-
able immediately, so much the easiest, most reliable, most
maintainable, and most lucid solution is simply to iterate in
Tcl over the list, and construct thus the corresponding Python
list.

I'm willing to write either a correct implementation, or a
counterexample with problematic data, if these sufficiently
interest readers.  It'll be a day or two before I can make the
time to be careful, though.



More information about the Python-list mailing list