Difference between revisions of "Python Resources"

From Simson Garfinkel
Jump to navigationJump to search
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
==For Learning Python==
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:
 
* Start with the [https://docs.python.org/3/ Official Python Documentation], including the tutorial
==Learning to use Python==
* [http://video.google.com/videoplay?docid=7760178035196894549 Advanced Python or Understanding Python], Thomas Wouters, Google Tech Talks, February 21, 2007
If you want to learn to use python, here are the key skills that you need to know:
* [http://diveintopython3.org/ Dive Into Python 3] (book, online for free)
 
* 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.
 
===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:
* https://www.learnpython.org, powered by DataCamp. For example, look at the [https://www.learnpython.org/en/String_Formatting String Formatting] lesson
* [https://www.w3schools.com/python/default.asp  W3School's Python Tutorial]
 
I also recommend:
* [https://www.diveinto.org/python3/ Dive Into Python 3] (book, online for free; a little dated.)
* [https://wiki.python.org/moin/BeginnersGuide/NonProgrammers Python for Non-Programmers]
 
===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:
* [https://docs.python.org/3/tutorial/index.html Python 3 Tutorial]
 
I then recommend reading the documentation:
* [https://docs.python.org/3/ Official Python Documentation]. Just read the documentation. Honestly. It's well-written.
* [http://book.pythontips.com/en/latest/index.html Python Tips] (book, online for free)
* [http://book.pythontips.com/en/latest/index.html Python Tips] (book, online for free)
* [https://wiki.python.org/moin/BeginnersGuide/NonProgrammers Python for Non-Programmers]
* [https://github.com/google/styleguide/blob/gh-pages/pyguide.md Python Google Style Guide].
* https://www.learnpython.org, powered by DataCamp. For example, look at the [https://www.learnpython.org/en/String_Formatting String Formatting] lesson
 
==Using Python==
Recommended packages and tools for different tasks
 
===Developer Tools===
* [https://www.jetbrains.com/pycharm/ PyCharm], free for educational use (if you have a .edu email address)
* Jupyter Notebook (comes with Anaconda; also available https://jupyter.org/ on the web!)
 
===Financial Modeling===
Nice blog entries:
* [https://pbpython.com/amortization-model.html Building a Financial Model with Pandas], looks at mortgage calculator
 
Some books:
* [https://www.amazon.com/Python-Finance-Analyze-Financial-Data/dp/1491945281 Python for Finance: Analyzing Big Financial Data] (O'Reilly)
* [https://www.amazon.com/Financial-Modelling-Python-Shayne-Fletcher/dp/0470987847 Financial Modelling in Python] (Wiley Finance)


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


===Date Data===
===Date Data===
* [http://labix.org/python-dateutil dateutil]  
For working with dates, see:
* [http://codespeak.net/icalendar/ iCalendar] package for Python
* [http://labix.org/python-dateutil dateutil], included in the Anaconda distribution
* [http://codespeak.net/icalendar/ iCalendar], package for Python, generates calendears
 
===Image Manipulation===
* Use Pillow, the Python2/3 fork of Python Image Library (PIL). 
 
Fun examples:
* http://jeremykun.com/2012/01/01/random-psychedelic-art/
* http://pythonvision.org/basic-tutorial
* http://stackoverflow.com/questions/138250/read-the-rgb-value-of-a-given-pixel-in-python-programatically


===String Formatting===
=== Text Processing ===
* https://pyformat.info
See [[Text processing notes]].


==Graphing, Graphics and Games==
===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]])
* geopy - geocoding
* holoview - a more modern but less featured system.
* [http://pyx.sourceforge.net/ PyX] - A Python PS and PDF graphing package
* [https://altair-viz.github.io/index.html Altair: Declarative Visualization in Python]
* [https://github.com/geopy/geopy geopy] - geocoding for Python
* Seaborn. (See [https://www.datacamp.com/community/tutorials/seaborn-python-tutorial Datacamp Tutorial])


===Easy Graphics===
===Easy Graphics===
Line 33: Line 87:
* 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


===Games===
===Database Access===
* http://www.pygame.org/ pygame - a Python game development system
Python contains built-in support for SQLite3.
* [http://peak.telecommunity.com/DevCenter/EasyInstall PEAK Easy Install]
 
* [http://www.crystalspace3d.org/ Crystal Space 3D]
For using MySQL, you'll need a connector. There are many available.
* [http://www.vpython.org/ VPython 3D Programming in Python]


===Image Manipulation===
Install MySQL connector on anaconda:
* Use Pillow, the Python2/3 fork of Python Image Library (PIL)
    conda install mysql-connector-python
 
Then you can:
    import mysql.connector as mysql
    c = mysql.connect(host=host,database=db,user=user,password=password)


Fun examples:
I generally prefer PyMySQL, as it's pure-python and has fewer dependencies.  
* http://jeremykun.com/2012/01/01/random-psychedelic-art/
* http://pythonvision.org/basic-tutorial
* http://stackoverflow.com/questions/138250/read-the-rgb-value-of-a-given-pixel-in-python-programatically


===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).


* tkinter - built in, but not recommended
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)
* wxwidgets - It's okay, but use PyQT5
* [http://en.wikibooks.org/wiki/Python_Programming/GUI_Programming wikibooks] has a page on Python Program/GUI Programming options
* [http://www.fltk.org/ FLTK], the Fast Light Toolkit
* [http://www.fltk.org/ FLTK], the Fast Light Toolkit


===Resources for  Tkinter===
However, I strongly recommend writing your python program as a web-based app (see next section)
* [http://my.safaribooksonline.com/book/programming/python/0130260363/advanced-topics/ch18#X2ludGVybmFsX0ZsYXNoUmVhZGVyP3htbGlkPTAtMTMtMDI2MDM2LTMvNDA5 Chapter 18 of Core Python Programming] discusses GUI Programming with Tkinter
* [http://docstore.mik.ua/orelly/other/python2/ Programming Python, 2nd Edition, Chapter 7 and 8]
* [http://www.reddit.com/r/Python/comments/i9rzj/are_there_any_good_python_3x_books_for_tkinter/ reddit has a page on Python 3.x books for Tkinter]
* [http://effbot.org/tkinterbook/ Introduction to Tkinter] on effbot.org
* [http://gaezipserver.appspot.com/python/manual/python-3.2-docs/library/tkinter.html Python 3.2 tkinter doc]
* [http://www.ferg.org/thinking_in_tkinter/index.html Thinking in Tkinter]
* [http://wiki.python.org/moin/GuiProgramming Python wiki 'GUI Programming in Python']
* [http://docs.python.org/library/tkinter.html Python 2.7 Tkinter Doc]
* [http://www.pythonware.com/library/tkinter/introduction/ Original 'An introduced to Tkinter'] by Fredrik Lundh


==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)
 
* Some sort of [https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface Web Server Gateway Interface] (WSGI)
* 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:
* See also https://wiki.python.org/moin/WebFrameworks
* See also https://wiki.python.org/moin/WebFrameworks
Examples and resources:
* https://github.com/MichaelPereira/webhooks-demo-application
* https://github.com/MichaelPereira/webhooks-demo-application
* https://mattcarrier.com/flask-dreamhost-setup/
* https://mattcarrier.com/flask-dreamhost-setup/
* https://www.byteconsole.com/get-flask-up-and-running-on-a-dreamhost-server-with-passenger/


===WSGI options===
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...)
* Flask (can run on Apache with Passenger, but needs its own domain name)
 
* CherryPy
===Running WSGI on Dreamhost===
To reload your program on Dreamhost with passenger:


===Notes on Flask===
     $ touch tmp/restart.txt        # reload your program
     $ touch tmp/restart.txt        # reload your program


===Using Passengers on Dreamhost===
To reload your program with mod_wsgi:
 
    $ touch wsgi_app.py
 
===Using Passengers on Dreamhost:===
* https://www.byteconsole.com/get-flask-up-and-running-on-a-dreamhost-server-with-passenger/
* https://help.dreamhost.com/hc/en-us/articles/215769578-Passenger-overview
* https://help.dreamhost.com/hc/en-us/articles/215769578-Passenger-overview
* https://help.dreamhost.com/hc/en-us/articles/216137717-Python-overview
* https://help.dreamhost.com/hc/en-us/articles/216137717-Python-overview
Line 93: Line 144:
* https://help.dreamhost.com/hc/en-us/articles/115000221112-Using-pip-to-install-Python-modules
* https://help.dreamhost.com/hc/en-us/articles/115000221112-Using-pip-to-install-Python-modules


==Python Installation==
===Good Tutorials===
If you are using a Mac, you almost certainly want to ditch Apple's python implementation and instead install your own.
Writing a full application with Apache, Bottle and MongoDB:
* https://reachtim.com/articles/BAM-Short-Stack.html


I now recommend the [https://www.anaconda.com/ Anaconda Python distribution], because it has almost everything that you need, and it's easy to add new modules.
=Advanced Python=
* [https://www.amazon.com/Fluent-Python-Concise-Effective-Programming/dp/1491946008 Fluent Python: Clear, Concise, and Effective Programming] (you can get it on Safari)


If you want to write a Python program with a GUI that can be run on any system, you'll need to use  Tkinter (Python's Tcl/Tk bindings). If you want to actually enjoy what you are writing, you want to use PyQT5..
* [https://www.amazon.com/Test-Driven-Development-Python-Selenium-JavaScript/dp/1491958707/ref=pd_sim_14_6/132-3407075-1922137?_encoding=UTF8 Test Driven Development with Python]


[[Category:Resources]]
[[Category:Resources]]

Latest revision as of 17:17, 2 February 2021

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

  • PyCharm, free for educational use (if you have a .edu email address)
  • Jupyter Notebook (comes with Anaconda; also available https://jupyter.org/ on the web!)

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