Jeff Boone Blog, Technology, Product Update, Aerospike Technology

 

The 2.0 version of the Aerospike Python client is a major release: it adds support for Python 3. Therefore, the client software is now compatible with Python versions 2.6, 2.7, 3.4 and 3.5. For those of you migrating your application from Python 2 to Python 3, there are a few key differences between the two versions to keep in mind as you work through porting the software to the next version.

Strings

This is one area that has changed pretty dramatically between Python 2 and 3. In most cases, you will need to modify code that uses Unicode, encodings or binary data in order to fit into the Python 3 model. Python 3 will not allow mixing text and data, which can help avoid bugs having to do with encoded vs. unencoded text.

This section of the python.org site addresses the string differences between the two major versions with the following list of recommendations:

  • Decide which of your APIs take text, and which take binary data
  • Make sure that your code that works with text also works with unicode, and that your code for binary data works with bytes in Python 2
  • Mark all binary literals with a b prefix, use a u prefix or __future__ import statement for text literals
  • Decode binary data to text as soon as possible; encode text as binary data as late as possible
  • Open files using io.open() and make sure to specify the b mode when appropriate
  • Be careful when indexing binary data

Division

In Python 3, all division between int values will result in a float, whereas Python 2 did floor division. The // operator can be used to perform floor division in Python 3. You can use

from __future__ import division

in your Python 2 code to get the Python 3 behavior. This can help detect errors where floor division is assumed.

Xrange

Python 2 has both a range() function as well as xrange(). The use of xrange() is very popular in Python 2 code as there is an optimized implementation in CPython which is substantially faster than range(). Both functions have the same functionality and syntax.

In Python 3, range() has been implemented like the Python 2 xrange() function; thus, the xrange() function has been eliminated.

Print

The print statement has been replaced with a print() function; thus, it now requires parentheses around the object(s) to be printed.

Exceptions

The syntax for raising and handling exceptions has changed between the two versions.

Raising exceptions

Python 2:
raise MyException, “an error occurred”
Python 3:
raise MyException(“an error occurred”)

Handling exceptions

Python 2:
try:

some_code

except MyException, err:

some_error_handling
Python 3:
try:

some_code

except MyException as err:

some_error_handling

Where to get more information

Some tools/resources that may aid in the migration of your project to Python 3 are listed below:

As always, we need your help and input to continue to improve and enhance your experience on Aerospike. Feel free to contribute your feedback, ideas and questions to our user forum, file Github issues or create a pull request for the next great feature you’d like to contribute to the Aerospike user community!

 

 

About Author

mm
Jeff Boone is the Director of Ecosystem Engineering at Aerospike. His engineering team is responsible for all ecosystem software products to support the Aerospike NoSQL database (clients, connectors, tools and cloud integration).