Topic: Open Source

Python Projects (111890866X) cover image

Python Projects

Laura Cassell, Alan Gauld
ISBN: 978-1-118-90866-2
Paperback
384 pages
December 2014
Other Available Formats: Wiley E-Text
If you are an instructor, you may request an evaluation copy for this title.
Paperback Version: US $45.00 Add to Cart

About This Title  |  Download Code  |  Errata  |  P2P Forum for This Title
INTRODUCTION xxv

CHAPTER 1: REVIEWING CORE PYTHON 1

Exploring the Python Language and the Interpreter 2

Reviewing the Python Data Types 3

Numeric Types: Integer and Float 4

The Boolean Type 5

The None Type 6

Collection Types 6

Strings 7

Bytes and ByteArrays 8

Tuples 10

Lists 10

Dictionaries 12

Sets 13

Using Python Control Structures 15

Structuring Your Program 15

Using Sequences, Blocks and Comments 16

Selecting an Execution Path 17

Iteration 18

Handling Exceptions 20

Managing Context 21

Getting Data In and Out of Python 21

Interacting with Users 21

Using Text Files 23

Extending Python 24

Defining and Using Functions 24

Generator Functions 26

Lambda Functions 27

Defining and Using Classes and Objects 28

Creating and Using Modules and Packages 33

Using and Creating Modules 33

Using and Creating Packages 34

Creating an Example Package 36

Using Third?]Party Packages 42

Summary 43

CHAPTER 2: SCRIPTING WITH PYTHON 47

Accessing the Operating System 48

Obtaining Information About Users and Their Computer 49

Obtaining Information About the Current Process 53

Managing Other Programs 55

Managing Subprocesses More Effectively 58

Obtaining Information About Files (and Devices) 60

Navigating and Manipulating the File system 62

Plumbing the Directory Tree Depths 69

Working with Dates and Times 72

Using the time Module 72

Introducing the datetime Module 75

Introducing the calendar Module 76

Handling Common File Formats 76

Using Comma?]Separated Values 76

Working with Confi g Files 83

Working with XML and HTML Files 86

Parsing XML Files 86

Parsing HTML Files 89

Accessing Native APIs with ctypes and pywin32 93

Accessing the Operating System Libraries 94

Using ctypes with Windows 95

Using ctypes on Linux 96

Accessing a Windows Application Using COM 96

Automating Tasks Involving Multiple Applications 98

Using Python First 98

Using Operating System Utilities 98

Using Data Files 98

Using a Third?]Party Module 99

Interacting with Subprocesses via a CLI 99

Using Web Services for Server?]Based Applications 99

Using a Native Code API 100

Using GUI Robotics 100

Summary 100

CHAPTER 3: MANAGING DATA 103

Storing Data Using Python 104

Using DBM as a Persistent Dictionary 104

Using Pickle to Store and Retrieve Objects 109

Accessing Objects with shelve 111

Analyzing Data with Python 116

Analyzing Data Using Built?]In Features of Python 116

Analyzing Data with ittertools 119

Utility Functions 119

Data Processing Functions 121

Taming the Vagaries of groupby() 122

Using itertools to Analyze LendyDB Data 124

Managing Data Using SQL 125

Relational Database Concepts 126

Structured Query Language 127

Creating Tables 128

Inserting Data 129

Reading Data 130

Modifying Data 133

Linking Data Across Tables 134

Digging Deeper into Data Constraints 134

Revisiting SQLite Field Types 135

Modeling Relationships with Constraints 136

Many?]to?]Many Relationships 140

Migrating LendyDB to an SQL Database 143

Accessing SQL from Python 143

Using SQL Connections 143

Using a Cursor 143

Creating the LendyDB SQL Database 145

Inserting Test Data 146

Creating a LendyDB API 148

Exploring Other Data Management Options 154

Client?]Server Databases 154

NoSQL 155

The Cloud 155

Data Analysis with RPy 156

Summary 157

CHAPTER 4: BUILDING DESKTOP APPLICATIONS 161

Structuring Applications 162

Building Command-Line Interfaces 164

Building the Data Layer 164

Building the Core Logic Layer 165

Building the User Interface 169

Using the cmd Module to Build a Command-Line Interface 173

Reading Command-Line Arguments 175

Jazzing Up the Command-Line Interface with Some Dialogs 177

Programming GUIs with Tkinter 181

Introducing Key GUI Principles 181

Event?]Based Programming 181

GUI Terminology 182

The Containment Tree 183

Building a Simple GUI 184

Building a Tic?]Tac?]Toe GUI 186

Sketching a UI Design 186

Building Menus 187

Building a Tic?]Tac?]Toe Board 188

Connecting the GUI to the Game 190

Extending Tkinter 194

Using Tix 194

Using ttk 198

Revisiting the Lending Library 199

Exploring Other GUI Toolkits for Python 206

wxPython 207

PyQt 207

PyGTK 208

Native GUIs: Cocoa and PyWin32 209

Dabo 209

Storing Local Data 210

Storing Application?]Specifi c Data 210

Storing User?]Selected Preferences 211

Storing Application State 212

Logging Error information 212

Understanding Localization 214

Using Locales 214

Using Unicode in Python 216

Using gettext 218

Summary 220

CHAPTER 5: PYTHON ON THE WEB 223

Python on the Web 224

Parts of a Web Application 225

The Client?]Server Relationship 226

Middleware and MVC 226

HTTP Methods and Headers 227

What Is an API? 230

Web Programming with Python 235

Using the Python HTTP Modules 235

Creating an HTTP Server 235

Exploring the Flask Framework 237

Creating Data Models in Flask 238

Creating Core Flask Files 239

More on Python and the Web 247

Static Site Generators 248

Web Frameworks 248

Using Python Across the Wire 248

XML?]RPC 249

Socket Servers 250

More Networking Fun in Python 253

Summary 254

CHAPTER 6: PYTHON IN BIGGER PROJECTS 257

Testing with the Doctest Module 258

Testing with the Unittest Module 262

Test?]Driven Development in Python 267

Debugging Your Python Code 267

Handling Exceptions in Python 272

Working on Larger Python Projects 276

Releasing Python Packages 280

Summary 282

CHAPTER 7: EXPLORING PYTHON’S FRONTIERS 285

Drawing Pictures with Python 286

Using Turtle Graphics 286

Using GUI Canvas Objects 286

Plotting Data 287

Using imghdr 287

Introducing Pillow 287

Trying Out ImageMagick 287

Doing Science with Python 288

Introducing SciPy 288

Doing Bioscience with Python 290

Using GIS 290

Watching Your Language 290

Getting It All 290

Playing Games with Python 291

Enriching the Experience with PyGame 291

Exploring Other Options 291

Going to the Movies 292

The Computer Graphics Kit 292

Modeling and Animation 292

Photo Processing 292

Working with Audio 293

Integrating with Other Languages 293

Jython 293

IronPython 294

Cython 294

Tcl/Tk 295

Getting Physical 296

Introducing Serial Options 296

Programming the RaspberryPi 296

Talking to the Arduino 297

Exploring Other Options 297

Building Python 298

Fixing Bugs 298

Documenting 298

Testing 299

Adding Features 299

Attending Conferences 299

Summary 299

APPENDIX A: ANSWERS TO EXERCISES 303

Chapter 1 Solutions 303

Chapter 2 Solutions 305

Chapter 3 Solutions 306

Chapter 4 Solutions 311

Chapter 5 Solutions 315

Chapter 6 Solutions 316

Chapter 7 Solutions 317

APPENDIX B: PYTHON STANDARD MODULES 319

APPENDIX C: USEFUL PYTHON RESOURCES 327

Asking Questions: Mailing Lists and More 327

Reading Blogs 328

Studying Tutorials and References 328

Watching Videos 329

And Now for Something Completely Different… 329

REFERENCES 331

INDEX 333