Programming Python (DONE)
Note: Based on Programming Python on Udacity
Table of Contents
Chapter 1 - Programming Python
Install Python on OSX
- Download Python 2 of 3, which includes:
- Python language interpreter
- IDLE IDE
-
pip - Python package management system
pip --help
- Note: pip and easyinstall download from Python Package Index Server
- Download latest version of Python (i.e. 3.6.0) from https://www.python.org/downloads/
- Note that the version will be created in: /Library/Frameworks/Python.framework/Versions/3.6
- Use virtualenv to create a virtual Python environment using this version
v.mk --python=python3.6 python_test_env_3.6.0
- Download Tcl/Tk dependency (ActiveTcl) for tkinter on MacOSX for IDLE IDE via this reference
- ActiveTcl Community Edition for MacOSX
- Note: Tk is toolkit to build standard cross-platform GUIs for Tcl language
- Note: tkinter is dependent on Tcl/Tk (ActiveTcl)
- Show where installed and version
- Run Tk Widget Demo based on Tcl/Tk code from Wish GUI
Note: IGNORE THIS Alternatively install via Homebrew brew install python
(BUT pyenv
appears to be better than brew
https://gist.github.com/Bouke/11261620)
- Setup development environment
- Create GitHub repo (i.e. PythonTest)
Packages Installed
- Output packages installed with
pip freeze
, i.e.
Product
- Brainstorm to create time tracking app
- Create program tracks time
- Loops
- Wait x hours
- Then open browser
- Share code with friend to truly understand
Using IDLE IDE
- Run IDLE IDE
- CMD+SPACE (Spotlight)
- Type “IDLE”
- New - Go to Menu: File > New File
- Save - Go to Menu: File > Save
- Run - Go to Menu: Run > Run Module
- Stop - CTRL+C in Console Window
>>>
- Autocompletion
- Start typing
- Press CTRL+SPACE
- Press up/down to desired method
- Press SPACE to execute
Debugging IDLE
- Debugging
- CTRL+C in IDLE terminal to terminate any running app
- Click in the Python 2.7.12 Shell window (not a code file)
- Debug - Go to Menu: Debug > Debugger
- Run - Go to Menu: Run (or FN + F5)
- Go To Line Number: CMD+J
IntelliJ with Python Plugin and source level debugging setup
-
After fresh installation of IntelliJ IDEA 2016.2.5 where Python 2.7.12 already installed
- Check Python Plugin installed (i.e. Python Plugin Version: 2016.2.162.43)
- IntelliJ IDEA > Preferences > Plugins
- Add Python as recognized file type
- Preferences > Editor > File Types (shortcut CMD+,)
- Click “+” to add a new “Recognized File Type”
- Add name value of: Python
- Add line comment value of: #
- Add block comment start: “””
- Add block comment end: “””
- Select all checkboxes (i.e. Support paired …, Support string escapes)
- Click “1” under “Keywords” and click “+” to add for syntax highlight colour 1: %, from, import
- Click “2” under “Keywords” and click “+” to add for syntax highlight colour 2: def
- Click “3” under “Keywords” and click “+” to add for syntax highlight colour 3: for, if, while, yield, next
- Click “4” under “Keywords” and click “+” to add for syntax highlight colour 4: print
- Click “OK” to save
- Add Python file types
- Preferences > Editor > File Types (shortcut CMD+,)
- Click “+” under “Registered Patterns” and enter: *.py
- Click “OK”
-
Check no Python files that you need to edit are listed under “Ignore files and folders”
-
Restart IntelliJ IDEA
- Go back to check recognized file types where suddely “Python” (with *.pjw) and “Python Stub” (with *.pyi) are shown (were previous efforts were in vain???)
- Preferences > Editor > File Types (shortcut CMD+,)
- Click “OK”
- Add Python Interpreter
http://stackoverflow.com/questions/24769117/how-do-i-configure-a-python-interpreter-in-intellij-idea-with-the-pycharm-plugin
- File > Project Structure > Platform Settings > SDKs
- Click “+” and select Python SDK > Local > enter directory where Python installed (check in terminal with
which python
). Note that this added “Python 2.7.12” for me - File > Project Structure > Project Settings > Project > Project SDK > Select “Python 2.7.12”
- Click “OK”
- Configure Python
- Preferences > Languages & Frameworks > Python Template Languages (i.e. Django)
- Preferences > Languages & Frameworks > BDD (i.e. Behave)
- Preferences > Tools > Python External Documentation
- Preferences > Tools > Python Integrated Tools > Default Test Runner (i.e. unittest, py.test)
- Configure Python debug configuration for source level debugging
- Preferences > Build, Execution, Deployment > Debugger > Python Debugger
-
Preferences > Build, Execution, Deployment > Console > Python Console > Click “Always show debug console”
- Run > Edit Configuration > Default > Python Tests > py.test
Add “Target” of project directory Check “Options” and add: -v –color=yes –exitfirst –showlocals –durations=5
- Run > Edit Configuration > Click “+”
select “Python Tests > py.test” > Rename to “Python tests custom” > Change “Target” to project directory
- Note: “Python Interpreter” should now be “Use specified interpreter” with value “Python 2.7.12 …”
-
Click “OK”
- Run “Python tests custom”
-
Problem: No breakpoints appear when I click in the sidebar in Python app!!! (however using a Ruby app breakpoints work!!)
- Attempt to fix #1: File > Invalidate Caches and Restart - DID NOT WORK
- Attempt to fix #2: Uninstalled the Python Plugin Version: 2016.2.162.43 and reinstalled same version - DID NOT WORK
- Solution Attempt to fix #2: Tried adding wildcard “.py” as a Registered Pattern but a popup appears “This wildcard is already registered by ‘Python’ filetype” with options Cancel or Reassign Wildcard (even though the only pattern listed is “.pyw” and none of the other registered filetypes use that wildcard). Clicking “Reassign Wildcard” allowed me to add “*.py” to the list. WORKED !!!!!
Python Configuration Information
- System Path
-
Interactive Terminal
Change Python Version (i.e. from 2.7 to 3.5) - Use virtualenv successfully instead
Package Versioning
- http://stackoverflow.com/questions/458550/standard-way-to-embed-version-into-python-package
Python Dependency Management
- Reference with excellent links
- Note: Python comes with primitive package management tool
easy_install
(i.e.easy_install pip
) - Python library dependencies are recommended to be installed using
pip
when a virtualenv is created - Pip downloads and installs app dependencies from central PyPi repo (similar to RubyGems or NPM)
- Virtualenv instance stores a copy of Python interpreter and dependencies in the global site-packages directory (i.e. /Library/Python/2.7/site-packages/) to isolate an apps dependencies from system-level packages
- Virtualenvwrapper Stores all Virtualenv’s in a single directory on the file system
- requirements.txt specifies an apps pegged (precise versions or Git tags) dependencies
- setup.py used to specify dependencies for Reusable Components (that will be imported as dependencies by other projects)
- Anaconda http://stackoverflow.com/questions/38217545/the-different-between-pyenv-virtualenv-anaconda-in-python
-
Python Docker Images (to switch between Python environments)
- List currently installed dependencies with
pip freeze
-
Create a requirements.txt file in the root directory of the app and past the output of
pip freeze
- Reference Links
- https://www.fullstackpython.com/application-dependencies.html
- http://jonathanchu.is/posts/virtualenv-and-pip-basics/
- Example Virtualenv and Virtualenvwrapper using Yolk for isolated environments using various Python Interpreters (i.e. v2.7 or v3.5)
- Cont’d…
- Install latest Virtualenvwrapper https://virtualenvwrapper.readthedocs.io/en/latest/
- Cont’d…
- Update ~/.bash_profile with directory (i.e. ~/.virtualenvs) where we will store all virtualenv’s
- Cont’d…
- Add the following to Base Profile: Create aliases to save keystrokes http://blog.doughellmann.com/2010/01/virtualenvwrapper-tips-and-tricks.html
- Cont’d…
- Show current location of Python Interpreter before creating a virtual environment
- Cont’d…
- Create new completely clean virtual environment (notice how command prompt changes at the end) with a specific Python version
- Cont’d…
- Show that the new virtual env contains its own copy of a specific Python Interpreter:
- Cont’d…
- Show where the new python_test_env has been created:
- Cont’d…
- Deactivate using python_test_env and then show globally installed packages
- Cont’d…
- Restart python_test_env again
- Cont’d…
- Install Python page Yolk, a utility to list packages installed for environment, and then list them, and show locally installed packages
- Cont’d…
- Install requirements i.e.
pip install x y z
- Install requirements i.e.
- Cont’d…
- Send a snapshot of all the requirements and current versions into the requirements.txt file
- Cont’d…
- Install the dependencies when cloned in another environment
$ pip install -r requirements.txt
- Cont’d…
- Remove virtual env
- Cont’d…
- Change to a different virtual env
v.switch python_test_env3.5.2
- Show current Python version
- Change to a different virtual env
- Cont’d…
- Create a virtual environement with a specific Python Interpreter (i.e. 3.5) version
- Cont’d…
- Install dependencies from requirements.txt
- Cont’d…
-
Try running a program to see if it breaks using the newer version of Python
-
Switch to a different virtual env
-
Pipenv
-
https://github.com/kennethreitz/pipenv
- Use suite such as:
- pylint & PEP 8 - Style Guide
- mypy & PEP 484 - Type Hints
- pipenv & PEP 508 - Dependency spec
- pytest - Tests
-
Install pipenv, initialise in python3 mode, start pipenv shell, install project dependencies, run tests
pip install pipenv pipenv --three pipenv shell -c pipenv install pytest
Mypy
-
Create stubs
-
https://github.com/python/mypy/wiki/Creating-Stubs-For-Python-Modules
-
i.e. mkdir stubs, then run
export MYPYPATH=/Users/<your_username>/code/aind-projects/clones/nd889/2_isolation/stubs
then Copy and paste the timeit.pyi stub posted on the typeshed GitHub repo here into the stubs directory to remove Mypy errorisolation/isolation.py:11: error: No library stub file for standard library module 'timeit'
-
Convert Python 2 to Python 3
- Use 2to3 utility on all files http://stackoverflow.com/questions/37891188/convert-python-2-code-to-3-in-pycharm
2to3 myfile.py -w
- Correct fixes for exceptions manually
Renaming Python executables and Symlinks
-
rename pip to pip2 using brace expansion
cd /usr/local/bin && mv /usr/local/bin/{pip,pip2}
-
created symlink for
pip
command to points atpip3
soln -s /usr/local/bin/pip3 /usr/local/bin/pip
Jupyter Notebook
If get errors in Jupyter Notebook when running code snippets like:
ModuleNotFoundError: No module named 'numpy'
Then note that Jupyter Notebook uses Python 3 by default, and the issue can be resolved by
installing the packages using pip3 install numpy matplotlib
(pointing to Python 3.6)
instead of just pip ...
(which pointed to Python 2.7) and then opening Jupiter Notebook and performing Run All.
pip3 list
shows which packages are installed.
Confirm which installation directory of Python 3.6 that Jupyter Notebook was using by running the following
commands within the code snippets in Jupyter Notebook:
print("{}".format(sys.version))
# show Python 2.7 executable directory
!which pip
# show Python 3.6 executable directory
!which pip3
# show jupyter config paths http://jupyter.readthedocs.io/en/latest/projects/jupyter-directories.html
!jupyter --paths
Which returned the following output:
3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
/Users/<my_username>/miniconda3/envs/aind/bin/pip
/Library/Frameworks/Python.framework/Versions/3.6/bin/pip3
config:
/Users/<my_username>/.jupyter
/Library/Frameworks/Python.framework/Versions/3.6/etc/jupyter
/usr/local/etc/jupyter
/etc/jupyter
data:
/Users/<my_username>/Library/Jupyter
/Library/Frameworks/Python.framework/Versions/3.6/share/jupyter
/usr/local/share/jupyter
/usr/share/jupyter
runtime:
/Users/<my_username>/Library/Jupyter/runtime
Notice how pip3
is in a subdirectory of /Library/Frameworks/Python.framework/Versions/3.6/
, whereas pip
was not
Pip Autcompletion
- Run the following to setup and enable, then use with
pip i[PRESS_TAB]
Python Standard Library
- Python Standard Library
- v2
- v3
- Abstractions hide details of libraries
webbrowser
linktime
link- CGI scripts
Python 3rd Party Library
- Twilio - Send SMS
- Cont’d
Google Style Guide for Python
Environment Variables
-
Alternatively add your credentials to your shell environment
Python Packages by Ranking
Python Standard Library Built-In Functions
- Built-in functions i.e.
open()
Iterable Data Sets
itertools
https://docs.python.org/3/library/itertools.html
Importing Files in Separate Directories
- Modules and Packages
- Note
from x import y
means it binds y pointing to module attribute x.modules[‘x’].y) but the rest of module x is still imported. there is not performance difference to just usingimport
, it’s just a more granular code style making it cleared what is specifically being imported- i.e.
from twilio.rest import TwilioRestClient
means in twilio there is a folder called rest, and inside that folder is a class called TwilioRestClient (defined in init.py). Use the class withTwilioRestClient(...)
. - Alternatively use
from twilio import rest
and use the class withrest.TwilioRestClient(...)
to call the classes init() function and create an instance
- i.e.
Python Path
- Add directory to $PYTHONPATH
Python Functional Programming (similar to Lodash)
- Example:
Python Frameworks
- Pyramid
- Flask
- Django
- Web2Py
Python Production
- AppHosted
- djangozoom
- Djangy
- ep.io
- Gondor.io
- Google App Engine
- Heroku
- Joyent
- Pydra
- stable.io
Python Package System (similar to RVM/RBEnv)
Python Versioning
- Show current version being used
python -V
- Indicate that a file only supports certain Python versions
import sys assert sys.version_info >= (3,5,2)
Python Unit Testing
- UnitTest
- Standard module testing framework (aka pyUnit similar to xnit)
- PyTest
- py.test lightweight unit test framework without boilerplate of unittest
- Test discovery requires all test filenames prefixed
test_
- PyTest Fixtures i.e. before_each, load sample data, etc
- Cont’d
- Nose
- Task runner that runs functions in modules whose name starts with ‘test’
- Test generators implement data-driven tests
- Runs unittest tests
- Coverage plugin available
- Google App Engine plugin
- Nose2
-
DocTest
- Other
- TwistedTrial extension of unittest
Continuous Integration with Travis CI
- Enable Travis CI to use GitHub app - https://travis-ci.org/auth
- Check build status of GitHub app - https://travis-ci.org/ltfschoen/PythonTest
- Create .travis.yml file - https://docs.travis-ci.com/user/getting-started/
- Customise .travis.yml file - https://docs.travis-ci.com/user/customizing-the-build/
- View other example .travis.yml files - https://docs.travis-ci.com/user/languages/python/#Examples
PyPy
- Alternative to Python
Python Syntax
- Inputs
input
evaluates expression upon entryraw_input
does not evaluate immediately
- Strings
- Join
''.join(['/', samples_folder_name, '/'])
- Join
- Booleans (first letter capitalised)
True
,False
- Print Output
print("Before - Index: %s, Filename: %s") % (index, filename)
- Control Flow
- Array
- Iterate array by index from specific starting index
- Examples http://stackoverflow.com/questions/1514553/how-to-declare-an-array-in-python
- Class
- Define template/blueprint (class) from which copies (instances/objects) may be made as types of it
- Calling the init() function to create space in memory for new instance of the class that can access all class methods
- Class variables are those with values common to all instances
- Pre-existing class variables include doc so you can create documentation for a class within
"""blah"""
https://discussions.udacity.com/t/exploring-built-in-functions-f/16116/2715
- Hash
- Dictionary using hash tables
- Random Numbers
str(randint(0,99))
random integer between 0 and 99 inclusive
- Random Strings
- Modules
- File with Python definitions and syntax
- Filename is
.py __name__
is available in Module as module name
- File Handling
-
Regular Expressions
- Replace after last instance of character being queried in string
Python Test Coverage
Python Developers Slack Forum
Udacity Forum
TODO
- Behave - https://pypi.python.org/pypi/behave
- Tox - https://tox.readthedocs.io/en/latest/
- Jenkins and Python - https://wiki.jenkins-ci.org/display/JENKINS/Python+Projects
- PyAtom - https://github.com/pyatom/pyatom
- Random - http://mathieu.agopian.info/presentations/2015_06_djangocon_europe/?full#test-files