Disassembling strings and turning them into function parameters

M.E.Farmer mefjr75 at hotmail.com
Sun Jan 30 13:32:55 EST 2005


mercuryp... at gmail.com wrote:
> 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

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.123', '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




More information about the Python-list mailing list