The Blog: Python Code Libraries Updates

08 Jul 2017

We are pleased to announce that we have a new and updated Python library available to use with our Raspberry Pi expansion boards. The new version 2.0 update includes several major changes which can be found in the changelog.md in the root of the github repository.

Since we launched the python library in 2014 it has grown organically with regular updates to the features and new boards and demos added. The result of all those updates is that the structure of the library had become untidy as the demos were in the same folders as the board classes and helper functions. The classes and demos were also not PEP8 compliant and the naming conventions were not consistent across the code.

For several years we have maintained two separate versions of the Python library for Python 2 and Python 3 releases. This meant that any change to one library would need to be duplicated which added extra work and the potential for bugs to creep into the code.

With these issues in mind we decided that it was time to restructure the library and bring everything up to the current Python PEP8 standards.

The new Python library is now compatible with Python 2 and 3 allowing you to develop your applications in either version from a single code base.

The demo files have been moved into a demos subfolder in each of the class folders making the library more organised. This change will make it easier for us to add more demos and tutorials for our expansion boards without adding clutter to the class folders.

We have renamed the class files removing the ABE_ from all the names so for example ABE_ADCPi is now ADCPi. We have also merged the ABE_Helper.py into the main classes so you no longer need to create an instance of the helper class and pass it into the board class. These changes should reduce the amount of code needed to use our expansion boards.

To give you an example with the previous version of the Python library you would need the following code to initialise an ADC Pi object:

All the function names are still the same so if you want to use the 2.0 release with your current project the code changes you need to make will be minimal.

As well as reducing the amount of code you need to write we have also gone through all the classes optimising the functions and adding more error protection. In the previous release, errors were printed to the console. With 2.0 we have removed all the print calls and changed the code to raise an exception when an error occurs. This should make it easier for you to manage exceptions and deal with them accordingly. We have also added extra input validation so the functions should raise a ValueError if it gets an unexpected value.

On the ADCPi and ADCDifferential Pi classes we have made several optimisations to reduce the number of function calls needed during each ADC sample. While this won’t have any major impact to the sample rate which is still limited by the ADC it does mean your Raspberry Pi will use less CPU cycles, improving application performance and power usage.

With the set_dac_raw() function in the ADCDACPi and ExpanderPi classes we have completely rewritten the way it calculates the values before sending them to the DAC which should give you a speed improvement of around 48%. On a Raspberry Pi 2 using the previous version of the Python library you could generate a 6KHz square wave from the DAC output, this has been increased to over 10KHz on the updated version.

The other major change which has been requested from several of our customers is the library can now be installed into the Python library path on your Raspberry Pi by using PIP or running the new setup.py file in the root of the library.