Difference between revisions of "Python Resources"

From Simson Garfinkel
Jump to navigationJump to search
m
Line 1: Line 1:
I've reviewed these resources and highly recommend them. I periodically update this page, but it may be out of date.
I've reviewed these resources and highly recommend them. I periodically update this page, but it may be out of date.


==Learning Python==
==Learning to use Python==
You need to be able to run Python programs. You have three options:
If you want to learn to use python, here are the key skills that you need to know:


# Although Python is pre-installed on many computers, you should download and install [https://www.anaconda.com/ the Anaconda Python distribution]. It has all of the packages you need.
* Basic Python syntax, including the role of whitespace, variables, objects, functions and modules.
* Intermediate Python syntax, including lambdas, list comprehensions, generators, and decorators.
* Building unit tests with unittest or (better) pytest
* Python package management with setuptools and pip.
* Editing python programs with a decent editor, such as pycharm, or an interactive IDE such as spyder or Jupyter notebook. You can also use emacs or vim. Don't use Idle or Notepad++ or nano.
 
Do not learn python2.
 
To learn python you must be able to run Python programs. You have two main options:
 
# Although Python is pre-installed on many computers, do not use the pre-installed version, as it is typically out of date. (It's also frequently python2, which is abandoned.) You can instead download and install [https://www.anaconda.com/ the Anaconda Python distribution]. It has all of the packages you need.
# Alternatively, you can learn it entirely using web-based Jupyter notebooks.
# Alternatively, you can learn it entirely using web-based Jupyter notebooks.


Line 42: Line 52:


===Numerical Data Processing===
===Numerical Data Processing===
* numpy - numeric analysis in python (needed for matplotlib)
* numpy and scipy are the standard numerical analysis tools in python.
* pandas or SparkSQL
* pandas is a popular data analysis platform that integrates with numpy. You can make it run on a cluster with Dask.
* Alternatively, you can use Apache Spark and SparkSQL
* tabulate - create tables easily (but see my tytable replacement)
* tabulate - create tables easily (but see my tytable replacement)


===Date Data===
===Date Data===
For working with dates, see:
* [http://labix.org/python-dateutil dateutil], included in the Anaconda distribution
* [http://labix.org/python-dateutil dateutil], included in the Anaconda distribution
* [http://codespeak.net/icalendar/ iCalendar], package for Python, generates calendears
* [http://codespeak.net/icalendar/ iCalendar], package for Python, generates calendears
===String Formatting===
* https://pyformat.info --- Big how-to on using the % and .format() formatting


===Image Manipulation===
===Image Manipulation===
Line 65: Line 74:


===Graphing===
===Graphing===
* [http://matplotlib.org/ matplotlib] - a handy system for plotting in python. ([[Short example of using matplotlib within jupyter]])
* [http://matplotlib.org/ matplotlib] - A well-developed system for plotting 2d and 3d static graphis. (See my [[Short example of using matplotlib within jupyter]])
* holoview
* holoview - a more modern but less featured system.
* [https://altair-viz.github.io/index.html Altair: Declarative Visualization in Python]
* [https://altair-viz.github.io/index.html Altair: Declarative Visualization in Python]
* [https://github.com/geopy/geopy geopy] - geocoding for Python
* [https://github.com/geopy/geopy geopy] - geocoding for Python
Line 76: Line 85:
* http://plotdevice.io/
* http://plotdevice.io/
* http://www.cc.gatech.edu/classes/AY2013/cs1301_summer/presentations/calico_graphics.pdf
* http://www.cc.gatech.edu/classes/AY2013/cs1301_summer/presentations/calico_graphics.pdf
===Writing Games===
* http://www.pygame.org/ pygame - a Python game development system
* [http://peak.telecommunity.com/DevCenter/EasyInstall PEAK Easy Install]
* [http://www.crystalspace3d.org/ Crystal Space 3D]
* [http://www.vpython.org/ VPython 3D Programming in Python]


===Database Access===
===Database Access===
Line 98: Line 101:


===Python GUI Options===
===Python GUI Options===
I need to write some programs that use GUI.  
I recommend writing GUI's using a web browser as a front end and a local web server (see next section).


If you must write a native app, I recommend using:
* pyqt - Use PyQT5 (it's part of Anaconda)
* pyqt - Use PyQT5 (it's part of Anaconda)
* [http://www.fltk.org/ FLTK], the Fast Light Toolkit
* [http://www.fltk.org/ FLTK], the Fast Light Toolkit
Line 106: Line 110:


==Building Web Applications==
==Building Web Applications==
You basically have two options:
You should use a framework that implements Python's [https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface Web Server Gateway Interface (WSGI)] to write web apps. Popular implementations are:
* Write a CGI script (slow, but easy. Don't use for more than 1 request every 10 seconds)
 
* Some sort of [https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface Web Server Gateway Interface] (WSGI). Options include:
* Bottle (easy, and the whole thing runs from a single file)
** Bottle (easy, and the whole thing runs from a single file)
* Flask (can run on Apache with Passenger, but needs its own domain name)
** Flask (can run on Apache with Passenger, but needs its own domain name)
** Writing your own (don't do this.)


The WSGI server needs to be run from a program that receives the Port80 connections. The two ways I've done this are:
Bottle has an built-in web server for development. For high performance you can combine it with CherryPy or use the Apache wsgi module.


* mod_wsgi running within Apache. (This works surprisingly well and allows you to run bottle or flask side-by-side other systems)
* CherryPy (Includes its own web server and will hand off to Bottle or Flas.)


For more information, see:
For more information, see:
Line 125: Line 125:
If you are using anything other CGI, you need some way to tell the web server to reload your python program. (You don't need to do this with CGI---it reloads Python and your program every time you serve another request. That's why it's so slow...)
If you are using anything other CGI, you need some way to tell the web server to reload your python program. (You don't need to do this with CGI---it reloads Python and your program every time you serve another request. That's why it's so slow...)


===Running WSGI on Dreamhost===
To reload your program on Dreamhost with passenger:
To reload your program on Dreamhost with passenger:



Revision as of 07:14, 24 August 2020

I've reviewed these resources and highly recommend them. I periodically update this page, but it may be out of date.

Learning to use Python

If you want to learn to use python, here are the key skills that you need to know:

  • Basic Python syntax, including the role of whitespace, variables, objects, functions and modules.
  • Intermediate Python syntax, including lambdas, list comprehensions, generators, and decorators.
  • Building unit tests with unittest or (better) pytest
  • Python package management with setuptools and pip.
  • Editing python programs with a decent editor, such as pycharm, or an interactive IDE such as spyder or Jupyter notebook. You can also use emacs or vim. Don't use Idle or Notepad++ or nano.

Do not learn python2.

To learn python you must be able to run Python programs. You have two main options:

  1. Although Python is pre-installed on many computers, do not use the pre-installed version, as it is typically out of date. (It's also frequently python2, which is abandoned.) You can instead download and install the Anaconda Python distribution. It has all of the packages you need.
  2. Alternatively, you can learn it entirely using web-based Jupyter notebooks.

If you don't know how to program

If you have never programmed with Python before, you may want to start with DataCamp's Learn python by example:

I also recommend:

If you know how to program

If you know another programming language (e.g. C, C++, FORTRAN, Java, etc.), you can learn Python in about 2 hours.

Start by reading the Python Tutorial:

I then recommend reading the documentation:

Using Python

Recommended packages and tools for different tasks

Developer Tools

Financial Modeling

Nice blog entries:

Some books:

Numerical Data Processing

  • numpy and scipy are the standard numerical analysis tools in python.
  • pandas is a popular data analysis platform that integrates with numpy. You can make it run on a cluster with Dask.
  • Alternatively, you can use Apache Spark and SparkSQL
  • tabulate - create tables easily (but see my tytable replacement)

Date Data

For working with dates, see:

  • dateutil, included in the Anaconda distribution
  • iCalendar, package for Python, generates calendears

Image Manipulation

  • Use Pillow, the Python2/3 fork of Python Image Library (PIL).

Fun examples:

Text Processing

See Text processing notes.

Graphing

Easy Graphics

Database Access

Python contains built-in support for SQLite3.

For using MySQL, you'll need a connector. There are many available.

Install MySQL connector on anaconda:

   conda install mysql-connector-python

Then you can:

   import mysql.connector as mysql
   c = mysql.connect(host=host,database=db,user=user,password=password)

I generally prefer PyMySQL, as it's pure-python and has fewer dependencies.

Python GUI Options

I recommend writing GUI's using a web browser as a front end and a local web server (see next section).

If you must write a native app, I recommend using:

  • pyqt - Use PyQT5 (it's part of Anaconda)
  • FLTK, the Fast Light Toolkit

However, I strongly recommend writing your python program as a web-based app (see next section)

Building Web Applications

You should use a framework that implements Python's Web Server Gateway Interface (WSGI) to write web apps. Popular implementations are:

  • Bottle (easy, and the whole thing runs from a single file)
  • Flask (can run on Apache with Passenger, but needs its own domain name)

Bottle has an built-in web server for development. For high performance you can combine it with CherryPy or use the Apache wsgi module.


For more information, see:

If you are using anything other CGI, you need some way to tell the web server to reload your python program. (You don't need to do this with CGI---it reloads Python and your program every time you serve another request. That's why it's so slow...)

Running WSGI on Dreamhost

To reload your program on Dreamhost with passenger:

   $ touch tmp/restart.txt         # reload your program

To reload your program with mod_wsgi:

   $ touch wsgi_app.py 

Using Passengers on Dreamhost:

Good Tutorials

Writing a full application with Apache, Bottle and MongoDB:

Advanced Python