Advanced Python (Fall 2017)/lecture1

From EIK wiki

Lecture 1

When to use what python version

Use python 3 if you:

are starting a new project, that you know will only be used internally, or used once, or used by people you know will use python 3.

When to python 2:

you are using an existing python 2 code base. or there is a python2 only package and there are no alternatives.

When to write code that will work on both python 2 and 3:

You are making a public package that may be used by people using either version. Or you are adding to a python2 project, and you want it to be forward compatible.

I recommend that you write python 3 code or write code that is compatible with both versions.

Python 2 and 3 differences

There are many small differences between python 2 and python 3. here is a comprhensive list: http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html

Strings

In python 3, all strings are unicode. python 2 strings are not any specific encoding. They are actually a list of bytes.

This can cause problems when you try to use special characters. the following code will not work on python 2, but will work on python 3.

"Hello {}".format(u"Kõrvits")

on python 2, you will get this

In [1]: "Hello {}".format(u"Kõrvits")
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-1-462f402d17a8> in <module>()
----> 1 "Hello {}".format(u"Kõrvits")

UnicodeEncodeError: 'ascii' codec can't encode character u'\xf5' in position 1: ordinal not in range(128)

To make a string explicitly unicode typed, put a 'u' before a string literal

u'hi'

or import

from __future__ import unicode_literals

which will make all string literals unicode.

Python Style

python has a very strict and standarized and widely style guide. It is referred to as "PEP8". PEP is the Python Enhancement Project, and 8 is the number of the proposal. This proposal, PEP8, contains the style rules for python code. Read it here: https://www.python.org/dev/peps/pep-0008/

it should be followed.

Checking the style guide

use a package called 'flake8'. It is quite sensible out of the box. It does not check naming, or all style rules. It checks spacing and syntax.

to install 'pip install flake8'

to run 'python -m flake8 PATH'

You may also use 'PyLint', which is much more robust and checks more things, including naming and documentation. It has to be configured because it is often too strict.

There is also 'autopep8', which reformats your code to follow PEP8. ## Task 1

Make bad legacy python code styled correctly and run on both python 2 and 3

# coding: utf-8

nAMES = [u'Võsandi',u'hyvä'];
for Name in nAMES:
    if 'y' in Name:
        print('{} is Finnish'.format(Name));
    else:
        print('{} is Estonian'.format(Name));

Solution

# coding: utf-8

from __future__ import unicode_literals
from __future__ import print_function
NAMES = ['Võsandi', 'hyvä']

for name in NAMES:
    if 'y' in name:
        print('{} is Finnish'.format(name))
    else:
        print('{} is Estonian'.format(name))