Python and PluralSight

The Python Developers Toolkit.

Why have a Python Toolkit

  • Python has defacto standatds
  • Python is an eco systeem and not just a language

Dependancy Management - [PIP]

How to import a package for use

import PACKAGE

Package Locations
import sys
sys.path
  • Lists the locations and order searched for packages
  • First item is ./
  • site-packages location is 3rd party and system wide package location
Python Packaging Authority
  • PPA advises best current method for package management because the ecosystem is a disaster.
  • Use setuptools
  • Use PIP
Getting PIP
>= Python 3.4
Includes PIP with the standard install
< Python 3.4
https://www.pip-installer.org
Using PIP
pip install <PACKAGE>
pip install <PACKAGE>==1.0
pip install '<PACKAGE>>=1.0'
pip list
pip show <PACKAGE>
pip freeze
pip freeze > requirements.txt
pip install -r requirements.txt

Environmnet Isolation - [virtualenv]

  • Don't install global packages.
  • Isolate project package dependancies
  • Keep environments together and separate from projects.
  • Common location is a .virtualenv/ directory
pip install virtualenv
.virtualenv/> virtualenv <PROJECT_NAME>
.virtualenv/PROJECT_NAME/bin/activate
# Command injected into path
deactive
Helper virtualenvwrapper
pip install virtualenvwrapper
pip install virtualenvwrapper-win
workon
workon <PROJECT_NAME>
# Create new project
mkproject <PROJECT_NAME> # Fails in windows.
# Env for current location
mkvirtualenv
# Assign environmnet for linux/mac
setvirtualenv <PROJECT_NAME>
# Assign environmnet for win
setprojectdir <PROJECT_NAME>
# Create tmp environment (somewhere to test pip installs)
mktmpenv

Code Quality - [pylint]

PEP
Python Enhancement Proposal
PEP8
Style Guide
  • Includes Layout, whitespace, comments, naming…
  • There is a full PEP for docstring contents.

Pylint :

  • is a static code checker.
  • in pep8 compliant
  • includes error detection
  • is configurable
  • intergrates with ides.
Running
  • GUI (py) doesn't work in windows.
  • init.py">Indicates a python folder
  • Pylint only searches python folder for called modules/packages
pylint <PACKAGE/MODULE>
Configuration
pylint --generate-rcfile > pylintrc
Disable Syntax
# pylint: disable=unused-arguments

Debugging - [Python Debugger]

PDB
The python debugger.
import pdb
pdb.set_trace()
l - location
n - next, step over
s - step into
w - where in stacktrace
c - continue
b[(file:lineno|function)] - break, add breakpoint
h (x) - help on (x)
r - return out of this level

Code Documentation - [Sphinx]

PEP 257 :: Docstring semantics and conventions.
dostring
  • first statement of a module, class, function
  • maps to the ~__doc__` variable
  • start/end with ~'''~
  • They are full punctuated senteces.
  • include return type, since only the variable name is returned automatically
Sphinx :: The Python documentation Generator
  • is the defacto standard
  • used by the core python team
  • input is reStructured text
  • allows output to multiple formats; pdf, html…

Packaging and Distribution

setuptools :: ./setup.py

from setuptools import setup
setup ( ... )
__author__ = 'sre'
Setup function
doc
this modules docstring
include_package_data=True
Uses MANIFEST.in for assets
install_requries
required packages for execution
Building Package
python setup.py sdist
sdist
source distribution, basically a zip
bdist_wheel
Binary wheel distrubution, (to include c)
wheel
python packaging standard
requirement
pip install wheel
(no term)
becoming defacto standard
Deploying Package Locally
pip install <FILE_PATH>
Deploying Package Publically
python setup.py register
python setup.py sdist upload
Distribution of executables.
  • Use setup(entry_points...)
    setup(
        # ...
        entrypoints={
    	'console_scripts':[
    	    'CREATED_FILENAME':'MODULE.CLASS.MAIN'
    	]
        }
        # ...
    )
    
  • Use Tools
    1. Py2exe :: Windows
    2. py2app :: mac
    3. PyInstaller :: generic

Python - Beyond the Basics.

Core Python: Organizing Larger Programs.

Unit Testing with Python.

Advanced Python.

Python Best Practices for Code Quality

Play By Play With Zed Shaw (break)

Introduction to the Flask Microframework

Functional Programming in Python

Full Stack Web Development with Python.

What's new in Python 3.7

Getting Started with the python standard library

WQirking With Data and Time in Python.

Building a REST API Using Python and Flask

Getting started in Python and SQL Server

Everything else