Showing posts with label Apress. Show all posts

- A Reference for Creating 2D and 3D Images -

B.J. Korites


e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 4.00
 Pages
 365 p
 File Size 
 10,569 KB
 File Type
 PDF format
 ISBN-13
 978-1-4842-3377-1 (pbk)
 978-1-4842-3378-8  (electronic) 
 Copyright©   
 2018 by B.J. Korites

About the Author
B.J. Korites has been involved in engineering and scientific
applications of computers for his entire career. He has
been an educator, consultant, and author of more than
ten books on geometric modelling, computer graphics,
artificial intelligence, simulation of physical processes,
structural analysis, and the application of computers
in science and engineering. He has been employed by
Northrop Corporation, the Woods Hole Oceanographic
Institute, Arthur D. Little, Itek, and Worcester Polytech.
He has consulted for Stone and Webster Engineering, Gould Inc, Wyman Gordon, CTI
Cryogenics, the US Navy, Aberdeen Proving Grounds, and others. Early in his career he
developed mathematics and software that would find physical interferences between
three-dimensional solid objects. This found wide application in the design of nuclear
power plants, submarines, and other systems with densely packed spaces. He enjoys
sailing and painting maritime landscapes in oils. He holds degrees from Tufts and Yale.

About the Technical Reviewer
Andrea Gavana has been programming in Python for
almost 15 years and dabbling with other languages since the
late nineties. He graduated from university with a Master’s
degree in Chemical Engineering, and he is now a Senior
Reservoir Engineer working for Maersk Oil in Copenhagen, Denmark.
Andrea enjoys programming at work and for fun, and
he has been involved in multiple open source projects, all
Python-based. One of his favorite hobbies is Python coding,
but he is also fond of cycling, swimming, and cozy dinners
with family and friends.

Table of Contents
About the Author .............
About the Technical Reviewer ..............
Acknowledgments ....
Chapter 1: Essential Python Commands and Functions
1.1 Programming Style   2
1.2 The Plotting Area   3
1.3 Establishing the Size of the Plotting Area   4
1.4 Importing Plotting Commands   6
1.5 Displaying the Plotting Area  8
1.6 T he Plotting Grid  8
1.7 Saving a Plot  8
1.8 G rid Color   9
1.9 T ick Marks  9
1.10 Custom Grid Lines   11
1.11 L abelling the Axes  13
1.12 T he Plot Title  14
1.13 C olors  15
1.13.1 C olor Mixing  16
1.13.2 C olor Intensity  19
1.14 O verplotting  20
1.15 B ackground Color  23
1.16 T he Plotting Area Shape  23
1.17 How to Correct Shape Distortions  26
1.17.1 Applying a Scale Factor When Plotting   27
1.17.2 The Best Way: Scaling the Axes in plt.axis( )  27
1.18 Coordinate Axes   29
1.19 Commonly Used Plotting Commands and Functions   30
1.19.1 Points and Dots Using scatter( )  31
1.19.2 Lines Using plot( )  32
1.19.3 Arrows   33
1.19.4 Text   34
1.19.5 Lists, Tuples, and Arrays   36
1.19.6 Arrays   41
1.19.7 arange( )  42
1.19.8 range( )   43
1.20 S ummary   43
Chapter 2: Graphics in Two Dimensions   
2.1 Lines from Dots  45
2.2 Dot Art  50
2.3 Circular Arcs from Dots  52
2.4 Circular Arcs from Line Segments  59
2.5 C ircles   60
2.6 D ot Discs   64
2.7 E llipses  68
2.8 2D Translation   75
2.9 2 D Rotation  78
2.10 Summary  100
Chapter 3: Graphics in Three Dimensions
3.1 The Three-Dimensional Coordinate System  101
3.2 Projections onto the Coordinate Planes   104
3.3 Rotation Around the y Direction   106
3.4 Rotation Around the x Direction   109
3.5 Rotation Around the z Direction  111
3.6 Separate Rotations Around the Coordinate Directions  113
3.7 Sequential Rotations Around the Coordinate Directions   121
3.8 Matrix Concatenation   129
3.9 Keyboard Data Entry with Functional Program Structure   133
3.10 Summary  141
Chapter 4: Perspective
4.1 Summary  152
Chapter 5: Intersections
5.1 Line Intersecting a Rectangular Plane  153
5.2 Line Intersecting a Triangular Plane  166
5.3 Line Intersecting a Circle   181
5.4 Line Intersecting a Circular Sector   181
5.5 Line Intersecting a Sphere   187
5.6 Plane Intersecting a Sphere   196
5.7 S ummary  201
Chapter 6: Hidden Line Removal
6.1 Box   203
6.2 Pyramid  212
6.3 Planes   218
6.4 Sphere   225
6.5 S ummary  233
Chapter 7: Shading
7.1 Shading a Box  236
7.2 Shading a Sphere  246
7.3 Summary   253
Chapter 8: 2D Data Plotting
8.1 Linear Regression   265
8.2 Function Fitting   269
8.3 Splines   275
8.4 Summary   283
Chapter 9: 3D Data Plotting
9.1 3D Surfaces   297
9.2 3D Surface Shading   305
9.3 Summary   319
Chapter 10: Demonstrations
10.1 Saturn  321
10.2 Solar Radiation  331
10.2.1 Photons and the Sun   331
10.2.2 Max Planck’s Black Body Radiation   333
10.2.3 The Sun’s Total Power Output  334
10.3 Earth’s Irradiance  344
10.3.1 The Earth Sun Model  346
10.4 Summary  351
Appendix A: Where to Get Python ....... .......... 353
Appendix B: Planck’s Radiation Law and the Stefan-Boltzmann Equation ........... 355
Index . ........................ 359


Bookscreen
e-books shop

Acknowledgments
I would like to thank my wife, Pam, for her patience during the many long days and
nights that I spent writing this book and for her understanding of the distant stare I
sometimes had while off in another world thinking of math and Python, two of life’s great
joys. I would also like to thank everyone at Apress, especially editors Todd Green and Jill
Balzano, who made the production of this book a fast and seamless process.

Sloan Kelly


e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 4.00
 Pages
 395 p
 File Size 
 5,991 KB
 File Type
 PDF format
 ISBN-13
 978-1-4842-4532-3 (pbk)
 978-1-4842-4533-0(electronic) 
 Copyright©   
 2019 by Sloan Kelly

About the Author
Sloan Kelly has worked in the games industry
for nearly 12 years. He has worked on a
number of AAA and indie titles and currently
works for an educational game company. He
lives in Ontario, Canada, with his wife and
children. Sloan is on Twitter @codehoose and
makes YouTube videos in his spare time.

About the Technical Reviewer
John Watson is a game developer, artist, guitar
player, husband, and father. Among John’s
many software-powered side projects, he’s
building a Raspberry Pi–powered device that
generates interactive music in live modern
dance performances. He’s also developing a
retro-inspired 2D twin-stick arcade shooter
called Gravity Ace. You can follow his progress
on Twitter @yafd or at gravityace.com. Stop by
and say hi!

Introduction
This book is intended for anyone who wants to learn how to program
games. It is ideally suited to students who want to learn Python and
PyGame on their Raspberry Pi. While not necessary, this book has been
oriented toward the Raspberry Pi computer.
The Python programming language is ideally suited to beginners and
experts alike. The skills you will learn in this book are easily transferable to
other computer languages too.
If you are unfamiliar with the Raspberry Pi, there are several good
eBook guides on getting started including mine called A Slice of Raspberry
Pi, available from all good eBook retailers.
This book assumes that you are familiar with the Raspberry Pi
computer and that you have the Raspberry Pi Foundation’s recommended
Raspbian operating system installed. Raspbian is a distribution of the
Debian Linux operating system built specifically for the Raspberry Pi. This
distribution contains all the Software Development Kits (SDKs) including
one for Python that includes PyGame. If you don’t have Raspbian installed,
you will have to ensure that you have Python and PyGame installed on
your system.
Don’t have a Raspberry Pi? Not to worry, you can still learn Python and
PyGame. The code in this book will work on other OSs with Python and
PyGame installed; Python is a platform-independent language.
You can obtain more information and download versions of Python from
www.python.org. PyGame can be obtained from www.pygame.org/.
Sprites from Ari Feldman’s SpriteLib have been used for the projects
contained in this book.


Table of Contents
About the Author ..............................................................................xvii
About the Technical Reviewer ...........................................................xix
Acknowledgments .............................................................................xxi
Introduction .....................................................................................xxiii
Chapter 1: What Is a Programming Language? ....................................1
What Does a Computer Program Do? ...................................................................2
Conclusion ............................................................................................................3
Chapter 2: What Is Python? ..................................................................5
Programming Styles ..............................................................................................5
Object-Oriented .....................................................................................................7
Functional .............................................................................................................8
What Is Pygame? ..................................................................................................8
Conclusion ............................................................................................................9
Chapter 3: Introducing Python ............................................................11
The Terminal Window ..........................................................................................11
Running the Python Interpreter ...........................................................................12
Python Is Interpreted ...........................................................................................13
Python As a Calculator ........................................................................................13
Keywords ............................................................................................................16
Printing ...............................................................................................................17
String Formatting ..........................................................................................19
Variables .............................................................................................................22
Naming Variables ...........................................................................................22
Python As a Calculator, Part II .............................................................................23
Arithmetic Operators .....................................................................................26
Data Types .....................................................................................................27
Numeric Types ...............................................................................................27
String Formatting Again ................................................................................29
Conclusion ..........................................................................................................31
Chapter 4: Breaking Free from the Interpreter ...................................33
What Is IDLE? ......................................................................................................33
Starting IDLE .......................................................................................................33
Starting a New File .............................................................................................34
Hello, World! ........................................................................................................36
Running from the Command Line ..................................................................36
Running from Inside IDLE ..............................................................................38
Conclusion ..........................................................................................................39
Chapter 5: Making Decisions ..............................................................41
A Note About Blocks ............................................................................................44
Testing for Equality .............................................................................................45
Using Boolean Logic ............................................................................................50
And ................................................................................................................50
Or ...................................................................................................................51
Not .................................................................................................................52
Nesting Ifs ...........................................................................................................52
A Note on Switch ................................................................................................54
Conclusion ..........................................................................................................54
Chapter 6: Making the Raspberry Pi Repeat Itself .............................55
The for Loop ........................................................................................................55
The range() Function .....................................................................................57
While Loops .........................................................................................................58
Counting ........................................................................................................58
Sentinel .........................................................................................................60
Conditional .....................................................................................................62
Conclusion ..........................................................................................................62
Chapter 7: Containers .........................................................................63
Container Nomenclature .....................................................................................63
Tuples ..................................................................................................................64
Removing Elements from a Tuple ..................................................................64
Changing Element Values ..............................................................................65
Tuples in Printing ...........................................................................................66
Deconstructing Tuples ...................................................................................67
Lists ....................................................................................................................67
List Creation ..................................................................................................68
Adding Values to the List ...............................................................................68
Removing Values from a List .........................................................................69
Doctor’s Waiting Room Program ....................................................................70
Dictionaries .........................................................................................................73
Iterating Through Dictionaries .......................................................................74
Adding New Items to Dictionaries .................................................................74
Removing Entries from a Dictionary ..............................................................74
Conclusion ..........................................................................................................75
Chapter 8: Putting It Together: Tic-Tac-Toe ........................................77
The Rules ............................................................................................................77
Program Layout ...................................................................................................79
Variables .............................................................................................................79
The Game ............................................................................................................80
Save and Run ......................................................................................................85
Conclusion ..........................................................................................................85
Chapter 9: Basic Introduction to PyGame ...........................................87
Importing the PyGame Framework .....................................................................87
Initializing PyGame ..............................................................................................88
The Main Loop ....................................................................................................89
Images and Surfaces ..........................................................................................92
Creating Images ..................................................................................................93
Loading Images ...................................................................................................93
Drawing Images ..................................................................................................94
Screen Coordinates and Resolution ....................................................................94
Sprite Sheets ......................................................................................................95
Full Listing ..........................................................................................................97
Conclusion ..........................................................................................................97
Chapter 10: Designing Your Game ......................................................99
Initial Concept .....................................................................................................99
Prototyping ..................................................................................................100
Functional Specification ...................................................................................101
Weapon Firing .............................................................................................101
Program Design ................................................................................................101
Coding ...............................................................................................................102
Testing ..............................................................................................................104
Iteration .............................................................................................................105
Conclusion ........................................................................................................105
Chapter 11: Game Project: Bricks ....................................................107
The Main Framework ........................................................................................108
Images ..............................................................................................................109
Moving the Bat ..................................................................................................110
Bat Initialization ...........................................................................................111
Drawing the Bat ...........................................................................................112
Moving the Bat ............................................................................................112
Moving the Ball .................................................................................................114
Ball Initialization ..........................................................................................114
Ball Movement .............................................................................................115
Bat and Ball Collision ...................................................................................118
Serving the Ball ...........................................................................................120
Brick Wall ..........................................................................................................121
Brick and Ball Collision ................................................................................122
Out of Bounds ..............................................................................................124
Conclusion ........................................................................................................125
Chapter 12: User-Defined Functions .................................................127
What Is a Function? ..........................................................................................127
Format of a Function .........................................................................................127
Functions as a Menial Task/Mnemonic Device .................................................128
Sending Parameters .........................................................................................129
Default Argument Values ...................................................................................131
Named Parameters ......................................................................................132
Returning Values ...............................................................................................133
Returning Tuples ..........................................................................................134
Accessing Global Variables ...............................................................................135
Real-World Example of a Function ....................................................................136
Conclusion ........................................................................................................139
Chapter 13: File Input and Output .....................................................141
Reading a File from Disk ...................................................................................141
Writing Data to a File ........................................................................................143
Reading and Writing Containers to a File ..........................................................144
Writing Your Own Serializer .........................................................................145
Writing Your Own Deserializer .....................................................................147
JSON .................................................................................................................148
JSON Serialization .......................................................................................148
JSON Deserializer ........................................................................................149
Handling Errors .................................................................................................150
Conclusion ........................................................................................................151
Chapter 14: Introducing Object-Oriented Programming ...................153
Classes and Objects ..........................................................................................154
Encapsulation ...................................................................................................154
Abstraction ........................................................................................................155
Inheritance ........................................................................................................155
Polymorphism ...................................................................................................155
Why Should You Use OOP? ................................................................................156
Data Hiding ..................................................................................................156
Reusable ......................................................................................................156
Easier to Code and Test Separately .............................................................156
The Ball Class ...................................................................................................157
Creating an Instance of the Class .....................................................................160
The Ball update( ) Method ............................................................................161
Constructors ................................................................................................162
SOLID ................................................................................................................164
Single Responsibility ...................................................................................165
Open-Closed Principle .................................................................................165
Liskov Substitution ......................................................................................166
Interface Segregation ..................................................................................166
Dependency Inversion .................................................................................167
Conclusion ........................................................................................................170
Chapter 15: Inheritance, Composition, and Aggregation ..................171
Inheritance ........................................................................................................172
Base and Child Classes .....................................................................................173
Programming to the Interface .....................................................................175
A Note About Constructors and Base Classes .............................................175
Composition ......................................................................................................177
Aggregation .......................................................................................................179
Conclusion ........................................................................................................180
Chapter 16: Game Project: Snake .....................................................181
Functions ..........................................................................................................182
Snake Framework .............................................................................................183
Images ..............................................................................................................190
Loading the Images .....................................................................................191
The Game Map ..................................................................................................192
Drawing the ‘Game Over’ Screen ......................................................................193
Drawing the Game ............................................................................................195
Drawing the Walls .............................................................................................196
Drawing the Player Data ...................................................................................198
Drawing the Snake ............................................................................................199
Updating the Game ...........................................................................................202
The updateGame( ) Method ..........................................................................203
Snake Movement .........................................................................................205
Touching a Berry ..........................................................................................206
Collision Detection ............................................................................................208
Helper Functions .........................................................................................208
Conclusion ........................................................................................................212
Chapter 17: Model View Controller ...................................................213
Model ................................................................................................................214
View ..................................................................................................................214
Controller ..........................................................................................................214
Why Use MVC? ..................................................................................................215
The Classes .................................................................................................216
Folder ..........................................................................................................217
The Robot Model .........................................................................................217
The Robot View ............................................................................................219
The Radar View ............................................................................................221
The Robot Controller ....................................................................................222
The Robot Generator ....................................................................................225
Ensuring Constant Speed ..................................................................................227
The Main Robot Program ..................................................................................228
Conclusion ........................................................................................................230
Chapter 18: Audio .............................................................................233
Playing a Sound ................................................................................................234
Playing, Pausing, and Changing Volume ...........................................................235
Conclusion ........................................................................................................240
Chapter 19: Finite State Machines ...................................................241
Game State .......................................................................................................241
Menu System ....................................................................................................241
Non-player Artificial Intelligence .......................................................................242
A Finite State Machine Example .......................................................................243
Finite State Machine Manager ....................................................................244
Conclusion ........................................................................................................249
Chapter 20: Game Project: Invaders .................................................251
The Classes .......................................................................................................253
The Finite State Machine ..................................................................................254
MVC and ‘Invaders’ ...........................................................................................255
The Framework .................................................................................................255
Bitmap Font .................................................................................................259
Interstitial Screens ......................................................................................263
The Main Menu ............................................................................................264
Player and Bullets .............................................................................................267
The Bullet Classes .......................................................................................267
The Player Classes ......................................................................................270
Testing Player ..............................................................................................273
The Alien Swarm Classes ..................................................................................275
Collision Detection ............................................................................................282
Explosions ...................................................................................................282
Collision Controller ......................................................................................285
The Main Program .............................................................................................288
The Main Game State ........................................................................................289
Running the Game ............................................................................................292
Conclusion ........................................................................................................293
Chapter 21: Simple Electronics with the GPIO Pins ..........................295
Voltage, Current, and Resistance ......................................................................296
What You Will Need ...........................................................................................298
Breadboard ..................................................................................................298
Breakout Board ............................................................................................299
Jumper Wires ..............................................................................................300
LEDs ............................................................................................................302
Resistors ......................................................................................................304
Switches ......................................................................................................307
Building a Circuit ...............................................................................................308
Connecting the Breakout Board to the Raspberry Pi ...................................308
Providing Power and Ground .......................................................................311
Adding the LED ............................................................................................312
Completing the Circuit .................................................................................313
Testing the Circuit ........................................................................................315
Pin Meanings ....................................................................................................316
The gpiozero Library .........................................................................................317
The Circuit ...................................................................................................318
The Python Program ....................................................................................319
Getting Button Input ....................................................................................320
Reading Button Input in Python ...................................................................321
Conclusion ........................................................................................................322
Chapter 22: Game Project: Memory ..................................................323
Arranging the Breadboard .................................................................................324
Placing the LEDs ..........................................................................................324
Testing the Circuit .............................................................................................326
Placing the Tact Switches ............................................................................327
Testing the Button Circuit ............................................................................328
The Memory Game ............................................................................................330
The ButtonLED and ButtonLEDCollection Classes .......................................331
The Main Program .......................................................................................334
Full Listing buttonled.py ....................................................................................336
Full Listing memorygame.py .............................................................................338
Conclusion ........................................................................................................339
Chapter 23: Game Project: Quiz ........................................................341
The Electronics .................................................................................................341
Testing the Buttons ......................................................................................342
The Finite State Machine ..................................................................................345
Making the Game ..............................................................................................347
The Questions ..............................................................................................348
UI Helper Classes .........................................................................................354
The Game Runner and Base State Class .....................................................359
Player Input .................................................................................................361
The State Classes ........................................................................................362
Playing the Game ..............................................................................................376
Conclusion ........................................................................................................377
Chapter 24: Conclusion .....................................................................379
Index .................................................................................................381


Bookscreen
e-books shop

Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Spandana Chatterjee
Development Editor: James Markham
Coordinating Editor: Divya Modi
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)

by Tom Funk


e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 2.50
 Pages
 263 p
 File Size 
 3,644 KB
 File Type
 PDF format
 ISBN-13
 978-1-43024407-3 (pbk)
 978-1-4302-4408-0 (electronic) 
 Copyright©   
 2013 by Tom Funk

About the Author
Tom Funk has been involved in ecommerce and online
marketing since the emergence of the commercial
internet in the mid 1990s A senior marketing manager
at Green Mountain Coffee Roasters, he has a wealth of
experience in social media, online advertising, website
usability, and more Tom is a frequent speaker at
industry conferences and events His previous books
include Web 2.0 and Social Media Playbook for Business

Acknowledgments
I’d like to thank all the businesses whose stories and experiences helped shape this book
Thanks to Michael McHale at Subaru of America, and Chris Boudreaux of
Accenture, for sharing their insights with me in interviews My writing also
benefited from best practices learned from Select Design, Gary Vayverchuck and
the team at VaynerMedia, Shama Khabani of Marketing Zen Group, Ted Wright
of Fizz Marketing, Seth Godin, and the Zappos Insights crew
Thanks to Leslie Kennedy and Tina Rubio at Facebook for keeping us dialed
into new developments, and getting me that coveted invite to a Facebook Hack
Thanks too, to Fred Tietze, Amanda Swan, and Myra Sack at Extole for helping
us navigate refer-a-friend campaigns, and OpenGraph sharing
I’d like to thank everyone who makes social media tick at Green Mountain
Coffee Roasters, especially Kristen Mercure who keeps them all in line Thanks
to the folks on my Café Express social media team, Marybeth Longo, Rob
Ouellette, and Riley Houser, who launched our program and pushed it to greater
heights Thanks to Brian Galloway, with whom it’s such a kick to test all those
shiny, new Facebook advertising options.
My editor, Jeff Olson, is a joy to work with He knows his craft He’s smart, fast,
and knows the care and feeding of those unpredictible beasts called authors
Thanks also to Rita Fernando and Tamsin Willard, for moving the project swiftly
and smoothly from manuscript to finished product.
Thanks to the Sugarbush ski patrol, Dr Eric Benz of Champlain Valley
Orthopedics, and to Bristol Physical Therapy (Note to self: Next time you’re on
deadline for a book project, try not to ski into a tree.)
Finally, thanks yet again to my incredible wife Elizabeth and three fantastic
daughters Hannah, Molly, and Louisa I couldn’t have done it without you!

Table of Contents
About the Author
Acknowledgments

Chapter 1: The Business Case
Chapter 2: Best Practices
Chapter 3: The Platforms
Chapter 4: Advertising and Promotion
Chapter 5: Facebook Advertising
Chapter 6: Advertising on Twitter and Other Networks
Chapter 7: Operations
Chapter 8: Measuring Success
Chapter 9: Advanced Social Media Campaigns
Chapter 10: Power Up Your Platform
Chapter 11: Bringing It All Together

Appendix A: Business Plan Example
Appendix B: Task Checklist
Index


Bookscreen
e-books shop

President and Publisher: Paul Manning
Acquisitions Editor: Jeff Olson
Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James
Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke,
Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh
Coordinating Editor: Rita Fernando
Copy Editor: Tamsin Willard
Compositor: Bytheway Publishing Services
Indexer: SPi Global
Cover Designer: Anna Ischenko

All you will ever need to start programming Python

Tim Hall and J-P Stacey

Contents at a Glance

Introducing Python
Designing Software
Variables and Data Types
Making Choices
Using Lists
Functions
Working with Text
Executable Files, Organization, and Python on the Web
Classes
Exceptions
Reusing Code with Modules and Packages
Simple Windowed Applications

e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 2.50
 Pages
 314 p
 File Size 
 7,505 KB
 File Type
 PDF format
 ISBN-13
 978-1-4302-1632-2 (pbk)
 978-1-4302-1633-9 (electronic) 
 Copyright©   
 2009 by Tim Hall and J-P Stacey 

About the Author
Tim Hall currently provides front—line support for 64 Studio. He has also written
newbie tutorials for Linux User and Developer magazine in between more
mundane system admin and web authoring jobs.
Tim has released albums and performed as a musician and songwriter, both
solo and in collaboration with other artists. He has been further honored as the
holder of the Bardic chair of Glastonbury between 2005 and 2007. Tim uses
Python as his main programming language, primarily as a means for creative
ends, because it is easy to read and fun to learn.
J-P Stacey has been a senior developer at Torchbox Ltd since 2005, building and maintaining
(among other things) Python, Django, and Drupal applications.
He organizes the Oxford Geek Nights and gets involved in tuition and seminars at Torchbox. In his
spare time he reads and writes fiction and blogs, buys too much music, and tries not to startle Cotswold
lions on his bicycle.

About the Technical Reviewer
Duncan Parkes has been coding in Python, both for work and for fun, for roughly
a decade. He drifted into programming largely by accident after initially taking a
doctorate in Combinatorial Group Theory, a branch of Pure Mathematics. As an
ex-mathematician, he loves the clarity and simplicity of Python and needs a bit of
persuading to code in anything else. After completing a technical review of this
book, he joined Apress as an editor in the Open Source group. He currently splits
his time between editing books and coding for mySociety, a charitable
organization that runs most of the UK's best-known democracy web sites.
Duncan lives in Oxford, England, with his wife, Ruth. When away from his
computer, Duncan enjoys playing the guitar very badly, cycling long distances on a
Brompton folding bicycle, and fiddling with old cars.
His friends and colleagues have been known to run sweepstakes on how far these vehicles can get
without the assistance of a breakdown service.

Table of Contents
■About the Author ........................................................................................................ xi
■About the Technical Reviewer................................................................................... xii
■Chapter 1: Introducing Python ....................................................................................1
Running Python on Various Systems .................................................................................1
Learning While Having Fun ................................................................................................4
Introducing the Interactive Shell........................................................................................4
Choosing to Code with a Text Editor..................................................................................5
Choosing to Code with an Integrated Development Environment......................................5
Getting Started with Programming ....................................................................................5
Creating Your Own Help Files ............................................................................................6
Jargon Busting...................................................................................................................6
Summary ...........................................................................................................................7
■Chapter 2: Designing Software....................................................................................9
Designing Your Own Software (Why Bother?)....................................................................9
Identifying the Problem....................................................................................................10
Creating Your Wish List....................................................................................................12
Devising a Solution ..........................................................................................................13
Breaking Down the Solution into Steps ...........................................................................15
Jargon Busting.................................................................................................................24
Summary .........................................................................................................................25
■Chapter 3: Variables and Data Types.........................................................................27
Choosing Good Identifiers................................................................................................27
Creating Variables and Assigning Values.........................................................................28
Recognizing Different Types of Variables ........................................................................29
Jargon Busting.................................................................................................................45
Summary .........................................................................................................................47
■Chapter 4: Making Choices........................................................................................49
Comparing Things............................................................................................................49
Taking Control of the Process..........................................................................................55
Dealing with Logical Errors..............................................................................................57
Using Conditional Code in the Application .......................................................................61
Now Repeat That .............................................................................................................65
Jargon Busting.................................................................................................................73
Summary .........................................................................................................................74
■Chapter 5: Using Lists................................................................................................75
Working with Python Data Structures..............................................................................75
Tuples ..............................................................................................................................79
Lists .................................................................................................................................80
Sets..................................................................................................................................85
Dictionaries......................................................................................................................86
A Simple Role-Playing Combat Game ..............................................................................89
Jargon Busting.................................................................................................................99
Summary .......................................................................................................................100
■Chapter 6: Functions ...............................................................................................101
Accessing Privileged Information ..................................................................................101
Working with Variable Scope.........................................................................................105
Refactoring rpcombat.py to Reduce Repetition .............................................................108
Jargon Busting...............................................................................................................123
Summary .......................................................................................................................124
■Chapter 7: Working with Text..................................................................................125
Strings and Things.........................................................................................................125
Matching Patterns Using Regular Expressions ..............................................................135
Using Files .....................................................................................................................141
Applications ...................................................................................................................145
Jargon Busting...............................................................................................................159
Summary .......................................................................................................................160
■Chapter 8: Executable Files, Organization, and Python on the Web........................161
Making Programs Executable as Stand-Alone Applications ..........................................161
Organizing Your Project .................................................................................................164
Writing Stylish Code.......................................................................................................165
Importing Modules.........................................................................................................170
Using exec() and eval()...................................................................................................172
Putting Python on the Web ............................................................................................173
Jargon Busting...............................................................................................................179
Summary .......................................................................................................................179
■Chapter 9: Classes...................................................................................................181
Empowering objects ......................................................................................................182
When Should Classes Be Used? ....................................................................................185
Customizing Classes......................................................................................................191
Application .....................................................................................................................200
Jargon Busting...............................................................................................................219
Summary .......................................................................................................................220
■Chapter 10: Exceptions............................................................................................221
When Something Goes Wrong .......................................................................................221
Classes of Exceptions ....................................................................................................224
A Final Note on Pythonic Exception Handling ................................................................238
Jargon Busting...............................................................................................................239
Summary .......................................................................................................................240
■Chapter 11: Reusing Code with Modules and Packages .........................................241
Understanding Python Modules.....................................................................................241
Everyday Module Usage.................................................................................................244
Advanced Module Behavior ...........................................................................................249
Combining Modules into Packages................................................................................252
The Universe of Python packages..................................................................................254
Jargon Busting...............................................................................................................259
Summary .......................................................................................................................260
■Chapter 12: Simple Windowed Applications............................................................261
Using Tkinter..................................................................................................................261
Saying “Hello” with PyGTK ............................................................................................265
Using Glade and tepache to Build Interfaces.................................................................279
Jargon Busting...............................................................................................................282
Summary .......................................................................................................................282
■Index........................................................................................................................283


Bookscreen
e-books shop

Lead Editor: Matthew Moodie
Technical Reviewer: Duncan Parkes
Additional material: Dr. J. Burton Browning
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank
Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Managers: Beth Christmas and Debra Kelly
Copy Editor: Heather Lang
Compositor: LaurelTech
Indexer: BIM Indexing and e-Services
Artist: April Milne
Loading...
DMCA.com Protection Status