Skip to main content

Python Projects

Python Projects

Laura Cassell, Alan Gauld

ISBN: 978-1-118-90866-2

Dec 2014

384 pages

In Stock

$45.00

Description

A guide to completing Python projects for those ready to take their skills to the next level

Python Projects is the ultimate resource for the Python programmer with basic skills who is ready to move beyond tutorials and start building projects.

The preeminent guide to bridge the gap between learning and doing, this book walks readers through the "where" and "how" of real-world Python programming with practical, actionable instruction. With a focus on real-world functionality, Python Projects details the ways that Python can be used to complete daily tasks and bring efficiency to businesses and individuals alike.

Python Projects is written specifically for those who know the Python syntax and lay of the land, but may still be intimidated by larger, more complex projects. The book provides a walk-through of the basic set-up for an application and the building and packaging for a library, and explains in detail the functionalities related to the projects. Topics include:

*How to maximize the power of the standard library modules
*Where to get third party libraries, and the best practices for utilization
*Creating, packaging, and reusing libraries within and across projects
*Building multi-layered functionality including networks, data, and user interfaces
*Setting up development environments and using virtualenv, pip, and more

Written by veteran Python trainers, the book is structured for easy navigation and logical progression that makes it ideal for individual, classroom, or corporate training.

For Python developers looking to apply their skills to real-world challenges, Python Projects is a goldmine of information and expert insight.

Related Resources

Instructor

Request an Evaluation Copy 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

Read Me
Download
Chapter 1 Code
Updated on 12/23/15
Download
Chapter 2 Code
Download
Chapter 3 Code
Download
Chapter 4 Code
Download
Chapter 5 Code
Download
Chapter 6 Code
Download
ChapterPageDetailsDatePrint Run
17Error in Text
last paragraph on the page
Currently Reads:
Python requires that literal strings be enclosed within quotation marks. Python is extremely flexible in this regard and accepts single quotes (‘Joe’), double quotes (“Joe”), triple quotes (‘‘‘Joe’’’), and triple double quotes (“““Joe”””), to delimit a string.
Should Read:
Python requires that literal strings be enclosed within quotation marks. Python is extremely flexible in this regard and accepts single quotes ('Joe'), double quotes ( Joe ), triple quotes ('''Joe'''), and triple double quotes ( Joe ) to delimit a string.
16 July 2015

119Error in Code
code at the bottom of the page
Currently Reads:
result = []
for n in range(1,11):
     if n*n % 2:
        result.append(n)
Should Read:
result = []
for n in range(1,11):
      if not n*n % 2:
          result.append(n)
17 July 2015

131Error in Code
In step 5
c2 = Circle2(42)
should read
c2 = tc.Circle2(42)
14/Aug/2015

37Text Correction: Error in Code
The bit() function definition in bits.py, on page 37, should read:
def bit(val,idx):
mask = 1 << idx # all 0 except idx
return bool(val & mask)
04/02/15

41Text Correction: Error in Code
Point 6
Currently reads:
Create a new empty __init.py__ file.
Should Read:
Create a new empty __init__.py file.
01/07/14

285Error in Code
at the top, in the code for step 5
Currently reads
>>> conf['Anne']['lending_period']
Should be followed by an additional line with '30' as such:
>>> conf['Anne']['lending_period']
'30'
14/Aug/2015

3147Text Correction: Error in Code
Chapter 3, page 147: the end of the code on this page, the 4th to last line is incorrectly indented and should be indented as follows:
owners = ('Fred','Mike','Joe','Rob','Anne','Fred')
for item in cur.execute( select id from item ).fetchall():
itemID = item[0]
cur.execute(set_owner_sql, (owners[itemID-1], itemID))
cur.close()
db.commit()
db.close
04/02/15

151Text Correction: Error in Code
On page 151, the initDB() function should read:
def initDB(filename = None):
global db, cursor
if not filename:
filename = 'lendy.db'
try:
db = sql.connect(filename)
cursor = db.cursor()
cursor.execute('PRAGMA Foreign_Keys=True')
#### THIS LINE IS MISSING ###
except:
print( Error connecting to , filename)
cursor = None
raise
04/02/15

5251Error in Code
Currently reads:
class TCPHandler(socketserver.BaseRequestHandler):
def handle(self):
self.data = self.request.recv(1024).strip()
print( {} wrote: .format(self.client_address[0]))
print(self.data)
# just send back the same data, but upper‐cased
self.request.sendall(self.data.upper())
Should Read:
class TCPHandler(socketserver.BaseRequestHandler):
def handle(self):
self.data = self.request.recv(1024).strip()
print( {} wrote: .format(self.client_address[0]))
print(self.data)
# just send back the same data, but upper‐cased
self.request.sendall(self.data.upper())
8 July 2015

6263Error in Code
(first code segment)
Currently reads:
import unittest
class PythonProjectsTest(unittest.TestCase):
eturn
Should Read:
import unittest
class PythonProjectsTest(unittest.TestCase):
pass
26 June 2015

6263Error in Code
(second code segment)
Currently Reads:
import unittest
class PythonProjectsTest(unittest.TestCase):
def test_to_fail(self):
self.failIf(False)
if __name__ == '__main__':
unittest.main()
Should Read:
import unittest
class PythonProjectsTest(unittest.TestCase):
def test_to_fail(self):
self.failIf(True)
if __name__ == '__main__':
unittest.main()
26 June 2015

6263Error in Text
(paragraph under second)
Currently reads:
In this case, you're passing in False, which will, of course, evaluate to false.
Should read:
In this case, you're passing in True, which will, of course, evaluate to true.
26 June 2015

6264Error in Code
(second to last line in last block of code on the page)
Currently Reads:
list_chars = ['m', 'd', 'z', 'l'] import unittest
Should read:
list_chars = ['m', 'd', 'z', 'l']
26 June 2015

Appendix A303Error in Code
Solution 3
The first line of code should NOT have parens round the red, orange,....violet
14/Aug/2015

Appendix A304Error in Code
solution 3
Currently reads
print('I don't like your color choice')
should say [with double quotes]
print( I don't like your color choice )
14/Aug/2015

Appendix A304Error in Code
Solution 7
In the reset() method, the line
if 0 < value < 9:
should read
if 0 <= value <= 9:
14/Aug/2015

140 & 41Errata in Code
Page 40, mid way down

Currently Reads:

def listbits(self, start=0,end=-1): end = end if end < 0 else end+2 return [int(c) for c in bin(self)[start+2:end]]
Should Read:

def listbits(self, start=0,end=None): if end: end = end if end < 0 else end+2 return [int(c) for c in bin(self)[start+2:end]]
That's a change on each of lines 1 and 2.

Also the output on page 41, top line should be changed to:

[1, 1, 1, 1, 0, 0, 0, 0]
ie. an extra zero at the end.
23-Dec-15