tcl list to python list?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jerry.levan@gmail.com

    tcl list to python list?

    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

  • Paddy

    #2
    Re: tcl list to python list?


    jerry.levan@gma il.com wrote:
    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
    Well, if you know TCL then read the bit about how to write a python
    list:
    (http://docs.python.org/tut/node5.htm...00000000000000)
    Then write a TCL function to traverse your TCL list and write it out in
    the python format.

    Another suggestion might be to use CSV as an intermediary:



    Now if you know Python and not TCL ... :-)

    - Paddy.

    Comment

    • Paul McGuire

      #3
      Re: tcl list to python list?

      <jerry.levan@gm ail.comwrote in message
      news:1158459542 .693284.98090@h 48g2000cwc.goog legroups.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,alp hanums+"_") | \
      Combine(Word(nu ms) + "." + Optional(Word(n ums)) ) | Word(nums) | \
      oneOf( list(r"(),.+=`~ !@#$%^&*-|\?/><;:") ) | Group( '{' + tcllist
      + '}' )
      tcllist << OneOrMore( element )

      import pprint
      pprint.pprint( tcllist.parseSt ring(tcl).asLis t() )

      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.wikis paces.com.

      -- Paul


      Comment

      • Cameron Laird

        #4
        Re: tcl list to python list?

        In article <CE4Pg.4266$vD2 .1873@tornado.t exas.rr.com>,
        Paul McGuire <ptmcg@austin.r r._bogus_.comwr ote:
        ><jerry.levan@g mail.comwrote in message
        >news:115845954 2.693284.98090@ h48g2000cwc.goo glegroups.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,alp hanums+"_") | \
        Combine(Word(nu ms) + "." + Optional(Word(n ums)) ) | Word(nums) | \
        oneOf( list(r"(),.+=`~ !@#$%^&*-|\?/><;:") ) | Group( '{' + tcllist
        >+ '}' )
        >tcllist << OneOrMore( element )
        >
        >import pprint
        >pprint.pprin t( tcllist.parseSt ring(tcl).asLis t() )
        >
        >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.wikis paces.com.
        >
        >-- Paul
        >
        >
        Noooooooooooooo oooo!

        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.

        Comment

        • Paul McGuire

          #5
          Re: tcl list to python list?

          "Cameron Laird" <claird@lairds. uswrote in message
          news:3k70u3-3d8.ln1@lairds. us...
          In article <CE4Pg.4266$vD2 .1873@tornado.t exas.rr.com>,
          Paul McGuire <ptmcg@austin.r r._bogus_.comwr ote:
          >
          <yet another post showing a pyparsing stab at a parsing problem, this time
          parsing Tcl lists>
          >
          Noooooooooooooo oooo!
          >
          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.
          Cameron -

          Er? Thanks for the nice comments re: pyparsing, sometimes I feel a little
          self-conscious always posting these pyparsing snippets. So I'm glad you
          clarified your intent with your "I'll be more precise" paragraph. But I'm
          not sure I see the reason for an 18-O "No!"

          "Outsider"? I'm no stranger to Tcl (although I *am* a bit rusty). In the
          90's I worked for several years with a script-based process control system
          for semiconductor manufacturing, in which we used Tcl for customer-defined
          control logic. Tcl led me to appreciate the value of a scripting language,
          despite its clunky assignment syntax ("set x 1"), and "$"-happiness. When I
          looked into Python a few years later, I remember thinking "Man, I wish we
          could have used Python instead."

          I took a look at how accessible Tcl is using Tkinter. It looks to me like
          one would need to:
          - write a Tcl proc to recursively traverse a list, returning a stringified
          list in Python-like syntax (enclosing non-list items in ''s, separating with
          commas, nesting sublists with []'s, etc.)
          - construct a string passing the OP's Tcl list string "sql commands ex:
          ...." to this method
          - invoke Tkinter.Tk().ev al on this string to perform the Tcl traversal
          - invoke Python's eval function on the returned string to get an actual
          list.

          Did I get that right? Or is there another, simpler back door into Tcl from
          Tkinter?

          I'll confess, I wrote my sample code based on the sample text posted by the
          OP, without much extra syntax (such as negative integers or floating-point
          values). So here is a version with a bit broader coverage:
          =============== =======
          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 | Combine(Optiona l("$") + Word(alphas,alp hanums+"_"))
          | \
          Combine(Optiona l(oneOf(list("+-")))+ Word(nums) + "." +
          Optional(Word(n ums)) ) | Word(nums+"+-",nums) | \
          oneOf( list(r"(),.+=`~ !@#$%^&*-|\?/><;:") ) | Group( '{' + tcllist
          + '}' )
          tcllist << ZeroOrMore( element )

          import pprint
          pprint.pprint( tcllist.parseSt ring(tcl).asLis t() )
          =============== =======

          This should handle empty lists, signed integers and reals, and variables
          with leading '$' signs.

          Before putting too much effort into problematic counter-example data, it
          might be a good idea to check with the OP, to see what types of values are
          likely to come up in practice. Given that c.l.py posting is only a
          spare-time activity (probably so for you, too), I wouldn't want to waste
          time on the be-all-and-end-all Tcl list parser that handles cases the OP
          wont run into.

          -- Paul


          Comment

          • Cameron Laird

            #6
            Re: tcl list to python list?

            In article <1158459542.693 284.98090@h48g2 000cwc.googlegr oups.com>,
            <jerry.levan@gm ail.comwrote:
            >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...)

            Comment

            • Cameron Laird

              #7
              Re: tcl list to python list?

              In article <AclPg.4515$vD2 .429@tornado.te xas.rr.com>,
              Paul McGuire <ptmcg@austin.r r._bogus_.comwr ote:

              Comment

              • Paul McGuire

                #8
                Re: tcl list to python list?

                "Cameron Laird" <claird@lairds. uswrote in message
                news:gv51u3-4j4.ln1@lairds. us...
                In article <1158459542.693 284.98090@h48g2 000cwc.googlegr oups.com>,
                <jerry.levan@gm ail.comwrote:
                >>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...)
                .
                .
                .
                No correct solution's going to be as elegant as I suspect you imagine.
                Here's an example of what's required:
                >
                # If you try anything you suspect is simpler, you're going to have to
                # teach Python about Tcl treatment of whitespace, or teach Tcl how
                # Python quotes, or ...
                >
                import Tkinter
                tcl_list = """{ begin { select * from foo
                where baz='whatever'}
                {select * from gooble } end
                { insert into bar values('Tom', 25) } }"""
                >
                # Collect the Python list here.
                result = []
                # Create a single Tcl interpretive context for all the work.
                tk_instance = Tkinter.Tk().tk .eval
                # Everything Tcl returns is a string; make this value an integer.
                tcl_list_length = int(tk_instance (
                "set tcl_list %s; llength $tcl_list" % tcl_list))
                >
                # With all the set-up done, simply loop over the elements.
                for counter in range(tcl_list_ length):
                # Ask Tcl for each successive list item.
                result.append(t k_instance("lin dex $tcl_list %d" % counter))
                >
                print result
                >
                The output is
                ['begin', " select * from foo\n where baz='whatever'" ,
                'select * from gooble ', 'end', " insert into bar values('Tom', 25) "]
                Elegant-shmelegant, looks like it gets the job done, and neatly too. I had
                no idea that you can invoke Tcl so easily from Python.

                Why is your indentation so weird though? The comments actually make your
                solution harder to read. If I may be so forward as to edit for readability
                (I think a list comprehension to build the actual list is easier to follow
                than the for loop with the strangely-indented comments):

                # Create a single Tcl interpretive context for all the work.
                tk_instance = Tkinter.Tk().tk .eval

                # define list in Tcl context, and extract number of elements
                tk_instance("se t tcl_list %s" % tcl_list)
                numItems = int(tk_instance ("llength $tcl_list"))

                # build Python list indexing by each item
                result = [ tk_instance("li ndex $tcl_list %d" % i)
                for i in range(numItems)]


                -- Paul


                Comment

                • Paddy

                  #9
                  Re: tcl list to python list?


                  Paul McGuire wrote:
                  "Cameron Laird" <claird@lairds. uswrote in message
                  news:gv51u3-4j4.ln1@lairds. us...
                  In article <1158459542.693 284.98090@h48g2 000cwc.googlegr oups.com>,
                  <jerry.levan@gm ail.comwrote:
                  >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...)
                  .
                  .
                  .
                  No correct solution's going to be as elegant as I suspect you imagine.
                  Here's an example of what's required:

                  # If you try anything you suspect is simpler, you're going to have to
                  # teach Python about Tcl treatment of whitespace, or teach Tcl how
                  # Python quotes, or ...

                  import Tkinter
                  tcl_list = """{ begin { select * from foo
                  where baz='whatever'}
                  {select * from gooble } end
                  { insert into bar values('Tom', 25) } }"""

                  # Collect the Python list here.
                  result = []
                  # Create a single Tcl interpretive context for all the work.
                  tk_instance = Tkinter.Tk().tk .eval
                  # Everything Tcl returns is a string; make this value an integer.
                  tcl_list_length = int(tk_instance (
                  "set tcl_list %s; llength $tcl_list" % tcl_list))

                  # With all the set-up done, simply loop over the elements.
                  for counter in range(tcl_list_ length):
                  # Ask Tcl for each successive list item.
                  result.append(t k_instance("lin dex $tcl_list %d" % counter))

                  print result

                  The output is
                  ['begin', " select * from foo\n where baz='whatever'" ,
                  'select * from gooble ', 'end', " insert into bar values('Tom', 25) "]
                  >
                  Elegant-shmelegant, looks like it gets the job done, and neatly too. I had
                  no idea that you can invoke Tcl so easily from Python.
                  >
                  Why is your indentation so weird though? The comments actually make your
                  solution harder to read. If I may be so forward as to edit for readability
                  (I think a list comprehension to build the actual list is easier to follow
                  than the for loop with the strangely-indented comments):
                  >
                  # Create a single Tcl interpretive context for all the work.
                  tk_instance = Tkinter.Tk().tk .eval
                  >
                  # define list in Tcl context, and extract number of elements
                  tk_instance("se t tcl_list %s" % tcl_list)
                  numItems = int(tk_instance ("llength $tcl_list"))
                  >
                  # build Python list indexing by each item
                  result = [ tk_instance("li ndex $tcl_list %d" % i)
                  for i in range(numItems)]
                  >
                  >
                  -- Paul
                  Thanks, Cameron, Paul;
                  I'm still in the Electronics business, where TCL is everywhere. I too
                  did not know how easy it is to invoke TCL from inside Python. Yet more
                  ammunition in my quest to be allowed to write more Python at work.

                  - Cheers, Paddy.

                  Comment

                  Working...