25
Pycourse – Self driving python course Eran Shlomo, Intel perceptual computing [email protected] ©

PyCourse - Self driving python course

Embed Size (px)

Citation preview

Page 1: PyCourse - Self driving python course

Pycourse – Self driving python course

Eran Shlomo, Intel perceptual [email protected] ©

Page 2: PyCourse - Self driving python course

About me

Haifa IoT Ignition lab and IPP(Intel ingenuity partnership program) tech lead. Intel Perceptual computing.Python expert, started around 2009 with silicon analytics. Focus on Data science and Machine learning in recent years (A lot of python).

Page 3: PyCourse - Self driving python course

About the PyCourse

Self driving course Just follow the slides, it is built both for self work as well as class work. If you are not in class make sure to read the attached references and links. If you are in class then read them later.

Page 4: PyCourse - Self driving python course

Why do you want to learn python ?• Very fast prototyping.• Cross platform.• Rich libraries and capabilities.• Good interactive/console experience. • Fast engine (in script land)• The leading language in AI world. • More on The good, The bad and the ugly of python can be found

here: https://www.slideshare.net/EranShlomo/python-the-good-the-bad-and-the-ugly

Page 5: PyCourse - Self driving python course

Before we start • Download and Install python 2.7 • Download, do not install :• Download, python 3.5, pycharm, miniconda and google python classes.

Page 6: PyCourse - Self driving python course

Hello world • Lets check we have python installed and good to go:• Go to the folder you have extracted the course package zip, lets call it our working dir • Open cmd in your working dir and check python is functional and in the right (2.7.12) version:

• If no python update your path, if version is incorrect change path variable order, you can check its properly configured using where :

Lets make a simple hello world file and run it, create a file called hello.py and put in it a single line printing hello: • print "hello pycourse“

• Save and run it in cmd console:• python hello.py

No python, in cmd type : set PATH=C:\Python27;%PATH%

Page 7: PyCourse - Self driving python course

Python versions Python versions have significant importance, Python world struggles to move between versions and there are many compatibility issues. Lets run our script on python 3:• Install python 3.5.2• Open cmd on working dir • Set python 3 path to take priority :set PATH=<python 3 path>;%PATH%• Run python - - version and make sure python version is right. • Run our hello: fails …. Fix it:

• In python 3 print is no longer statement but function , many more differences…• See more : http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html

Page 8: PyCourse - Self driving python course

PackagingPython packing is pretty messy with a lot of ambiguity, today in python community the two leading ones are:• Easy_install – 2004, python first significant attempt to supply packaging mechanism • Pip – 2008, alternative to easy_install and the common one as of today.For more details : https://packaging.python.org/pip_easy_install/ There are two main aspect to packaging:• I want to use packages in my project• I want to package my project for others – out of this course scope. Lets install a package, virtualenv :• In command line, working dir (we are still in python 3) type: pip install virtualenv

Page 9: PyCourse - Self driving python course

Our python root

Being familiar with your python root structure is very important, many times debug will take you there. Lets look for our virtualenv package install:• Go to python home, reminder where python will show you home. • Under Lib you will find the packages and models (later on difference)

that comes with install. You will also find a folder called site-packages - our freshly installed virtualenv package is there.

• Any packages we install will go there as well, which becomes pretty messy after a while as different project requires different packages and versions.

Page 10: PyCourse - Self driving python course

Virtual environmentsIn order to allow developers easily develop between versions (many times you need to work on many different version on the same machine) we have virtualenv package. More details : http://docs.python-guide.org/en/latest/dev/virtualenvs/This package allows you to create an isolated python environments, each with its own dependencies and navigate between them. Lets create virtural environment:• In our work dir type in console :virtualenv pycourse • This creates virtual environment with the name pycourse, and installs base python +

setuptools into it, notice virtual env folder was created under sub dir pycourse

• Activate the virtualenv , in cmd type : .\pycourse\Scripts\activate

• And now deactivate it , in cmd type : .\pycourse\Scripts\deactivate

Page 11: PyCourse - Self driving python course

Module and packages• Python has two main mechanisms for managing code blocks: modules and packages.

Both are almost the same conceptually, packages allows you to create more complex file structures and name spaces:• Module – single .py file • Package – directory with __init__.py in it, can conatain sub packages.Lets create an hello module and run it : create a file called hello_mod.py with the code, print ("hello pycourse module”) Create a subfolder called hello_pack, inside it create a file called __init__.py with the

code, print ("hello pycourse package ”) create a file called main.py with the following : Run main.py : Read more on the matter http://knowpapa.com/modpaclib-py/

Page 12: PyCourse - Self driving python course

FunctionLets make a function in hello mod:• Python uses indentation as block, use 4 spaces for every block. Indentation rules can be

found here but keeping in mind 4 spaces in what usually is {}:• Using Ide usually takes care of that for you easily. • http://www.peachpit.com/articles/article.aspx?p=1312792&seqNum=3

Add function to hello_mod.py so it look like:Lets run it, this time in console: run python in cmd, this will get you into interactive interpreter mode. Now you can code directly into the interpreter. Import the module and call the function: ...

Page 13: PyCourse - Self driving python course

Time to IDE Once we go into more coding IDE becomes extremely usefull, There are many IDEs out there, we will be using JetBrains Pycharm, install it (download or take from course distribution).Lets create a project with our code: • File Create new project • Put location of your work dir, choose interpreter of your python 3.5.2 install.• • Pycharm will warn on existing code, confirm it – it will create project with your code. • Run main on ide, now run menu is empty :• Right click main.py and click run main: • And now run menu is active, with main as default run config:• Put a breakpoint in the hello function and click debug :• Debug button :

Page 14: PyCourse - Self driving python course

ClassesSo we have an IDE working, pycharm is a great distribution of eclipse & pydev, so if you are coming from JAVA you might feel at home pretty fast.Python have two type of classes : classic and new style, always use the new style, notice the pass keyword:

More on pass: https://www.tutorialspoint.com/python/python_pass_statement.htmMore on class stype differences:http://stackoverflow.com/questions/54867/what-is-the-difference-between-old-style-and-new-style-classes-in-pythonCreate an hello class inside hello_mod, new style as follows: notice the self reference, it’s the python equivalent of thisRun main in pycharm and see in output console the result

CLASSIC

NEW

Page 15: PyCourse - Self driving python course

Wrapping point – short exerciseBuild an image class rotator, Using the pillow package. Do the following:• Install the Pillow package • Build a class rotator with the following methods:• Load – load an image to the rotator, parameter :im_path • Rotate – rotate the image , parameter :rotation • save– saves the currently rotated image, parameter :im_path • Run the following code using your class and lena.jpg (inside project package, or

choose image of your choice) to get rotated images:

• Take the time to understand the code, first time we see loop and string formatting

Page 16: PyCourse - Self driving python course

PEPs Python is developed and progress using PEPs, Python Enhancements ProposalThis is how community decides on language future and features, PEP0 contains the full list: https://www.python.org/dev/peps/As you are becoming python programmer, let go over PEP8:• Authored by Guido, contains the “right syntax style” to python.• Stick to PEP8 • PyCharm actually mark you where you got it wrong:• Two violations here, import not on top, missing two blank lines. • Nice feature : PyCharm contains auto fix under CodeReformat code

Read PEP20, we will get back to it later - https://www.python.org/dev/peps/pep-0020/

Page 17: PyCourse - Self driving python course

Time to revisit our project folderSo while we are inside our nice and cozy IDE stuff is happening under the hood. Important to stay in touch in the underlying project folder(especially if you manage joint development and source control):• So what do we have there now ?• .idea folder – this is where pycharm stores it magic • __pycache__ - this is where python caches the complied modules, read more here http://

stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files • Pycourse – the virtual env folder we used – Can you find PIL in there ?• Our modules and package• And a bunch of lena rotated images. • Clean your work folder be fore we continue.

Page 18: PyCourse - Self driving python course

Google python classes

A great collection of python function you need to code, gradually covering different python aspects while allowing you to test your code.It contains some tutorials, Videos and exercises – we will focus on the exercises:• More details @ https://developers.google.com/edu/pythonThe exercises have two parts:• Basic, python modules that you need to “solve”• Advanced, 3 small problems you need to code.In the class we will focus on the basic, take as HW the advanced ones.

Page 19: PyCourse - Self driving python course

Setting our environment • Create a new python project• Add the basic subfolder into it • Run string1.py, you have a bug fix it • Notice the print formatting in the files is different then the one we have seen so far. You can

use both.• The flow for each file:

• Module is running main(), what is the difference from what we did so far (calling main in the module)?

• Main calls test function with different inputs • Each test is calling your function implementation, marking pass(OK)/fail (X) on console.• You need to make it all pass , fail example : • Lets start

Page 20: PyCourse - Self driving python course

String1.py • You need to implement 4 strings manipulation functions:

• Each marked with letter (A,B,C,D).• Each has instructions what is needed • Each has a function signature

Solve all 4 functions, tips:• Each module comes with reading page, read it. • You can run it as many times as you want, work iteratively • Google is your friend • Use python console for fast experimenting • Use debugger and break points and inspection. • Combine both debugger and interactive –

Page 21: PyCourse - Self driving python course

Lets complete the rest

Complete rest of the modules, with the following order:• String2• List1, List2 • Wordcount • If you got the time then go for mimic as well

Keep in mind every module has details in the google classes , strings details for example :https://developers.google.com/edu/python/strings

Page 22: PyCourse - Self driving python course

Recommendation for your next steps - Recommended order • Complete the advanced google execrcises• Cover these shortly :docstrings, lambda functions, inheritance, getters and setter, decorators. • Package your code using setuptools. • Repeat the above using wheels. • Learn about generators and iterators. • Download and install miniconda package manager, install tensor flow using conda.• Write multi-thread python program and see how fast it is:

• Network bounded – web page fetches. • CPU bounded – count till 10^9

• Repeat the above using one of python multithread libs : twisted, gevent, async• Learn how to use PYTHONPATH, write a pth file • Install pip that requires build during install (like opencv)• Write extension for python using c • Use c extention from python

Page 23: PyCourse - Self driving python course

Just before we go – PEP20

The zen of python, use it as a mantra to an endless journeyLets go over them.

Page 24: PyCourse - Self driving python course

1.Beautiful is better than ugly.2.Explicit is better than implicit.3.Simple is better than complex.4.Complex is better than complicated.5.Flat is better than nested.6.Sparse is better than dense.7.Readability counts.8.Special cases aren't special enough to break the rules.9.Although practicality beats purity.10.Errors should never pass silently.11.Unless explicitly silenced.12.In the face of ambiguity, refuse the temptation to guess.13.There should be one-- and preferably only one --obvious way to do it.14.Although that way may not be obvious at first unless you're Dutch.15.Now is better than never.16.Although never is often better than *right* now.17.If the implementation is hard to explain, it's a bad idea.18.If the implementation is easy to explain, it may be a good idea.19.Namespaces are one honking great idea -- let's do more of those!