## Pycse is now using Python3

| categories: python | tags:

I spent the last couple of days going through the notes for pycse and updating it for Python 3! If you aren't familiar with pycse, it is an acronym for Python3 Calculations in Science and Engineering, and it is about 400 pages of examples spanning scientific and engineering computations, and a python module that extends numpy/scipy with some functions for adding events to differential equation solvers, and regression with confidence intervals.

It is mostly done, and was pretty easy. The Python module for pycse (http://github.com/jkitchin/pycse ) should also be Python 3 compliant. Yep, on my Mac I have switched over to the Anaconda Python 3 from Continuum IO (thanks for that!).

import sys
print(sys.version)

3.5.1 |Anaconda 2.5.0 (x86_64)| (default, Dec  7 2015, 11:24:55)
[GCC 4.2.1 (Apple Inc. build 5577)]


Now we can succinctly do matrix multiplication like this:

import numpy as np
a = np.array([1, 2, 3])
a = np.array([1, 2, 3])
print(a @ a)  # the matrix multiplication operator

print(np.dot(a, a))

14
14


Here is a summary of what the changes to the Python2 version of Pycse entailed.

1. Change all print statements to print functions. There were so many…
2. Eliminate use of tabs in some code blocks, Python3 is not flexible on that.

I wrote this function to fix both of these for me (I use Emacs as my editor), which worked nicely:

(defun fp ()
"convert print to print() allowing for line end comments. does not do multiline.
and untabify"
(interactive)
(beginning-of-line)
(re-search-forward "print " (line-end-position) t)
(delete-char -1)
(insert "(")
;; rm spaces
(delete-forward-char
(save-excursion
(skip-chars-forward " ")))
(when
(re-search-forward "#" (line-end-position) 'move)
(goto-char (match-beginning 0)))
(skip-chars-backward " ")
(insert ")")
;; untabify
(let ((src (org-element-context)))
(untabify (org-element-property :begin src)
(org-element-property :end src))))

1. reduce no longer exists, you have to use functools.reduce. Probably will not affect me much…
2. Strings are sometimes bytes, and need to be encoded/decoded depending on what you are doing.

Otherwise, most things seemed to work fine. In a few places I had articles on Windows specific code, which I couldn't test on the Mac I work on now. Only one package was apparently not ready for Python3, and that was scimath from Enthought, which had units capability. Quantities worked fine though.

For some reason I could not do interactive key-presses in matplotlib. I am not sure if that is a Python3, or my Mac setup for now.

When I first wrote the Pycse book (in org-mode naturally), I must have had a default session setup for org-mode, because there was no :session information in a few of the src-block headers. I had to add these back in a few places.

Overall the transition was pretty seamless. It looks like I will be transitioning to Python3 in teaching this fall! Big thanks to the Anaconda team for packaging Python 3.5 and making it so easy to install!