A basic Python question...

Author
Discussion

SturdyHSV

Original Poster:

10,207 posts

173 months

Sunday 7th May 2023
quotequote all
Just a quick one.... I know there are better places to ask but I already have a PH account and don't want some unhelpful neckbeard on StackOverflow to tell me to it's been answered before.

I have some open source Python 2 code that was abandoned 10 years ago, and I have no Python experience whatsoever. I have got it to compile (I have Python 3.11) and it runs but alas I have some issues that appear to be where functionality has changed from Python 2 to 3.

This is namely the use of the 'encode' / 'decode' (I think...) whereby the original source is using .encode("hex") on an int, but that's not allowed any more for 'reasons' that I'm not overly interested in learning about hehe

As an example:

response_checksum = response[module_location + response_len + 2]
response_checksum_hex = response_checksum.encode("hex")

response_checksum is defined as an int, response_checksum_hex is just an 'Any'.

That _hex field is then used shortly afterwards as below, if this gives any additional context...

if int(response_checksum_hex,16) == self.checksum([ord(expecting_module), response_len + 0x55 ] + [int(x,16) for x in response_body_hex]):
#print "Checksum OK!"
return [ hex(ord(expecting_module))[2:], response_len_hex ] + response_body_hex + [response_checksum_hex]

As it's handling serial coms, I'm wary of changing the behaviour if I don't the functionality right... hehe

Is there a simple answer / 'equivalent' I can call, or am I doomed to go down a research rabbit hole understanding how Python 3 has changed its handling of unicode / strings and why that's the coolest thing ever?

EDIT:

I think I've just found a suitable answer, using 'codecs' as per this page:

https://riptutorial.com/python/example/5809/encode...

Edited by SturdyHSV on Sunday 7th May 17:41

TonyRPH

13,108 posts

174 months

Sunday 7th May 2023
quotequote all
I put your question to chatgpt (exactly as you worded it) and:

chatgpt said:
In Python 3, the bytes.hex() method can be used to convert bytes to hexadecimal representation, which is similar to encode("hex") in Python 2.

To fix the issue in your code, you can replace the line:

response_checksum_hex = response_checksum.encode("hex")

with:

response_checksum_hex = response_checksum.to_bytes(1, byteorder='big').hex()

Here, the to_bytes() method is used to convert the integer to a byte string of length 1, and the hex() method is used to convert the bytes to a hexadecimal string.

You can also replace the line:

if int(response_checksum_hex,16) == self.checksum([ord(expecting_module), response_len + 0x55 ] + [int(x,16) for x in response_body_hex]):

with:

if response_checksum == self.checksum([expecting_module, response_len + 0x55 ] + [int(x,16) for x in response_body_hex]):

Here, we're comparing the integer value of response_checksum directly with the result of the checksum() method, instead of converting it to a hexadecimal string.

I hope this helps! Let me know if you have any further questions.