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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sullivan WxPyQtKinter

    #1

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

    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'='pri nt',
    'argumentlist'=['a','b','c','d' ,'e']}
    Are there any lib to implement this?


    Ideally , the lib should have a function like below:

    def extract(command string, splitformat)

    eg:
    extract("see#yo u@tonight","wha t#whom@when")
    return: {"what":"see"," whom":"you","wh en":"tonight" }

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

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

    extract("fruit: apple~orgrange~ pear~grape#name :tom;sam;dim;ha m"
    ,"class1:instan ce1[~]#class2:instanc e2[;]")
    return: {"class1":"frui t",
    instance1:['apple','orange ','pear','grape '],
    "class2":"name" ,
    "instance2" :['tom','sam','di m','ham']
    }



    ####### # # # #
    # # # # #
    # # # # #
    # ####### #
    # # # # #
    # # # # #
    # # # # #

  • James Stroud

    #2
    Re: Making a tiny script language using python: I need a string processinglib

    Sullivan WxPyQtKinter wrote:[color=blue]
    > 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'='pri nt',
    > 'argumentlist'=['a','b','c','d' ,'e']}
    > Are there any lib to implement this?
    >
    >
    > Ideally , the lib should have a function like below:
    >
    > def extract(command string, splitformat)
    >
    > eg:
    > extract("see#yo u@tonight","wha t#whom@when")
    > return: {"what":"see"," whom":"you","wh en":"tonight" }
    >
    > extract("1;2;3; 4;5","list[;]")
    > return: {"list":['1','2','3','4' ,'5']}
    >
    > extract("print a,b,c,d,e","com mand arglist[,]")
    > return: {"command":"pri nt","arglist" :['a','b','c','d' ,'e']}
    >
    > extract("fruit: apple~orgrange~ pear~grape#name :tom;sam;dim;ha m"
    > ,"class1:instan ce1[~]#class2:instanc e2[;]")
    > return: {"class1":"frui t",
    > instance1:['apple','orange ','pear','grape '],
    > "class2":"name" ,
    > "instance2" :['tom','sam','di m','ham']
    > }[/color]

    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

    Comment

    • Paul McGuire

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

      "Sullivan WxPyQtKinter" <sullivanz.pku@ gmail.com> wrote in message
      news:1141350818 .374837.197270@ v46g2000cwv.goo glegroups.com.. .[color=blue]
      > 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'='pri nt',
      > 'argumentlist'=['a','b','c','d' ,'e']}
      > Are there any lib to implement this?
      >
      >
      > Ideally , the lib should have a function like below:
      >
      > def extract(command string, splitformat)
      >
      > eg:
      > extract("see#yo u@tonight","wha t#whom@when")
      > return: {"what":"see"," whom":"you","wh en":"tonight" }
      >
      > extract("1;2;3; 4;5","list[;]")
      > return: {"list":['1','2','3','4' ,'5']}
      >
      > extract("print a,b,c,d,e","com mand arglist[,]")
      > return: {"command":"pri nt","arglist" :['a','b','c','d' ,'e']}
      >
      > extract("fruit: apple~orgrange~ pear~grape#name :tom;sam;dim;ha m"
      > ,"class1:instan ce1[~]#class2:instanc e2[;]")
      > return: {"class1":"frui t",
      > instance1:['apple','orange ','pear','grape '],
      > "class2":"name" ,
      > "instance2" :['tom','sam','di m','ham']
      > }
      >
      >
      >
      > ####### # # # #
      > # # # # #
      > # # # # #
      > # ####### #
      > # # # # #
      > # # # # #
      > # # # # #
      >[/color]
      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


      Comment

      • Christos Georgiou

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

        On 2 Mar 2006 17:53:38 -0800, rumours say that "Sullivan WxPyQtKinter"
        <sullivanz.pku@ gmail.com> might have written:
        [color=blue]
        >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'='pr int',
        >'argumentlist' =['a','b','c','d' ,'e']}[/color]

        Have you checked the shlex module in the standard library? It might be
        useful.
        --
        TZOTZIOY, I speak England very best.
        "Dear Paul,
        please stop spamming us."
        The Corinthians

        Comment

        • Sullivan WxPyQtKinter

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

          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.

          Comment

          Working...