Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Disassembling strings and turning them into function parameters

Reply
Thread Tools

Disassembling strings and turning them into function parameters

 
 
mercuryprey@gmail.com
Guest
Posts: n/a
 
      01-30-2005
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

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      01-30-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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..


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
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-30-2005
<(E-Mail Removed)> wrote:

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

>>> s = "connect 123.123.123.123 21 user password"
>>> s.split()

['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>



 
Reply With Quote
 
M.E.Farmer
Guest
Posts: n/a
 
      01-30-2005
(E-Mail Removed) 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

 
Reply With Quote
 
mercuryprey@gmail.com
Guest
Posts: n/a
 
      01-30-2005
Hey,
that's exactly what I need! Thanks for your help, the others too of
course Didn't expect to get answers so quickly..

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disassembling a Lumix Camera Sqwertz Digital Photography 1 01-14-2013 05:11 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Disassembling a notebook battery Ike Computer Support 5 05-26-2006 02:38 AM
Method for turning strings into code Steve Litt Ruby 8 12-31-2005 03:37 PM
Turning Strings into Functions Freddy Python 3 09-26-2003 03:51 PM



Advertisments