Python 2.7 and Python 3.8 … the Siblings That Never Got On With Each Other

With Repl.it, it has never been easier to convert

Photo by Shahadat Rahman on Unsplash

Python 2.7 and Python 3.8 … the Siblings That Never Got On With Each Other

With Repl.it, it has never been easier to convert

Well, as some people are Spring cleaning, I’ve been busily doing my own Spring cleaning, and upgrading 100s of my old Python 2.7 scripts on my Web site [here]. Last Thursday was the last Python 2.7 release (Python 2.7.18), and then that will be it.

So, I have upgrade, or my students will complain that they can’t get their crypto scripts to work. Along with this, much of the new code is matched to Python 3, and won’t work with Python 2.

Python was created by Guido van Rosum, and, in 2006, he defined a new standard as PEP (Python Enhancement Proposal) 3000. Overall, Python Version 2 had been around since 2000, and based on PEP 3000, Version 3 came along in 2008. But they never really got on with each other. Version 3 just didn’t like the way that Python 2 was a bit all over the place with its data types and in the loose way of doing string manipulations. The two were just not going to be compatible! For some programs, it was the easy conversion of:

print "Hello world. I am "+name

to:

print ("Hello world. I am ",name)

But, the conversion went much deeper, and where the conversion would often break current implementations.

For upgrades, the community has moved quickly, and at the core is pip, and which is an application that goes to the Python Package Index (PyPI), and pulls down the required library. Most of the code has now moved forward, which makes it extremely difficult to maintain both Version 2 and Version 3.

The thing that has really helped me is Repl.it, and where I could prototype my new code within an easy-to-use environment:

Mostly my libraries worked with the majority just appearing automatically in the pyproject.toml file. For example, a good deal of my code uses this import:

This import depends on PyCryptodome, so it was a matter of just integrating this into my project [here]:

And if a library didn’t work, I just found the version that worked, and Repl.it did the rest. I just love using Repl.it for teaching, as when a student has problems with their I just ask them to send me a share, and I can try and fix any problems they have.

So here are my tips for the many files I upgraded:

A. Copy and paste the code into a new file.

B. Run “2to3 -w” to do:

   
billbuchanan@MacBook-Pro:~$ 2to3 -w elc2.py
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored elc2.py
— — elc2.py (original)
+++ elc2.py (refactored)
@@ -7,33 +7,33 @@
alice = pyelliptic.ECC()
bob = pyelliptic.ECC()

This can then be pasted into Repl.it. This is not perfect but will catch most of the print statements that need to be converted to print(“”).

C. Look at the imports, and see if Repl.it can import the required files into pyproject.toml. If not, then go to Pypi, and find the latest version and define this in the toml file:

[tool.poetry.dependencies]
pycrypto = “2.6.1”

D. Get the code to run, and check against the Python 2.7 version (very important in crypto, as it might run, but might give the wrong outputs).

If errors or not working:

E. Find integer divisions (‘/’) and fix with “//”.

F. Find string conversions (str) that need to be converted into byte arrays with str.encode(). Please try and understand the conversion from strings into byte arrays (with .encode()) and then from byte arrays to strings (with .decode()).

G. Watch out for those integers with “xxL”, where there is an “L” on the end. The ‘L’ needs to be deleted, and also watch when they are parsed as a string, as some conversions look to remove the “L”, and can end up removing a digit.

H. The 2to3 application doesn’t fix indentation issues, but Repl.it is more sensitive with these. So, it’s a good idea to go back over the code, and reapply with tab spaces (but watch you do not break the code!). Repl.it has a good history buffer — but sometimes crashes — so you can easily backtrack.

Now, I think, I am all converted. If you spot some old Python 2.7 code, please tell me.

My advice …

  1. go learn Python!!!!!!

2. go learn Python!!!!!!

3. go learn Python!!!!!!

4. go learn Python!!!!!!

5. go learn Python!!!!!!

6. goto 1.

Here is the power of Repl.it and Python with COVID-19 infection models: