Beginning Python - Magnus Lie Hetland

Beginning Python (eBook)

From Novice to Professional
eBook Download: PDF
2006 | 1st ed.
XXX, 640 Seiten
Apress (Verlag)
978-1-4302-0072-7 (ISBN)
Systemvoraussetzungen
37,44 inkl. MwSt
  • Download sofort lieferbar
  • Zahlungsarten anzeigen
* Totaling 900 pages and covering all of the topics important to new and intermediate users, Beginning Python is intended to be the most comprehensive book on the Python ever written.

* The 15 sample projects in Beginning Python are attractive to novice programmers interested in learning by creating applications of timely interest, such as a P2P file-sharing application, Web-based bulletin-board, and an arcade game similar to the classic Space Invaders.

* The author Magnus Lie Hetland, PhD, is author of Apress' well-received 2002 title, Practical Python, ISBN: 1-59059-006-6. He's also author of the popular online guide, Instant Python Hacking (http://www.hetland.org), from which both Practical Python and Beginning Python are based.



A bio is not available for this author.
Beginning Python: From Novice to Professional is the most comprehensive book on the Python ever written. Based on Practical Python, this newly-revised book is both an introduction and practical reference for a swath of Python-related programming topics, including addressing language internals, database integration, network programming, and web services. Advanced topics, such as extending Python and packaging/distributing Python applications, are also covered.Ten different projects illustrate the concepts introduced in the book. You will learn how to create a P2P file-sharing application and a web-based bulletin board, and how to remotely edit web-based documents and create games. Author Magnus Lie Hetland is an authority on Python and previously authored Practical Python. He also authored the popular online guide, Instant Python Hacking, on which both books are based.

A bio is not available for this author.

Contents at a Glance 6
Contents 8
About the Author 24
About the Technical Reviewer 26
Preface 28
Introduction 30
CHAPTER 1 Instant Hacking: The Basics 32
Installing Python 32
Windows 32
Linux and UNIX 34
Macintosh 37
Other Distributions 37
Keeping In Touch and Up to Date 39
The Interactive Interpreter 39
Algo . . . What? 40
Numbers and Expressions 41
Large Integers 43
Hexadecimals and Octals 44
Variables 44
Statements 45
Getting Input from the User 46
Functions 47
Modules 48
cmath and Complex Numbers 49
Back to the __future__ 50
Saving and Executing Your Programs 50
Running Your Python Scripts from a Command Prompt 51
Making Your Scripts Behave Like Normal Programs 52
Comments 53
Strings 54
Single-Quoted Strings and Escaping Quotes 54
Concatenating Strings 55
String Representations, str and repr 56
input vs. raw_input 57
Long Strings, Raw Strings, and Unicode 57
A Quick Summary 60
New Functions in This Chapter 61
What Now? 61
CHAPTER 2 Lists and Tuples 62
Common Sequence Operations 63
Indexing 64
Slicing 65
Adding Sequences 68
Multiplication 68
Membership 70
Length, Minimum, and Maximum 71
Lists: Python’s Workhorse 71
The list Function 72
Basic List Operations 72
List Methods 74
Tuples: Immutable Sequences 80
The tuple Function 81
Basic Tuple Operations 81
So What’s the Point? 82
A Quick Summary 82
New Functions in This Chapter 83
What Now? 83
CHAPTER 3 Working with Strings 84
Basic String Operations 84
String Formatting: The Short Version 84
String Formatting: The Long Version 86
Simple Conversion 87
Width and Precision 88
Signs, Alignment, and Zero-Padding 89
String Methods 90
find 91
join 92
lower 93
replace 93
split 94
strip 94
translate 95
A Quick Summary 96
New Functions in This Chapter 97
What Now? 97
CHAPTER 4 Dictionaries: When Indices Won’t Do 98
But What Are They For? 98
Dictionary Syntax 99
The dict Function 100
Basic Dictionary Operations 100
String Formatting with Dictionaries 102
Dictionary Methods 103
clear 103
copy 104
fromkeys 105
get 105
has_key 106
items and iteritems 106
keys and iterkeys 107
pop 107
popitem 107
setdefault 107
update 108
values and itervalues 108
A Quick Summary 110
New Functions in This Chapter 110
What Now? 110
CHAPTER 5 Conditionals, Loops, and Some Other Statements 112
More About print and import 112
Printing with Commas 112
Importing Something As Something Else 113
Assignment Magic 114
Sequence Unpacking 114
Chained Assignments 115
Augmented Assignments 115
Blocks: The Joy of Indentation 116
Conditions and Conditional Statements 117
So That’s What Those Boolean Values Are For 117
Conditional Execution and the if Statement 118
else Clauses 118
elif Clauses 119
Nesting Blocks 119
More Complex Conditions 120
Assertions 124
Loops 124
while Loops 125
for Loops 126
Iterating Over Dictionaries 127
Some Iteration Utilities 127
Breaking Out of Loops 129
else Clauses in Loops 131
List Comprehension—Slightly Loopy 132
And Three for the Road 133
Nothing Happened! 133
Deleting with del 134
Executing and Evaluating Strings with exec and eval 135
A Quick Summary 138
New Functions in This Chapter 139
What Now? 139
CHAPTER 6 Abstraction 140
Laziness Is a Virtue 140
Abstraction and Structure 141
Creating Your Own Functions 142
Documenting Functions 143
Functions That Aren’t Really Functions 143
The Magic of Parameters 144
Where Do the Values Come From? 144
Can I Change a Parameter? 145
Keyword Parameters and Defaults 150
Collecting Parameters 152
Scoping 157
Rebinding Global Variables 158
Recursion 160
Two Classics: Factorial and Power 161
Another Classic: Binary Search 162
Throwing Functions Around 164
map 165
filter 165
reduce 166
apply 168
A Quick Summary 168
New Functions in This Chapter 169
What Now? 169
CHAPTER 7 More Abstraction 170
The Magic of Objects 170
Polymorphism 171
Encapsulation 174
Inheritance 177
Classes and Types 178
What Is a Class, Exactly? 178
Making Your Own Classes 179
Attributes, Functions, and Methods 180
Throwing Methods Around 181
The Class Namespace 182
Specifying a Superclass 184
Investigating Inheritance 184
Multiple Superclasses 185
Interfaces and Introspection 186
Some Thoughts on Object-Oriented Design 187
A Quick Summary 188
New Functions in This Chapter 189
What Now? 189
CHAPTER 8 Exceptions 190
What Is an Exception? 190
Making Things Go Wrong . . . Your Way 191
The raise Statement 191
Custom Exception Classes 193
Catching Exceptions 193
Look, Ma, No Arguments! 194
More Than One except Clause 195
Catching Two Exceptions with One Block 195
Catching the Object 196
A Real Catchall 196
When All Is Well 197
And Finally . . . 199
Exceptions and Functions 199
The Zen of Exceptions 200
A Quick Summary 202
New Functions in This Chapter 202
What Now? 202
CHAPTER 9 Magic Methods, Properties, and Iterators 204
Before We Begin . . . 204
Constructors 205
Overriding the Constructor 206
Calling the Unbound Superclass Constructor 208
Using the super Function 209
Item Access 210
The Basic Sequence and Mapping Protocol 211
Subclassing list, dict, and str 213
More Magic 215
Properties 215
The property Function 216
__getattr__, __setattr__, and Friends 219
Iterators 220
The Iterator Protocol 221
Making Sequences from Iterators 222
Generators 222
Making a Generator 222
A Recursive Generator 223
Generators in General 225
Avoiding Generators 225
The Eight Queens 226
Backtracking 227
The Problem 227
State Representation 227
Finding Conflicts 228
The Base Case 228
The Recursive Case 229
Wrapping It Up 231
A Quick Summary 232
New Functions in This Chapter 233
What Now? 233
CHAPTER 10 Batteries Included 234
Modules 234
Modules Are Programs 234
Modules Are Used to Define Things 236
Making Your Modules Available 238
Packages 241
Exploring Modules 242
What’s in a Module? 242
Getting Help with help 243
Documentation 244
Use the Source 245
The Standard Library: A Few Favorites 246
sys 246
os 247
fileinput 250
Sets, Heaps, and Deques 252
time 257
random 259
shelve 262
re 266
Other Interesting Standard Modules 282
A Quick Summary 283
New Functions in This Chapter 284
What Now? 284
CHAPTER 11 Files and Stuff 286
Opening Files 286
The Mode Argument 287
Buffering 288
The Basic File Methods 288
Reading and Writing 289
Reading and Writing Lines 291
Closing Your Files 292
Iterating Over File Contents 294
Doing It Byte by Byte 295
One Line at a Time 295
Reading Everything 296
Lazy Line Iteration with fileinput and xreadlines 296
The New Kids on the Block: File Iterators 297
A Quick Summary 298
New Functions in This Chapter 299
What Now? 299
CHAPTER 12 Graphical User Interfaces 300
An Example GUI Application 300
A Plethora of Platforms 301
Downloading and Installing wxPython 302
Getting Started 303
Creating Windows and Components 304
Labels and Positions 305
More Intelligent Layout 307
Event Handling 309
The Finished Program 309
But I’d Rather Use . . . 311
Using Tkinter 312
Using Jython and Swing 313
Using Something Else 314
A Quick Summary 314
What Now? 314
CHAPTER 13 Database Support 316
The Python DB API 316
Global Variables 317
Exceptions 318
Connections and Cursors 318
Types 320
Downloading and Installing pysqlite 321
Getting Started 322
An Example Database Application 322
Creating and Populating Tables 323
Searching and Dealing with Results 325
A Quick Summary 326
New Functions in This Chapter 326
What Now? 326
CHAPTER 14 Network Programming 328
A Handful of Networking Modules 328
socket 329
urllib and urllib2 331
Other Modules 332
SocketServer and Friends 333
More Information 334
Multiple Connections 334
Forking and Threading with SocketServers 336
Asynchronous I/O with select and poll 336
Twisted 339
Downloading and Installing Twisted 339
Writing a Twisted Server 340
A Quick Summary 342
New Functions in This Chapter 343
What Now? 343
CHAPTER 15 Python and the Web 344
Screen Scraping 344
Tidy and XHTML Parsing 345
Beautiful Soup 350
Dynamic Web Pages with CGI 352
Step 1. Preparing the Web Server 352
Step 2. Adding the Pound Bang Line 352
Step 3. Setting the File Permissions 353
CGI Security Risks 354
A Simple CGI Script 354
Debugging with cgitb 355
Using the cgi Module 356
A Simple Form 358
One Step Up: mod_python 359
Installing 360
CGI Handler 361
PSP 362
The Publisher 363
Web Services: Scraping Done Right 366
RSS 366
XML-RPC 368
A Quick Summary 370
New Functions in This Chapter 370
What Now? 370
CHAPTER 16 Testing, 1-2-3 372
Test First, Code Later 372
Precise Requirement Specification 373
Planning for Change 374
The 1-2-3 (and 4) of Testing 375
Tools for Testing 375
doctest 375
unittest 378
Beyond Unit Tests 381
PyChecker and PyLint 382
Profiling 384
A Quick Summary 385
New Functions in This Chapter 386
What Now? 386
CHAPTER 17 Extending Python 388
The Really Easy Way: Jython and IronPython 389
Writing C Extensions 391
A Swig of . . . SWIG 392
Hacking It on Your Own 396
A Quick Summary 401
New Functions in This Chapter 402
What Now? 402
CHAPTER 18 Packaging Your Programs 404
Distutils Basics 404
Basic Installation 405
Wrapping Things Up 407
Compiling Extensions 409
Creating Executable Programs with py2exe 410
A Quick Summary 411
New Functions in This Chapter 411
What Now? 411
CHAPTER 19 Playful Programming 412
Why Playful? 412
The Ju-Jitsu of Programming 412
Prototyping 413
Configuration 414
Extracting Constants 414
Configuration Files 415
Logging 416
If You Can’t Be Bothered 418
Project Structure 419
A Quick Summary 419
What Now? 420
CHAPTER 20 Project 1: Instant Markup 422
What’s the Problem? 422
Specific Goals 423
Useful Tools 423
Preparations 423
First Implementation 425
Adding Some Markup 426
Second Implementation 427
Handlers 428
A Handler Superclass 429
Rules 430
A Rule Superclass 431
Filters 432
The Parser 432
Constructing the Rules and Filters 433
Putting It All Together 434
Further Exploration 439
What Now? 440
CHAPTER 21 Project 2: Painting a Pretty Picture 442
What’s the Problem? 442
Specific Goals 443
Useful Tools 443
How Does It Work? 443
Preparations 443
First Implementation 444
Drawing with ReportLab 444
Constructing Some PolyLines 446
The Prototype 447
Second Implementation 448
Getting the Data 448
Using the LinePlot Class 449
Further Exploration 451
What Now? 451
CHAPTER 22 Project 3: XML for All Occasions 452
What’s the Problem? 452
Specific Goals 453
Useful Tools 453
Preparations 454
First Implementation 455
Creating a Simple Content Handler 456
Creating HTML Pages 459
Second Implementation 461
A Dispatcher Mix-In Class 461
Factoring Out the Header, Footer, and Default Handling 463
Support for Directories 463
The Event Handlers 464
Further Exploration 468
What Now? 468
CHAPTER 23 Project 4: In the News 470
What’s the Problem? 470
Specific Goals 471
Useful Tools 471
Preparations 471
First Implementation 472
Second Implementation 475
Further Exploration 483
What Now? 484
CHAPTER 24 Project 5: A Virtual Tea Party 486
What’s the Problem? 486
Specific Goals 486
Useful Tools 487
What’s It For? 487
Preparations 487
First Implementation 488
The ChatServer Class 488
The ChatSession Class 490
Putting It Together 492
Second Implementation 494
Basic Command Interpretation 494
Rooms 495
Login and Logout Rooms 496
The Main Chat Room 497
The New Server 497
Further Exploration 503
What Now? 503
CHAPTER 25 Project 6: Remote Editing with CGI 504
What’s the Problem? 504
Specific Goals 504
Useful Tools 505
Preparations 505
First Implementation 505
Second Implementation 507
index.html 507
edit.cgi 507
save.cgi 509
Running the Editor 510
Further Exploration 512
What Now? 512
CHAPTER 26 Project 7: Your Own Bulletin Board 514
What’s the Problem? 514
Specific Goals 514
Useful Tools 515
Preparations 515
First Implementation 517
Second Implementation 520
main.cgi 521
view.cgi 523
edit.cgi 524
save.cgi 525
Trying It Out 527
Further Exploration 529
What Now? 529
CHAPTER 27 Project 8: File Sharing with XML-RPC 530
What’s the Problem? 530
Specific Goals 531
Useful Tools 532
Preparations 532
First Implementation 532
Second Implementation 540
The Client Interface 540
The Exceptions 541
Validating File Names 541
Trying Out the Second Implementation 542
Further Exploration 547
What Now? 547
CHAPTER 28 Project 9: File Sharing II—Now with GUI! 548
What’s the Problem? 548
Specific Goals 548
Useful Tools 548
Preparations 549
First Implementation 549
Second Implementation 552
Further Exploration 556
Further Exploration 556
What Now? 556
CHAPTER 29 Project 10: Do-It-Yourself Arcade Game 558
What’s the Problem? 558
Specific Goals 559
Useful Tools 559
pygame 559
pygame.locals 560
pygame.display 560
pygame.font 560
pygame.sprite 561
pygame.mouse 561
pygame.event 561
pygame.image 561
Preparations 561
First Implementation 562
Second Implementation 566
Further Exploration 576
What Now? 577
APPENDIX A The Short Version 578
The Basics 578
Functions 580
Objects and Stuff . . . 581
Some Loose Ends 585
APPENDIX B Python Reference 588
Expressions 588
Statements 597
Simple Statements 597
Compound Statements 600
APPENDIX C Online Resources 602
Python Distributions 602
Python Documentation 603
Useful Toolkits and Modules 603
Newsgroups and Mailing Lists 604
Index 606

Erscheint lt. Verlag 7.11.2006
Zusatzinfo XXX, 640 p.
Verlagsort Berkeley
Sprache englisch
Themenwelt Mathematik / Informatik Informatik Programmiersprachen / -werkzeuge
Mathematik / Informatik Informatik Software Entwicklung
Schlagworte Compiler • Development • Exception • Image Processing • interfaces • language • programming • Programming language • user interface • Web Services • WINDOWS • XML
ISBN-10 1-4302-0072-3 / 1430200723
ISBN-13 978-1-4302-0072-7 / 9781430200727
Haben Sie eine Frage zum Produkt?
PDFPDF (Ohne DRM)

Digital Rights Management: ohne DRM
Dieses eBook enthält kein DRM oder Kopier­schutz. Eine Weiter­gabe an Dritte ist jedoch rechtlich nicht zulässig, weil Sie beim Kauf nur die Rechte an der persön­lichen Nutzung erwerben.

Dateiformat: PDF (Portable Document Format)
Mit einem festen Seiten­layout eignet sich die PDF besonders für Fach­bücher mit Spalten, Tabellen und Abbild­ungen. Eine PDF kann auf fast allen Geräten ange­zeigt werden, ist aber für kleine Displays (Smart­phone, eReader) nur einge­schränkt geeignet.

Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.

Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.

Mehr entdecken
aus dem Bereich
Das umfassende Handbuch

von Johannes Ernesti; Peter Kaiser

eBook Download (2023)
Rheinwerk Computing (Verlag)
31,43
Das Handbuch für Webentwickler

von Philip Ackermann

eBook Download (2023)
Rheinwerk Computing (Verlag)
34,93
Deterministische und randomisierte Algorithmen

von Volker Turau; Christoph Weyer

eBook Download (2024)
De Gruyter (Verlag)
64,95