473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Jul 18 '05 #1
4 1173
me*********@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..


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
Jul 18 '05 #2
<me*********@gmail.com> 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>

Jul 18 '05 #3
mercuryp...@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

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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Freddy | last post by:
How do you turn a string into a callable function? I'm writing a program which takes user input and then looks up the associated function to run from a shelved dictionary. So, I have a dictionary...
5
by: Kerem Gümrükcü | last post by:
Hi, how can i protect a assembly against disassembling with ILDASM and other products like that. i have a dll with some encryption methods implemented and i dont want them to be exposed, to...
2
by: wongjoekmeu | last post by:
Hello All, I have the following C++ code which I do not understand. I have a class Employee and a class String. Employee is using data members of type String. The definition of the overloaded...
3
by: googleboy | last post by:
Hi there. I have defined a class called Item with several (about 30 I think) different attributes (is that the right word in this context?). An abbreviated example of the code for this is: ...
89
by: scroopy | last post by:
Hi, I've always used std::string but I'm having to use a 3rd party library that returns const char*s. Given: char* pString1 = "Blah "; const char* pString2 = "Blah Blah"; How do I append...
9
by: Brian Tkatch | last post by:
I'm looking for a simple way to unique an array of strings. I came up with this. Does it make sense? Am i missing anything? (Testing seems to show it to work.) Public Function Unique(ByVal...
2
by: varusnyc | last post by:
Hello, I just started learning strings and dont even know how to properly execute simple part of the program that splits phrase of two words with one space in between into 2 separate strings. I...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
11
by: krishnakant Mane | last post by:
hello, I finally got some code to push a pickled list into a database table. but now the problem is technically complex although possible to solve. the problem is that I can nicely pickle and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.