Disassembling strings and turning them into function parameters

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

    #1

    Disassembling strings and turning them into function parameters

    Hi,
    I'm pretty new to Python, to programming overall...so how would I make
    something where the user inputs multiple words in a string - like
    "connect 123.123.123.123 21 user password" or similar, and then I can
    split this string up to pass these arguments to a function like
    ftp_connect(ip, port, user, pw) etc...? I have no idea how to "break"
    the string up so I can get these out of it..


    thanks for answers,
    munin

  • Peter Hansen

    #2
    Re: Disassembling strings and turning them into function parameters

    mercuryprey@gma il.com wrote:[color=blue]
    > Hi,
    > I'm pretty new to Python, to programming overall...so how would I make
    > something where the user inputs multiple words in a string - like
    > "connect 123.123.123.123 21 user password" or similar, and then I can
    > split this string up to pass these arguments to a function like
    > ftp_connect(ip, port, user, pw) etc...? I have no idea how to "break"
    > the string up so I can get these out of it..[/color]

    The .split() method of strings should work for you.

    If you need more, provide more background... and maybe
    let us know that this isn't homework. ;-)

    -Peter

    Comment

    • Fredrik Lundh

      #3
      Re: Disassembling strings and turning them into function parameters

      <mercuryprey@gm ail.com> wrote:
      [color=blue]
      > I'm pretty new to Python, to programming overall...so how would I make
      > something where the user inputs multiple words in a string - like
      > "connect 123.123.123.123 21 user password" or similar, and then I can
      > split this string up to pass these arguments to a function like
      > ftp_connect(ip, port, user, pw) etc...? I have no idea how to "break"
      > the string up so I can get these out of it..[/color]

      you can use the split() method to split on whitespace:
      [color=blue][color=green][color=darkred]
      >>> s = "connect 123.123.123.123 21 user password"
      >>> s.split()[/color][/color][/color]
      ['connect', '123.123.123.12 3', '21', 'user', 'password']

      btw, the cmd module might be useful for your project:


      Source code: Lib/cmd.py The Cmd class provides a simple framework for writing line-oriented command interpreters. These are often useful for test harnesses, administrative tools, and prototypes tha...


      </F>



      Comment

      • M.E.Farmer

        #4
        Re: Disassembling strings and turning them into function parameters

        mercuryp...@gma il.com wrote:[color=blue]
        > Hi,
        > I'm pretty new to Python, to programming overall...so how would I[/color]
        make[color=blue]
        > something where the user inputs multiple words in a string - like
        > "connect 123.123.123.123 21 user password" or similar, and then I can
        > split this string up to pass these arguments to a function like
        > ftp_connect(ip, port, user, pw) etc...? I have no idea how to "break"
        > the string up so I can get these out of it..
        >
        >
        > thanks for answers,
        > munin[/color]

        Ok well this is pretty basic but it sounds wrong on some level.
        Maybe you should post some code, you will get better responses.
        You could try something like:
        Py>stuff = "connect 123.123.123.123 21 user password"
        Py>parts_list = stuff.split()# can handle other seperators
        Py>print parts_list
        ['connect', '123.123.123.12 3', '21', 'user', 'password']
        Py>def StrFtp(userinfo ):
        .... parts = userinfo.slpit( )
        .... funktion, ip, port, usedr, pw = parts
        .... funktion = funktion.lower( )
        .... if funktion == 'connect'
        .... return ftp_connect(ip, port, user, pw)
        .... elif funktion == 'other_function _name_here':
        .... return 'your other action here'
        .... else:
        .... return None

        Py>ftpconnect = StrFtp("connect 123.123.123.123 21 user password")

        Also add asserts and error checking all through the code for malformed
        input.
        hth,
        M.E.Farmer

        Comment

        • mercuryprey@gmail.com

          #5
          Re: Disassembling strings and turning them into function parameters

          Hey,
          that's exactly what I need! Thanks for your help, the others too of
          course :) Didn't expect to get answers so quickly..

          Comment

          Working...