473,785 Members | 2,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1192
me*********@gma il.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*********@gm ail.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.12 3', '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...@gma il.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.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

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
1562
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 with entries like this: myDict = {"desc":"This is Case1", "func1": "Handler1", "func2" : "Handler2", "args": "ACCEPT"} Then my program gets the string "Case 1" from the user, and needs to assign func1 and func2 as handlers for certain events....
5
1873
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 the outside of the world. of cource there are restrictions like modifiers and misc. but i dont want the assembly "disassembled" from someone with "bad intentions"...
2
1047
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 constructor of Employee is as follow: ------------- Employee::Employee(char *firstname, char *lastname, char *address, long salary):
3
1867
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: class Item(object): def __init__(self, height, length, function): params = locals()
89
5167
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 the contents of pString2 to pString? (giving "Blah Blah Blah")
9
5685
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 List() As String) As String() ' Returns the unique values of in array, in an array. Dim Temp As New System.Collections.Specialized.StringCollection()
2
1436
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 realize that substr() need to be used, but words may varie in length so I have to make it initialize first string any length before first space and second string after the space. For example: "computer science" into first string "computer" second...
95
5428
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 ____________________________________ | | | ------------------ | | | BUTTON | | | ...
11
3668
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 store lists in a blob field with the help of dumps() for picklling into a string and then passing the string to the blob. I am also able to get back the string safely and do a loads() to unpickle the object. but this only works when list contains...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.