Python Playground

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Mahesh Venkitachalam
Publisher: No Starch Press, Inc.
Year: 2016

Language: English

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