Disassembling strings and turning them into function parameters

M

mercuryprey

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
 
P

Peter Hansen

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..

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
 
F

Fredrik Lundh

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..

you can use the split() method to split on whitespace:
['connect', '123.123.123.123', '21', 'user', 'password']

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

http://effbot.org/librarybook/cmd.htm
http://docs.python.org/lib/module-cmd.html

</F>
 
M

M.E.Farmer

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
 
M

mercuryprey

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top