Thread: Quick Question

    #1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155

    Quick Question


    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Originally Posted by †Yegg†
    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.
    [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?]

    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.

    Code:
    >>> "a" + "b"
    'ab'
    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:

    Code:
    >>> "a" + 1
    ...
    TypeError: cannot concatenate 'str' and 'int' objects
    >>>
    >>> "a" + ["b"]
    ...
    TypeError: cannot concatenate 'str' and 'list' objects
    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:

    Code:
    print "***A is:"
    print "Type: ", type(a)
    print str(a)
    print "***B is:"
    print "Type: ", type(b)
    print str(b)
    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.
    Last edited by sfb; December 15th, 2004 at 07:18 PM.
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    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?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Originally Posted by †Yegg†
    Umm, I didn't realize you need to use spaces between the + sign. Or does that not matter?
    It doesn't matter, I just use them because I think it looks a bit clearer.

    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?
    If you only need to know how many items are True or False;
    Code:
    trueCount = processlist.count(True)
    falseCount = processlist.count(False)
    If you need to grab items from a list which meet certain criteria, you can use list comprehensions:

    Code:
    trueItems = [item for item in processlist if item == True]
    falseItems = [item for item in processlist if item == 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).

    This will leave you with lists like:
    [True, True, True, True]
    [False, False]

    which is a bit silly, but since True and False are strange concepts, you could change it to:

    Code:
    trueItems = [item for item in processlist if item]
    falseItems = [item for item in processlist if not item]
    This would take things that evaluate to True or False.
    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.
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Well, once again sfb you have helped me in my time of need, it works great, thanks a lot.

IMN logo majestic logo threadwatch logo seochat tools logo