@radossslaw a few comments, not related to the error message:
The code piece you show has no line 54.
the indentation of the first line seems to be wrong
But this starting section is odd:
num=data
num=int(num)
num=str(num)
x=num[0]
y=num[1:3]
It starts with data, which is a bytearray. Converting it to int() and back to str() makes it a string with whitespace stripped. You would get the same with: num = data.strip().decode(). Then you take two substrings, x and y.
These are not numbers. And I guess, that you uses these later in the code, e.g. in line 54, in a comparison with a number, which fails.