7. Python

Author:Peter Parente

7.1. Goals

  • Gain the ability to read and write Python code
  • Recognize Python’s strengths as a language
  • Understand how to tap into the Python ecosystem
  • Practice writing Python
  • Practice using pip and virtualenv

7.2. Introduction

Python is language built around clarity, expressiveness, and all around general utility. It is often recommended as a language for beginners, but is equally useful to experienced developers in creating scripts, gluing together large systems, building web sites, and analyzing data.

To get started, watch the Python slidecast (~45 minutes) introducing the Python programming language. The slidecast describes the following language features using short, interactive code samples:

Now Watch the Python Ecosystem slidecast (~45 minutes) which sows how to find and install libraries that can save you time in building more advanced Python applications. The slidecast includes demos of the following:

If time permits, review these additional pages:

7.3. Exercises

You will need to complete the Setting Up instructions before you proceed with these exercises. Once you are set up, SSH into tottbox using the vagrant ssh command from the setup instructions. Then tackle the problems below. Document what you find in a gist and share it with the TotT community later.

7.3.1. Guess a number

Write a Python guessing game that picks a random, secret number between 1 and 100, lets the user take up to 5 guesses, and states if the secret number is equal to, higher, lower than a guess. Read input from stdin and print to stdout.

7.3.2. Run python -m

Run the following command at the prompt.

python -m SimpleHTTPServer

What does it output? What is SimpleHTTPServer? Where does it live? What does the -m command line flag do? Document what you discover.

7.3.3. Compare pip and npm

How are pip and npm similar? Different? What about pip plus virtualenv? Write down your observations.

7.3.4. Resize images

Pillow is an image manipulation library for Python.

Create a virtualenv and use pip to install Pillow. Write a Python script that uses Pillow to resize all of the images in a directory. Let the user specify the name of the input directory, name of the output directory, and desired width or height of the resulting images on the command line. Maintain the aspect ratio of each image processed. (Hint: Look into the optparse or argparse packages for Python to help with command line parsing.)

7.3.5. Bottle it up

Bottle is a web microframework for Python. (It’s a single .py file!)

Use pip to install Bottle into the same virtualenv you created for the image resize utility. Then define a web service API for your image resize utility. Support upload of images in common formats and specification of the desired width or height while maintaining aspect. For example, I might access your completed service using curl like so:

curl -X POST -d @myimage.png http://example.com/resize?width=250 --header "Content-Type:image/png"

7.3.6. Show HackerNews

HackerNews is a social new web site for geeks. The site has a web API for hackers to, well, hack.

Use pip to install the thekarangoel/HackerNewsAPI, a Python library simplifying access to the HN API. Use it to write a command line tool that can print the top N front page items or newly posted items where the user can specify N.

Add more options to your command line tool as it suits your fancy (e.g., show comments for a given news story and pipe to less for navigation.)

7.3.7. Spoon the Web soup

BeautifulSoup makes it easy to parse messy HTML markup. It’s very useful in screen scraping applications.

Find a public web page of interest to you that provides no clean web API (e.g., http://durhamnc.gov/Pages/NNList.aspx). Using BeautifulSoup, scrape the page for notices. Compare what you extract on the current run to the last run, and notify the user of any differences.

Think about how you might make your site checker run on an interval and notify the user of changes unobstrusively.

7.3.8. Mechanize the web

Install the Mechanize library using pip. What does it do? How might it be useful? Build something using it. (Hint: A CLI for your favorite search engine?)

7.3.9. Explore PyPI

The Python Package Index (PyPI) is host to quite a few libraries. Browse through it. Get a feel for what exists. Pick one or more libraries that interests. Write an example application demonstrating what they do. Write up a little blog post explaining how to use it. Share it with the world.

7.4. Projects

If you want to try your hand at something larger than an exercise, consider one of the following.

7.4.1. Create The Daily Dose

Create a web application that generates spoken summaries of select web sites for users to download for offline listening, say during a commute, while working out, on a bike ride, etc. Allow users to pick what sites they would like included in their summary and in what order.

Don’t worry about user customization initially. Offer each visiting user the list of sources, allow him or her to pick and order, generate the summary (perhaps cached on an interval), and offer a link to download it.

Choose a web framework, text-to-speech library, and new sources to support. (Hint: HackerNews please.) Also consider if a text summarizer like sumy would help, depending on the types of sites and pages you choose to summarize.

7.5. References

PyPI
Official Python package index
Hitchiker’s Guide to Python
Opinionated best-practice guide for Python developers