December 15th, 2004, 05:56 PM
In Visual Basic when you see something like, buffer = buffer & data. In Python would i do this like, buffer = buffer and data or buffer = buffer+data, or some other way? Both of these ways don't seem to work.
December 15th, 2004, 07:16 PM
[Edit: Wait a minute... buffer is a builtin function name (Python 2.3 here). I didn't know that. Maybe that's causing your problem?]
Originally Posted by †Yegg†
As far as I can gather, & only does string concatenation in VB - it uses 'and' for the bitwise AND operator.
Python uses & for bitwise AND, and + for string concatenation so + should be the one to use.
That it doesn't work possibly implies that you aren't getting two strings. If you try to do some odd addition, it will raise a TypeError:
If it isn't working or raising an exception, I can only think that you should put some test prints to check what's happening, such as:
>>> "a" + 1
TypeError: cannot concatenate 'str' and 'int' objects
>>> "a" + ["b"]
TypeError: cannot concatenate 'str' and 'list' objects
That way you should find out what they are and what's in them if possible, or at least have some more information to go on.
print "***A is:"
print "Type: ", type(a)
print "***B is:"
print "Type: ", type(b)
Last edited by sfb; December 15th, 2004 at 07:18 PM.
December 15th, 2004, 07:49 PM
Umm, I didn't realize you need to use spaces between the + sign. Or does that not matter? Also have have another question. I've added ALL of ym bots commands into a list called processlist. How can I get the program to do something like check which items = True and which items = False? For instance, I want it so that if only 1 item = True then the bot will send a message at an interval of 1 second. If 2 items in processlist = True then send a message at an interval of 2 seconds, and so on and so on. Do you know how to do this?
December 15th, 2004, 08:37 PM
It doesn't matter, I just use them because I think it looks a bit clearer.
Originally Posted by †Yegg†
If you only need to know how many items are True or False;
If you need to grab items from a list which meet certain criteria, you can use list comprehensions:
trueCount = processlist.count(True)
falseCount = processlist.count(False)
(If you haven't met that form before, they build a list  straight from a for loop - create a new list of items taken from processlist, but only taking items where the condition is met).
trueItems = [item for item in processlist if item == True]
falseItems = [item for item in processlist if item == False]
This will leave you with lists like:
[True, True, True, True]
which is a bit silly, but since True and False are strange concepts, you could change it to:
This would take things that evaluate to True or False.
trueItems = [item for item in processlist if item]
falseItems = [item for item in processlist if not item]
Things which are "False" for the purposes of if tests include:
0, , (), "", None
Zero, empty lists, empty tuples, empty strings, None
Things which are "True" are everything else. Positive or negative numbers, lists and tuples with things in them, strings with something in them, etc.
How you do the "sending messages at set intervals" would depend on how your program is organised. If it's based on an event driven framework like wxWindows, you could use a timer with an n second interval. Otherwise, you could use time.sleep() to delay for a bit of time before sending again - but that will also freeze your program. I don't know how Twisted would handle it.
I haven't really played with that sort of thing much.
December 16th, 2004, 03:50 PM
Well, once again sfb you have helped me in my time of need, it works great, thanks a lot.