I know that this is not really Panda3D-related, but may be somebody can give me a short help/hint…
I want to determine a strings length because I want to search the string for a particular character… I want to do it the way that I want to determine the strings complete length and then just let python search each character of the list(string) from 0 to strlen(string) and compare if the character searched is present.
My question is: is there a operation similar ot strlen() from C++ or a more secure one integrated into python that can tell me the string size? What do I need to import? Due to the fact that this function will be called later very often it would be good to have a simple solution for that… In case I can do it without the need to get the exact strings size, I would be glad too, but I do not know if the character will be contained and I can’t use a search for another character that I have designed to be hit first to show the end of the string (because I can’t be sure that the string will be transmitted correctly)
...snip
received=str(line)
length_received=len(received)
if received[0]=='!':
spaceAtLocation=0
spaceAtLocation=self.compare(received,length_received,' ')
print "space at location: ",spaceAtLocation
def compare(self, text='text', length=1, compareValue=' '):
i=0
while i<length:
if text[i]==compareValue:
return(i)
else:
i=i+1
...snip
What does nothing else than just search for a space to separate a command from variables that can be given from within a console/chat client/whatever and returns the position of the space. With that knowledge you can now sort out the command and handle the command separate from the arguments.
Thanks… that is working - as long as you don’t have more than 1 space in your string because IF you have that, you will get “ValueError: unpack list of wrong size”
Regards, Bigfoot29
Edit: A simple way to avoid it:
import string
text='test me'
i=0
for c in text:
if c==' ':
i=i+1
if i==1:
(command, data)=string.split(text, ' ')
print command
print data
But as said… thanks for your help I guess the way it is now is the most elegant one… g
text = "createnew object color=blue"
args = text.split(" ")
#This would give you a list called args that contains each word
#as a seperate entry.
You’ll have to choose to either seperate each word into it’s own entry, or to seperate only the first one out. Basically, if you anticipate that most commands are going to take a single string argument (like /tell or /msg), go with zpavlov’s version, as you’ll end up doing extra commands less often. If, on the other hand, you find that most often commands are called with multiple single-world arguments, it would probably be more efficient to use the above method.
As for the length question, just to add some info:
len() is a built-in function that pretty much just calls len() on whatever object you pass it, so if you have a custom class that you want to get the length of (in a manner that most python people are familiar with), you can just implement the len method:
Obviously that example is silly because you could just derive a class from List and not need to reimplement the len func, but it shows the concept. This pattern applies to most every built-in function, you can write str, repr, cmp, add, and just about every other mathematical or logical operation you can use in the language.