Helpful and Most Frequented Python References

I’ve never written a Python 2 module so several of these links go directly to the latest stable Python 3 (3.4 in this case). No worries though, you can simply manipulate the URL or select your version once you land on the page. Some of these links are for Python in General, like the style guide. While some of the more instructional links will use their respective authors choice. All is good in Python regardless of version 2 or 3, it’s relatively easy to move back in forth. But what do I know, I’ve only written Python 3 modules.

Any time I’m away from Python for any length of time, I find it helpful to review the “Style Guide for Python” (PEP 8) and the “Structure Your Project” Python Guide. I find that these two pages help me write more Pythonic code and avoid common pitfalls. I like to also think these help me write cleaner code and save me some embarrassment when I publish my code.

Even if others never ever use your Python modules, it is very important to provide meaningful feedback. I like to keep the Built In Exception Hierarchy listing close by so that I can generate the best possible exceptions. This helps make catch statements and traces relevant and informative.

Here are a few more pages I use so frequently that I wanted a nice place to collect them at.

Alex Martelli (Google) Python Design Patterns Video Series:

Advertisements

Calculating MSP430 BSL Checksum

Texas Instruments has a quick little java tool for calculating the checksum of a MSP430 dataframe. Recently, I’ve been seeing some forum posts looking for information on how to calculate the checksum so I thought I would post my quick Python 3 function for others.

def checksum_bsl_frame(frame):
  # http://docs.python.org/3.2/library/struct.html#format-characters
  fmt = "<" + len(frame) // 2*"H"

words = struct.unpack(fmt, frame)

words_xored = 0;

for word in words:
    words_xored ^= word

checksum = words_xored ^ 0xFFFF

return checksum.to_bytes(2, 'little')

Here, framis a bytes or bytesarray object and checksum returns a bytes objects.

Remember, strings are strings in Python3 and bytes and bytearray are data in Python3. Of course, if you are running PySerial on Python3 then this is the format you will be using.

The above code could be simplified I’m sure but it’s nice to have endianness defined out and in the open.

When you want to “human” check the data frame there is an easy one liner way of printing a formatted output.

def print_bsl_frame(frame):
  print(" ".join('{:02X}'.format(byte) for byte in frame))

Now, it’s easy to do things like…

password = bytearray.fromhex("80 10 24 24 00 00 00 00" + 32*" FF")
password.extend(checksum_bsl_frame(password))
print_bsl_frame(password)

The previous would print “80 10 24 24 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 5B CB” to the screen.

I hope this helps someone, it will help you more in a custom class definition.