Magnus Lie Hetland

Beginning Python

From Novice to Professional

3rd ed.

Magnus Lie Hetland

Trondheim, Norway

Any source code or other supplementary material referenced by the author in this book is available to readers for download or cloning at Github via the book's product page, located at www.apress.com/9781484200292 . For more detailed information, please visit http://www.apress.com/us/services/source-code .

ISBN 978-1-4842-0029-2

e-ISBN 978-1-4842-0028-5

DOI 10.1007/978-1-4842-0028-5

Library of Congress Control Number: 2017934891

© Magnus Lie Hetland 2017

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Printed on acid-free paper

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

For Kjersti and Tor.

Preface

To quote the old Monty Python song: “Here comes another one / Here it comes again / Here comes another one / When will it ever end?” Since the previous edition, Python 3 has become much more widespread, so this edition has fully transitioned to the Py3 world. There have been other changes as well, with packages in the Python ecosystem coming and going and coding practices going in and out of fashion. Where it has been necessary or useful, the book has been rewritten, but its origins are still visible. For example, when the original Practical Python came out at the beginning of the millennium, Usenet was still in semi-widespread use, though nowadays most Internet users probably haven’t even heard of it. So when the fourth code project (Chapter 23 ) involves connecting to an NNTP server, this is more of a historical curiosity than skills you are likely to apply directly in a mainstream programming career. Still, I’ve kept some of these more quirky parts of the book, as they still work well as programming examples and as they are part of the history of the book.

All the people who helped previous editions see the light of day still deserve as many thanks as before. This time around, I would in particular like to extend my thanks to Mark Powers, who has been a paragon of patience when my progress faltered. I’d also like to thank Michael Thomas, who has done a great job of checking the technical aspects of the book (… and pointing out all the Python 2–style print statements I had missed; I hope I got them all). I hope you enjoy this updated edition, even though, as Terry Jones says about the song mentioned initially, “Obviously it would be better with a full orchestra.”

Preface to the Second Edition

Here it is—a shiny new edition of Beginning Python . If you count its predecessor, Practical Python , this is actually the third edition, and a book I’ve been involved with for the better part of a decade. During this time, Python has seen many interesting changes, and I’ve done my best to update my introduction to the language. At the moment, Python is facing perhaps its most marked transition in a very long time: the introduction of version 3. As I write this, the final release isn’t out yet, but the features are clearly defined and working versions are available. One interesting challenge linked to this language revision is that it isn’t backward-compatible. In other words, it doesn’t simply add features that I could pick and choose from in my writing. It also changes the existing language, so that certain things that are true for Python 2.5 no longer hold.

Had it been clear that the entire Python community would instantly switch to the new version and update all its legacy code, this would hardly be a problem. Simply describe the new language! However, a lot of code written for older versions exists, and much will probably still be written, until version 3 is universally accepted as The Way To Go™.

So, how have I gotten myself out of this pickle? First of all, even though there are incompatible changes, most of the language remains the same. Therefore, if I wrote entirely about Python 2.5, it would be mostly correct for Python 3 (and even more so for its companion release, 2.6). As for the parts that will no longer be correct, I have been a bit conservative and assumed that full adoption of version 3 will take some time. I have based the book primarily on 2.5 and noted things that will change throughout the text. In addition, I’ve included Appendix D, which gives you an overview of the main changes. I think this will work out for most readers.

In writing this second edition, I have had a lot of help from several people. Just as with the previous two versions (the first edition, and, before it, Practical Python ), Jason Gilmore got me started and played an important role in getting the project on the road. As it has moved along, Richard Dal Porto, Frank Pohlmann, and Dominic Shakeshaft have been instrumental in keeping it going. Richard Taylor has certainly played a crucial role in ensuring that the code is correct (and if it still isn’t, I’m the one to blame), and Marilyn Smith has done a great job tuning my writing. My thanks also go out to other Apress staff, including Liz Berry, Beth Christmas, Steve Anglin, and Tina Nielsen, as well as various readers who have provided errata and helpful suggestions, including Bob Helmbold and Waclaw Kusnierczyk. I am also, of course, still thankful to all those who helped in getting the first two incarnations of this book on the shelves.

Preface to the First Edition

A few years ago, Jason Gilmore approached me about writing a book for Apress. He had read my online Python tutorials and wanted me to write a book in a similar style. I was flattered, excited, and just a little nervous. The one thing that worried me the most was how much time it would take, and how much it would interfere with my studies (I was a Ph.D student at the time). It turned out to be quite an undertaking, and it took me a lot longer to finish than I had expected.

Luckily, it didn’t interfere too much with my school work, and I managed to get my degree without any delays.

Last year, Jason contacted me again. Apress wanted an expanded and revised version of my book. Was I interested? At the time, I was busy settling into a new position as associate processor, while spending all my spare time portraying Peer Gynt, so again time became the major issue. Eventually (after things had settled down a bit, and I had a bit more time to spare), I agreed to do the book, and this (as I’m sure you’ve gathered) is the result. Most of the material is taken from the first version of the book, Practical Python (Apress, 2002). The existing material has been completely revised, based on recent changes in the Python language, and several new chapters have been added. Some of the old material has also been redistributed to accommodate the new structure. I’ve received a lot of positive feedback from readers about the first version. I hope I’ve been able to keep what people liked and to add more of the same.

Without the persistent help and encouragement from several people, this book would never have been written. My heartfelt thanks go out to all of them. In particular, I would like to thank the team that has worked directly with me in the process of writing the book: Jason Gilmore, for getting the project off the ground and steering it in the right direction; Beckie Stones, for keeping everything together; Jeremy Jones and Matt Moodie for their technical comments and insights; and Linda Marousek for being so patient with me. I’m also grateful to the rest of the team for making the process as smooth as it has been. But this book wouldn’t be what it is without several people who worked with me on the previous version: I’d like to thank Jason Gilmore and Alex Martelli for their excellent technical editing (Jason on the entire book, and Alex on the first half) and for going above and beyond the call of duty in dispensing advice and suggestions; Erin Mulligan and Tory McLearn for holding my hand through the process and for nudging me along when that was needed; Nancy Rapoport for her help polishing my prose; and Grace Wong for providing answers when no one else could. Pete Shinners gave me several helpful suggestions on the game in Project 10, for which I am very grateful. My morale has also been heavily boosted by several encouraging emails from satisfied readers—thanks! Finally, I would like to thank my family and friends, and my girlfriend, Ranveig, for putting up with me while I was writing this book.

Introduction

A C program is like a fast dance on a newly waxed dance floor by people carrying razors.

—Waldi Ravens

C++: Hard to learn and built to stay that way.

—Anonymous

Java is, in many ways, C++ – –.

—Michael Feldman

And now for something completely different . . .

—Monty Python’s Flying Circus

I’ve started this introduction with a few quotes to set the tone for the book, which is rather informal. In the hope of making it an easy read, I’ve tried to approach the topic of Python programming with a healthy dose of humor, and true to the traditions of the Python community, much of this humor is related to Monty Python sketches. As a consequence, some of my examples may seem a bit silly; I hope you will bear with me. (And, yes, the name Python is derived from Monty Python, not from snakes belonging to the family Pythonidae.) In this introduction, I give you a quick look at what Python is, why you should use it, who uses it, who this book’s intended audience is, and how the book is organized.

So, what is Python, and why should you use it? To quote an old official blurb, it is “an interpreted, object-oriented, high-level programming language with dynamic semantics.” Many of these terms will become clear as you read this book, but the gist is that Python is a programming language that knows how to stay out of your way when you write your programs. It enables you to implement the functionality you want without any hassle and lets you write programs that are clear and readable (much more so than programs in most other currently popular programming languages).

Even though Python might not be as fast as compiled languages such as C or C++, what you save in programming time will probably be worth using it, and in most programs, the speed difference won’t be noticeable anyway. If you are a C programmer, you can easily implement the critical parts of your program in C at a later date and have them interoperate with the Python parts. If you haven’t done any programming before (and perhaps are a bit confused by my references to C and C++), Python’s combination of simplicity and power makes it an ideal choice as a place to start.

So, who uses Python? Since Guido van Rossum created the language in the early 1990s, its following has grown steadily, and interest has increased markedly in the past few years. Python is used extensively for system administration tasks (it is, for example, a vital component of several Linux distributions), but it is also used to teach programming to complete beginners. The US National Aeronautics and Space Administration (NASA) uses Python both for development and as a scripting language in several of its systems. Industrial Light & Magic uses Python in its production of special effects for large-budget feature films. Yahoo! uses it (among other things) to manage its discussion groups. Google has used it to implement many components of its web crawler and search engine. Python is being used in such diverse areas as computer games and bioinformatics. Soon one might as well ask, “Who isn’t using Python?”

This book is for those of you who want to learn how to program in Python. It is intended to suit a wide audience, from neophyte programmer to advanced computer wiz. If you have never programmed before, you should start by reading Chapter 1 and continue until you find that things get too advanced for you (if, indeed, they do). Then you should start practicing and write some programs of your own. When the time is right, you can return to the book and proceed with the more intricate stuff.

If you already know how to program, some of the introductory material might not be new to you (although there will probably be some surprising details here and there). You could skim through the early chapters to get an idea of how Python works, or perhaps read through Appendix A, which is based on my online Python tutorial “Instant Python.” It will get you up to speed on the most important Python concepts. After getting the big picture, you could jump straight to Chapter 10 (which describes the Python standard libraries).

The last ten chapters present ten programming projects, which show off various capabilities of the Python language. These projects should be of interest to beginners and experts alike. Although some of the material in the later projects may be a bit difficult for an inexperienced programmer, following the projects in order (after reading the material in the first part of the book) should be possible.

The projects touch upon a wide range of topics, most of which will be very useful to you when writing programs of your own. You will learn how to do things that may seem completely out of reach to you at this point, such as creating a chat server, a peer-to-peer file-sharing system, or a full-fledged graphical computer game. Although much of the material may seem hard at first glance, I think you will be surprised by how easy most of it really is. If you would like to download the source code, it’s available from the Source Code/Download section of the Apress web site ( http://www.apress.com ).

Well, that’s it. I always find long introductions a bit boring myself, so I’ll let you continue with your Pythoneering, either in Chapter 1 or in Appendix A. Good luck, and happy hacking.

Contents

  1. Chapter 1:​ Instant Hacking:​ The Basics
    1. The Interactive Interpreter
    2. Algo .​ .​ .​ What?​
    3. Numbers and Expressions
      1. Hexadecimals Octals and Binary
    4. Variables
    5. Statements
    6. Getting Input from the User
    7. Functions
    8. Modules
      1. cmath and Complex Numbers
      2. Back to the _​_​future_​_​
    9. Saving and Executing Your Programs
      1. Running Your Python Scripts from a Command Prompt
      2. Making Your Scripts Behave Like Normal Programs
      3. Comments
    10. Strings
      1. Single-Quoted Strings and Escaping Quotes
      2. Concatenating Strings
      3. String Representations, str and repr
      4. Long Strings, Raw Strings, and bytes
    11. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  2. Chapter 2:​ Lists and Tuples
    1. Sequence Overview
    2. Common Sequence Operations
      1. Indexing
      2. Slicing
      3. Adding Sequences
      4. Multiplication
      5. Membership
      6. Length, Minimum, and Maximum
    3. Lists:​ Python’s Workhorse
      1. The list Function
      2. Basic List Operations
      3. List Methods
    4. Tuples:​ Immutable Sequences
    5. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  3. Chapter 3:​ Working with Strings
    1. Basic String Operations
    2. String Formatting:​ The Short Version
    3. String Formatting:​ The Long Version
      1. Replacement Field Names
      2. Basic Conversions
      3. Width, Precision, and Thousands Separators
      4. Signs, Alignment, and Zero-Padding
    4. String Methods
      1. center
      2. find
      3. lower
      4. replace
      5. split
      6. strip
      7. translate
      8. Is My String …
    5. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  4. Chapter 4:​ Dictionaries:​ When Indices Won’t Do
    1. Dictionary Uses
    2. Creating and Using Dictionaries
      1. The dict Function
      2. Basic Dictionary Operations
      3. String Formatting with Dictionaries
      4. Dictionary Methods
    3. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  5. Chapter 5:​ Conditionals, Loops, and Some Other Statements
    1. More About print and import
      1. Printing Multiple Arguments
      2. Importing Something as Something Else
    2. Assignment Magic
      1. Sequence Unpacking
      2. Chained Assignments
      3. Augmented Assignments
    3. Blocks:​ The Joy of Indentation
    4. Conditions and Conditional Statements
      1. So That’s What Those Boolean Values Are For
      2. Conditional Execution and the if Statement
      3. else Clauses
      4. elif Clauses
      5. Nesting Blocks
      6. More Complex Conditions
      7. Assertions
    5. Loops
      1. while Loops
      2. for Loops
      3. Iterating Over Dictionaries
      4. Some Iteration Utilities
      5. Breaking Out of Loops
      6. else Clauses in Loops
    6. Comprehensions—Slightly Loopy
    7. And Three for the Road
      1. Nothing Happened!
      2. Deleting with del
      3. Executing and Evaluating Strings with exec and eval
    8. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  6. Chapter 6:​ Abstraction
    1. Laziness Is a Virtue
    2. Abstraction and Structure
    3. Creating Your Own Functions
      1. Documenting Functions
      2. Functions That Aren’t Really Functions
    4. The Magic of Parameters
      1. Where Do the Values Come From?​
      2. Can I Change a Parameter?​
      3. Why Would I Want to Modify My Parameters?​
    5. What If My Parameter Is Immutable?​
      1. Keyword Parameters and Defaults
      2. Collecting Parameters
      3. Reversing the Process
    6. Parameter Practice
    7. Scoping
    8. Recursion
      1. Two Classics:​ Factorial and Power
    9. Another Classic:​ Binary Search
    10. A Quick Summary
    11. New Functions in This Chapter
    12. What Now?​
  7. Chapter 7:​ More Abstraction
    1. The Magic of Objects
      1. Polymorphism
      2. Polymorphism and Methods
      3. Polymorphism Comes in Many Forms
      4. Encapsulation
      5. Inheritance
    2. Classes
      1. What Is a Class, Exactly?​
      2. Making Your Own Classes
      3. Attributes, Functions, and Methods
      4. Privacy Revisited
      5. The Class Namespace
      6. Specifying a Superclass
      7. Investigating Inheritance
      8. Multiple Superclasses
      9. Interfaces and Introspection
      10. Abstract Base Classes
    3. Some Thoughts on Object-Oriented Design
    4. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  8. Chapter 8:​ Exceptions
    1. What Is an Exception?​
    2. Making Things Go Wrong .​ .​ .​ Your Way
      1. The raise Statement
      2. Custom Exception Classes
    3. Catching Exceptions
      1. Look, Ma, No Arguments!
      2. More Than One except Clause
      3. Catching Two Exceptions with One Block
      4. Catching the Object
      5. A Real Catchall
      6. When All Is Well
      7. And Finally .​ .​
    4. Exceptions and Functions
    5. The Zen of Exceptions
    6. Not All That Exceptional
    7. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  9. Chapter 9:​ Magic Methods, Properties, and Iterators
    1. If You’re Not Using Python 3
    2. Constructors
      1. Overriding Methods in General, and the Constructor in Particular
      2. Calling the Unbound Superclass Constructor
      3. Using the super Function
    3. Item Access
      1. The Basic Sequence and Mapping Protocol
      2. Subclassing list, dict, and str
    4. More Magic
    5. Properties
      1. The property Function
      2. Static Methods and Class Methods
      3. _​_​getattr_​_​, _​_​setattr_​_​, and Friends
    6. Iterators
      1. The Iterator Protocol
      2. Making Sequences from Iterators
    7. Generators
      1. Making a Generator
      2. A Recursive Generator
      3. Generators in General
      4. Generator Methods
      5. Simulating Generators
    8. The Eight Queens
      1. Generators and Backtracking
      2. The Problem
      3. State Representation
      4. Finding Conflicts
      5. The Base Case
      6. The Recursive Case
      7. Wrapping It Up
    9. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  10. Chapter 10:​ Batteries Included
    1. Modules
      1. Modules Are Programs
      2. Modules Are Used to Define Things
      3. Making Your Modules Available
      4. Packages
    2. Exploring Modules
      1. What’s in a Module?​
      2. Getting Help with help
      3. Documentation
      4. Use the Source
    3. The Standard Library:​ A Few Favorites
      1. sys
      2. os
      3. fileinput
      4. Sets, Heaps, and Deques
      5. time
      6. random
      7. shelve and json
      8. re
      9. Other Interesting Standard Modules
    4. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  11. Chapter 11:​ Files and Stuff
    1. Opening Files
      1. File Modes
    2. The Basic File Methods
      1. Reading and Writing
      2. Piping Output
      3. Reading and Writing Lines
      4. Closing Files
      5. Using the Basic File Methods
    3. Iterating over File Contents
      1. One Character (or Byte) at a Time
      2. One Line at a Time
      3. Reading Everything
      4. Lazy Line Iteration with fileinput
      5. File Iterators
    4. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  12. Chapter 12:​ Graphical User Interfaces
    1. Building a Sample GUI Application
      1. Initial Exploration
      2. Layout
      3. Event Handling
      4. The Final Program
    2. Using Something Else
    3. A Quick Summary
      1. What Now?​
  13. Chapter 13:​ Database Support
    1. The Python Database API
      1. Global Variables
      2. Exceptions
      3. Connections and Cursors
      4. Types
    2. SQLite and PySQLite
      1. Getting Started
      2. A Sample Database Application
      3. Creating and Populating Tables
      4. Searching and Dealing with Results
    3. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  14. Chapter 14:​ Network Programming
    1. A Handful of Networking Modules
      1. The socket Module
      2. The urllib and urllib2 Modules
      3. Opening Remote Files
      4. Retrieving Remote Files
      5. Other Modules
    2. SocketServer and Friends
    3. Multiple Connections
      1. Forking and Threading with SocketServer
      2. Asynchronous I/​O with select and poll
    4. Twisted
      1. Downloading and Installing Twisted
      2. Writing a Twisted Server
    5. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  15. Chapter 15:​ Python and the Web
    1. Screen Scraping
      1. Tidy and XHTML Parsing
      2. What’s Tidy?​
      3. Getting Tidy
      4. But Why XHTML?​
      5. Using HTMLParser
      6. Beautiful Soup
    2. Dynamic Web Pages with CGI
      1. Step 1:​ Preparing the Web Server
      2. Step 2:​ Adding the Pound Bang Line
      3. Step 3:​ Setting the File Permissions
      4. CGI Security Risks
      5. A Simple CGI Script
      6. Debugging with cgitb
      7. Using the cgi Module
      8. A Simple Form
    3. Using a Web Framework
      1. Other Web Application Frameworks
    4. Web Services:​ Scraping Done Right
      1. RSS and Friends
      2. Remote Procedure Calls with XML-RPC
      3. SOAP
    5. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  16. Chapter 16:​ Testing, 1-2-3
    1. Test First, Code Later
      1. Precise Requirement Specification
      2. Planning for Change
      3. The 1-2-3 (and 4) of Testing
    2. Tools for Testing
      1. doctest
      2. unittest
    3. Beyond Unit Tests
      1. Source Code Checking with PyChecker and PyLint
      2. Profiling
    4. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  17. Chapter 17:​ Extending Python
    1. The Best of Both Worlds
    2. The Really Easy Way:​ Jython and IronPython
    3. Writing C Extensions
      1. A Swig of … SWIG
      2. What Does It Do?​
      3. I Prefer Pi
      4. The Interface File
      5. Running SWIG
      6. Compiling, Linking, and Using
      7. A Shortcut Through the Magic Forest of Compilers
      8. Hacking It on Your Own
      9. Reference Counting
      10. A Framework for Extensions
      11. Palindromes, Detartrated 1 for Your Enjoyment
    4. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  18. Chapter 18:​ Packaging Your Programs
    1. Setuptools Basics
    2. Wrapping Things Up
    3. Compiling Extensions
    4. Creating Executable Programs with py2exe
    5. A Quick Summary
      1. New Functions in This Chapter
      2. What Now?​
  19. Chapter 19:​ Playful Programming
    1. Why Playful?​
    2. The Jujitsu of Programming
    3. Prototyping
    4. Configuration
      1. Extracting Constants
      2. Configuration Files
    5. Logging
    6. If You Can’t Be Bothered
    7. If You Want to Learn More
    8. A Quick Summary
      1. What Now?​
  20. Chapter 20:​ Project 1:​ Instant Markup
    1. What’s the Problem?​
    2. Useful Tools
    3. Preparations
    4. First Implementation
      1. Finding Blocks of Text
      2. Adding Some Markup
    5. Second Implementation
      1. Handlers
      2. A Handler Superclass
      3. Rules
      4. A Rule Superclass
      5. Filters
      6. The Parser
      7. Constructing the Rules and Filters
      8. Putting It All Together
    6. Further Exploration
      1. What Now?​
  21. Chapter 21:​ Project 2:​ Painting a Pretty Picture
    1. What’s the Problem?​
    2. Useful Tools
    3. Preparations
    4. First Implementation
      1. Drawing with ReportLab
      2. Constructing Some PolyLines
      3. Writing the Prototype
    5. Second Implementation
      1. Getting the Data
      2. Using the LinePlot Class
    6. Further Exploration
      1. What Now?​
  22. Chapter 22:​ Project 3:​ XML for All Occasions
    1. What’s the Problem?​
    2. Useful Tools
    3. Preparations
    4. First Implementation
      1. Creating a Simple Content Handler
      2. Creating HTML Pages
    5. Second Implementation
      1. A Dispatcher Mix-In Class
      2. Factoring Out the Header, Footer, and Default Handling
      3. Support for Directories
      4. The Event Handlers
    6. Further Exploration
      1. What Now?​
  23. Chapter 23:​ Project 4:​ In the News
    1. What’s the Problem?​
    2. Useful Tools
    3. Preparations
    4. First Implementation
    5. Second Implementation
    6. Further Exploration
      1. What Now?​
  24. Chapter 24:​ Project 5:​ A Virtual Tea Party
    1. What’s the Problem?​
    2. Useful Tools
    3. Preparations
    4. First Implementation
      1. The ChatServer Class
      2. The ChatSession Class
      3. Putting It Together
    5. Second Implementation
      1. Basic Command Interpretation
      2. Rooms
      3. Login and Logout Rooms
      4. The Main Chat Room
      5. The New Server
    6. Further Exploration
      1. What Now?​
  25. Chapter 25:​ Project 6:​ Remote Editing with CGI
    1. What’s the Problem?​
    2. Useful Tools
    3. Preparations
    4. First Implementation
    5. Second Implementation
      1. Creating the File Name Form
      2. Writing the Editor Script
      3. Writing the Save Script
      4. Running the Editor
    6. Further Exploration
      1. What Now?​
  26. Chapter 26:​ Project 7:​ Your Own Bulletin Board
    1. What’s the Problem?​
    2. Useful Tools
    3. Preparations
    4. First Implementation
    5. Second Implementation
      1. Writing the Main Script
      2. Writing the View Script
      3. Writing the Edit Script
      4. Writing the Save Script
      5. Trying It Out
    6. Further Exploration
      1. What Now?​
  27. Chapter 27:​ Project 8:​ File Sharing with XML-RPC
    1. What’s the Problem?​
    2. Useful Tools
    3. Preparations
    4. First Implementation
      1. Implementing a Simple Node
      2. Trying Out the First Implementation
    5. Second Implementation
      1. Creating the Client Interface
      2. Raising Exceptions
      3. Validating File Names
      4. Trying Out the Second Implementation
    6. Further Exploration
      1. What Now?​
  28. Chapter 28:​ Project 9:​ File Sharing II—Now with GUI!
    1. What’s the Problem?​
    2. Useful Tools
    3. Preparations
    4. First Implementation
    5. Second Implementation
    6. Further Exploration
      1. What Now?​
  29. Chapter 29:​ Project 10:​ Do-It-Yourself Arcade Game
    1. What’s the Problem?​
    2. Useful Tools
      1. pygame
      2. pygame.​locals
      3. pygame.​display
      4. pygame.​font
      5. pygame.​sprite
      6. pygame.​mouse
      7. pygame.​event
      8. pygame.​image
    3. Preparations
    4. First Implementation
    5. Second Implementation
    6. Further Exploration
      1. What Now?​
  30. Appendix A: The Short Version
  31. The Basics
  32. Functions
  33. Objects and Stuff . . .
  34. Some Loose Ends
  35. Appendix B: Python Reference
  36. Expressions
  37. Statements
  38. Simple Statements
  39. Compound Statements
  40. Index

About the Author and About the Technical Reviewer

About the Author

A326949_3_En_BookFrontmatter_Figb_HTML.jpg

Magnus Lie Hetland is an experienced Python programmer, having used the language since the late 1990s. He is also an associate professor of computer science at the Norwegian University of Science and Technology, where he specializes in algorithm analysis and design. Hetland is the author of Python Algorithms .

About the Technical Reviewer

A326949_3_En_BookFrontmatter_Figc_HTML.jpg

Michael Thomas has worked in software development for more than 20 years as an individual contributor, team lead, program manager, and vice president of engineering. Michael has more than 10 years of experience working with mobile devices. His current focus is in the medical sector, using mobile devices to accelerate information transfer between patients and healthcare providers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset