This week I taught a class called Moderately Advanced Python Projects, for five of my best students, who are going into grades 8–10. I’m considering offering a class, Simple to Intermediate Python Projects, for my younger and/or less experienced students.

Starting with a Project

I’ve had good results starting with a project idea, and then teaching the Python required to implement it along the way. Making progress on a fun and interesting project motivates my students to do the work to learn.

This Week’s Class

I’ll go into some detail about this week’s class, Moderately Advanced Python Projects.

Ideas

I started by proposing some ideas:

  • Conway’s Game of Life in Processing
  • Web clients
  • Web servers
  • Fetch and graph info, such as weather
  • Show a Google Map around a place
  • Joke, quote, math puzzle, treasure hunt servers
  • Adventure games

Review

I started with a review using these notebooks I created.

Right Triangles

In practicing list comprehensions, we decided to find some right triangles using the Pythagorean Theorem. I tweeted about it.

Conway’s Game of Life

We started a Conway’s Game of Life using Processing. We got as far as drawing cells and setting them to random patterns.

Web Clients

We wrote web clients using the requests package, and directed them at a web server one of the students was running. The kids were pleased with how quickly we could make requests and even slow down the server computer. We discussed how this is like a distributed denial of service attack (DDOS), and how we would never do this outside of a local environment.

Morse Code Server Access

Give us the passphrase

Our main project was a Web server that would not give up its secret unless it was called with a stream of Web requests with the morse code representation of a passphrase, open sesame; and a client that would send the requests. Have a look at the source code.

New Python for This Project

Writing the Morse Code client and server required learning some new things:

  • Python logging
  • Response headers (text/html, text/plain)
  • Getting the client’s IP addresses from flask (request.remote_addr)
  • HTTP status codes (200, 400, 403)
  • Classes
  • Getting None from a dictionary
  • Getting a value from a dictionary with a default value

Review Questions

Our review questions should give you some idea of the things we covered.

  • Relationship between HTTP, TCP, and IP?
  • What is an IP address and a port?
  • How to make a generator?
  • A set?
  • A list comprehension with an “if”?
  • How can you make something like a two-dimensional array in Python?
  • How can you set grade to ‘P’ if score is > 90, otherwise ‘F’
  • How can we get JSON data from the web and turn it into a dictionary?
  • How can you have a Flask server save information from request to request, and update it inside an “@app.route” function?
  • How did I prove that M.’s “404” was fake?
  • How is the Morse Code server able to keep separate the streams of activity from multiple clients?
  • What’s the benefit of the different logging levels?
  • How are we able to use a single @app.route and function for both “-” and “.”?
  • How does a new UserState get created?
  • Why do we need to set the Content-Type response header?
  • Why might it be good to use a generator when memory and CPU constraints don’t suggest a need for it?
  • What does the time function of the time module return?
  • What are some HTTP request verbs?