Python Playground

Book description

Python is a powerful programming language that's easy to learn and fun to play with. But once you've gotten a handle on the basics, what do you do next?

Python Playground is a collection of imaginative programming projects that will inspire you to use Python to make art and music, build simulations of real-world phenomena, and interact with hardware like the Arduino and Raspberry Pi. You'll learn to use common Python tools and libraries like numpy, matplotlib, and pygame to do things like:

  • Generate Spirograph-like patterns using parametric equations and the turtle module
  • Create music on your computer by simulating frequency overtones
  • Translate graphical images into ASCII art
  • Write an autostereogram program that produces 3D images hidden beneath random patterns
  • Make realistic animations with OpenGL shaders by exploring particle systems, transparency, and billboarding techniques
  • Construct 3D visualizations using data from CT and MRI scans
  • Build a laser show that responds to music by hooking up your computer to an Arduino

Programming shouldn't be a chore. Have some solid, geeky fun with Python Playground.

Publisher resources

View/Submit Errata

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Brief Contents
  5. Contents in Detail
  6. Acknowledgments
  7. Introduction
    1. Who Is This Book For?
    2. What’s in This Book?
      1. Part I: Warming Up
      2. Part II: Simulating Life
      3. Part III: Fun with Images
      4. Part IV: Enter 3D
      5. Part V: Hardware Hacking
    3. Why Python?
      1. Python Versions
      2. The Code in This Book
  8. Part I: Warming Up
    1. Chapter 1: Parsing iTunes Playlists
      1. Anatomy of the iTunes Playlist File
      2. Requirements
      3. The Code
        1. Finding Duplicates
        2. Extracting Duplicates
        3. Finding Tracks Common Across Multiple Playlists
        4. Collecting Statistics
        5. Plotting Your Data
        6. Command Line Options
      4. The Complete Code
      5. Running the Program
      6. Summary
      7. Experiments!
    2. Chapter 2: Spirographs
      1. Parametric Equations
        1. Spirograph Equations
        2. Turtle Graphics
      2. Requirements
      3. The Code
        1. The Spiro Constructor
        2. The Setup Functions
        3. The restart() Method
        4. The draw() Method
        5. Creating the Animation
        6. The SpiroAnimator Class
        7. The genRandomParams() Method
        8. Restarting the Program
        9. The update() Method
        10. Showing or Hiding the Cursor
        11. Saving the Curves
        12. Parsing Command Line Arguments and Initialization
      4. The Complete Code
      5. Running the Spirograph Animation
      6. Summary
      7. Experiments!
  9. Part II: Simulating Life
    1. Chapter 3: Conway’s Game of Life
      1. How It Works
      2. Requirements
      3. The Code
        1. Representing the Grid
        2. Initial Conditions
        3. Boundary Conditions
        4. Implementing the Rules
        5. Sending Command Line Arguments to the Program
        6. Initializing the Simulation
      4. The Complete Code
      5. Running the Game of Life Simulation
      6. Summary
      7. Experiments!
    2. Chapter 4: Generating Musical Overtones with the Karplus-Strong Algorithm
      1. How It Works
        1. The Simulation
        2. Creating WAV Files
        3. The Minor Pentatonic Scale
      2. Requirements
      3. The Code
        1. Implementing the Ring Buffer with deque
        2. Implementing the Karplus-Strong Algorithm
        3. Writing a WAV File
        4. Playing WAV Files with pygame
        5. The main() Method
      4. The Complete Code
      5. Running the Plucked String Simulation
      6. Summary
      7. Experiments!
    3. Chapter 5: Boids: Simulating a Flock
      1. How It Works
      2. Requirements
      3. The Code
        1. Computing the Position and Velocities of the Boids
        2. Setting Boundary Conditions
        3. Drawing a Boid
          1. Plotting the Boid’s Body and Head
          2. Updating the Boid’s Position
        4. Applying the Rules of the Boids
        5. Adding a Boid
        6. Scattering the Boids
        7. Command Line Arguments
        8. The Boids Class
      4. The Complete Code
      5. Running the Boids Simulation
      6. Summary
      7. Experiments!
  10. Part III: Fun with Images
    1. Chapter 6: ASCII Art
      1. How It Works
      2. Requirements
      3. The Code
        1. Defining the Grayscale Levels and Grid
        2. Computing the Average Brightness
        3. Generating the ASCII Content from the Image
        4. Command Line Options
        5. Writing the ASCII Art Strings to a Text File
      4. The Complete Code
      5. Running the ASCII Art Generator
      6. Summary
      7. Experiments!
    2. Chapter 7: Photomosaics
      1. How It Works
        1. Splitting the Target Image
        2. Averaging Color Values
        3. Matching Images
      2. Requirements
      3. The Code
        1. Reading in the Tile Images
        2. Calculating the Average Color Value of the Input Images
        3. Splitting the Target Image into a Grid
        4. Finding the Best Match for a Tile
        5. Creating an Image Grid
        6. Creating the Photomosaic
        7. Adding the Command Line Options
        8. Controlling the Size of the Photomosaic
      4. The Complete Code
      5. Running the Photomosaic Generator
      6. Summary
      7. Experiments!
    3. Chapter 8: Autostereograms
      1. How It Works
        1. Perceiving Depth in an Autostereogram
        2. Depth Maps
      2. Requirements
      3. The Code
        1. Repeating a Given Tile
        2. Creating a Tile from Random Circles
        3. Creating Autostereograms
        4. Command Line Options
      4. The Complete Code
      5. Running the Autostereogram Generator
      6. Summary
      7. Experiments!
  11. Part IV: Enter 3D
    1. Chapter 9: Understanding OpenGL
      1. Old-School OpenGL
      2. Modern OpenGL: The 3D Graphics Pipeline
        1. Geometric Primitives
        2. 3D Transformations
        3. Shaders
          1. A Vertex Shader
          2. A Fragment Shader
        4. Vertex Buffers
        5. Texture Mapping
        6. Displaying OpenGL
      3. Requirements
      4. The Code
        1. Creating an OpenGL Window
        2. Setting Callbacks
          1. The Keyboard Callback
          2. The Window-Resizing Event
          3. The Main Loop
        3. The Scene Class
          1. Defining the 3D Geometry
          2. Defining the GLSL Shaders
      5. The Complete Code
      6. Running the OpenGL Application
      7. Summary
      8. Experiments!
    2. Chapter 10: Particle Systems
      1. How It Works
        1. Modeling the Motion of a Particle
        2. Setting a Maximum Spread
        3. Rendering the Particles
        4. Using OpenGL Blending to Create More Realistic Sparks
        5. Using Billboarding
        6. Animating the Sparks
      2. Requirements
      3. The Code for the Particle System
        1. Defining the Particle Geometry
        2. Defining the Time-Lag Array for the Particles
        3. Setting the Initial Particle Velocities
        4. Creating the Vertex Shader
        5. Creating the Fragment Shader
        6. Rendering
          1. Computing the Rotation Matrix for Billboarding
          2. The Main Rendering Code
        7. The Camera Class
      4. The Complete Particle System Code
      5. The Box Code
      6. The Code for the Main Program
        1. Updating the Particles at Each Step
        2. The Keyboard Handler
        3. Managing the Main Program Loop
      7. The Complete Main Program Code
      8. Running the Program
      9. Summary
      10. Experiments!
    3. Chapter 11: Volume Rendering
      1. How It Works
        1. Data Format
        2. Generating Rays
          1. Ray Casting in the GPU
          2. Showing 2D Slices
        3. Displaying the OpenGL Window
      2. Requirements
      3. An Overview of the Project Code
      4. Generating a 3D Texture
      5. The Complete 3D Texture Code
      6. Generating Rays
        1. Defining the Color Cube Geometry
        2. Creating the Frame Buffer Object
        3. Rendering the Back-Faces of the Cube
        4. Rendering the Front-Faces of the Cube
        5. Rendering the Whole Cube
        6. The Resize Handler
      7. The Complete Ray Generation Code
      8. Volume Ray Casting
        1. The Vertex Shader
        2. The Fragment Shader
      9. The Complete Volume Ray Casting Code
      10. 2D Slicing
        1. The Vertex Shader
        2. The Fragment Shader
        3. A User Interface for 2D Slicing
      11. The Complete 2D Slicing Code
      12. Putting the Code Together
      13. The Complete Main File Code
      14. Running the Program
      15. Summary
      16. Experiments!
  12. Part V: Hardware Hacking
    1. Chapter 12: Introduction to the Arduino
      1. The Arduino
      2. The Arduino Ecosystem
        1. Language
        2. IDE
        3. Community
        4. Peripherals
      3. Requirements
      4. Building the Light-Sensing Circuit
        1. How the Circuit Works
        2. The Arduino Sketch
        3. Creating the Real-Time Graph
      5. The Python Code
      6. The Complete Python Code
      7. Running the Program
      8. Summary
      9. Experiments!
    2. Chapter 13: Laser Audio Display
      1. Generating Patterns with a Laser
        1. Motor Control
        2. The Fast Fourier Transform
      2. Requirements
        1. Constructing the Laser Display
          1. Aligning the Mirrors
          2. Powering the Motors
        2. Wiring the Motor Driver
      3. The Arduino Sketch
        1. Configuring the Arduino’s Digital Output Pins
        2. The Main Loop
        3. Stopping the Motors
      4. The Python Code
        1. Selecting the Audio Device
        2. Reading Data from the Input Device
        3. Computing the FFT of the Data Stream
        4. Extracting Frequency Information from the FFT Values
        5. Converting Frequency to Motor Speed and Direction
        6. Testing the Motor Setup
        7. Command Line Options
        8. Manual Testing
      5. The Complete Python Code
      6. Running the Program
      7. Summary
      8. Experiments!
    3. Chapter 14: A Raspberry Pi–based Weather Monitor
      1. The Hardware
        1. The DHT11 Temperature and Humidity Sensor
        2. The Raspberry Pi
        3. Setting Up Your Pi
      2. Installing and Configuring Software
        1. The Operating System
        2. Initial Configuration
        3. Wi-Fi Setup
        4. Setting Up the Programming Environment
        5. Connecting via SSH
        6. The Bottle Web Framework
        7. Plotting with flot
        8. Shutting Down the Pi
      3. Building the Hardware
      4. The Code
        1. Handling Sensor Data Requests
        2. Plotting the Data
        3. The update() Method
        4. The JavaScript Handler for the LED
        5. Adding Interactivity
      5. The Complete Code
      6. Running the Program
      7. Summary
      8. Experiments!
  13. Appendix A: Software Installation
    1. Installing Source Code for the Book’s Projects
    2. Installing on Windows
      1. Installing GLFW
      2. Installing Prebuilt Binaries for Each Module
      3. Other Options
    3. Installing on OS X
      1. Installing Xcode and MacPorts
      2. Installing Modules
    4. Installing on Linux
  14. Appendix B: Basic Practical Electronics
    1. Common Components
    2. Essential Tools
    3. Building Circuits
    4. Going Further
  15. Appendix C: Raspberry Pi Tips and Tricks
    1. Setting Up Wi-Fi
    2. Checking Whether Your Pi Is Connected
    3. Preventing the Wi-Fi Adapter from Going to Sleep
    4. Backing Up Your Code and Data from the Pi
    5. Backing Up Your Entire Pi OS
    6. Logging In to Your Pi with SSH
    7. Using the Raspberry Pi Camera
    8. Enabling Sound on Your Pi
    9. Making Your Pi Talk
    10. Making HDMI Work
    11. Making Your Pi Mobile
    12. Checking Your Raspberry Pi Hardware Version
  16. Index
  17. Footnotes
    1. Chapter 2: Spirographs
    2. Chapter 8: Autostereograms
    3. Chapter 11: Volume Rendering
    4. Chapter 13: Laser Audio Display
    5. Appendix B: Basic Practical Electronics
    6. Appendix C: Raspberry Pi Tips and Tricks
  18. Resources
  19. About the Author

Product information

  • Title: Python Playground
  • Author(s): Mahesh Venkitachalam
  • Release date: October 2015
  • Publisher(s): No Starch Press
  • ISBN: 9781593276041