Need help converting code to 2.5

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
Forum rules
This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
User avatar
berkinet
Posts: 3441
Joined: Tue Nov 18, 2008 2:08 pm
Location: Berkeley, CA, USA & Mougins, France

Need help converting code to 2.5

Post by berkinet »

The following (from a Phidgets device driver) is written in Python 2.6 and does not work in 2.5:

Code: Select all

        hexArray = c_char*16
        self.__hexLookup = hexArray(b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D', b'E', b'F')
results in:

Code: Select all

    self.__hexLookup = hexArray(b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D', b'E', b'F')
                                   ^
SyntaxError: invalid syntax
Here is where it is actually used:

Code: Select all

        str = ""
        for i in range(5):
            str += (self.__hexLookup[int(tagValue[i] / 16)]).decode()
            str += (self.__hexLookup[int(tagValue[i] % 16)]).decode()
If I print out the individual values in tagValue (tagValue) I see a set of strings representing ints. Like: 1, 6, 142, 7, 129
But, if I try to print tagValue
itself I get: ctypes.LP_c_ubyte object at 0x4935d0

I am guessing the output is supposed to be a hex string. Unfortunately, the original author is no longer at Phidgets, and no one there is sure what the out pit should look like.

Any help replacing this with 2.5 compatible code would be greatly appreciated.
User avatar
berkinet
Posts: 3441
Joined: Tue Nov 18, 2008 2:08 pm
Location: Berkeley, CA, USA & Mougins, France

Re: Need help converting code to 2.5

Post by berkinet »

I answered my own question. For those who were interested, this:
  • tag += str("%02x" % tagValue).upper()


accomplishes the same thing as:
  • str += (self.__hexLookup[int(tagValue / 16)]).decode()
    str += (self.__hexLookup[int(tagValue % 16)]).decode()


and I just dropped the array declaration altogether.
Last edited by berkinet on Mon Jan 02, 2012 9:16 pm, edited 2 times in total.
User avatar
jay (support)
Site Admin
Posts: 19232
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Need help converting code to 2.5

Post by jay (support) »

I think you can just remove the "b" on each argument. According to the Python docs on string literals:
A prefix of 'b' or 'B' is ignored in Python 2
Clearly it's not ignored in Python 2.5 since it thinks it's a syntax error.
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
User avatar
berkinet
Posts: 3441
Joined: Tue Nov 18, 2008 2:08 pm
Location: Berkeley, CA, USA & Mougins, France

Re: Need help converting code to 2.5

Post by berkinet »

jay wrote:I think you can just remove the "b" on each argument.
Thanks Jay, you and I were posting at the same time. BTW, Your suggestion also worked. :)
Locked

Return to “Extending Indigo with Plugins and Python”