30,99 €
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.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 630
Veröffentlichungsjahr: 2014
Introduction
Why We Wrote This Book
Who This Book For
What You Will Learn
What You Need to Use This Book
Conventions
Source Code
Errata
p2p.wrox.com
Chapter 1: Reviewing Core Python
Exploring the Python Language and the Interpreter
Reviewing the Python Data Types
Using Python Control Structures
Getting Data In and Out of Python
Extending Python
Creating and Using Modules and Packages
Creating an Example Package
Using Third-Party Packages
Summary
Chapter 2: Scripting with Python
Accessing the Operating System
Working with Dates and Times
Handling Common File Formats
Accessing Native APIs with ctypes and pywin32
Automating Tasks Involving Multiple Applications
Summary
Chapter 3: Managing Data
Storing Data Using Python
Analyzing Data with Python
Managing Data Using SQL
Migrating LendyDB to an SQL Database
Exploring Other Data Management Options
Summary
Chapter 4: Building Desktop Applications
Structuring Applications
Building Command-Line Interfaces
Using the cmd Module to Build a Command-Line Interface
Reading Command-Line Arguments
Jazzing Up the Command-Line Interface with Some Dialogs
Programming GUIs with Tkinter
Storing Local Data
Understanding Localization
Summary
Chapter 5: Python on the Web
Python on the Web
Web Programming with Python
More on Python and the Web
Using Python Across the Wire
More Networking Fun in Python
Summary
Exercises
Chapter 6: Python in Bigger Projects
Testing with the Doctest Module
Testing with the Unittest Module
Test-Driven Development in Python
Debugging Your Python Code
Working on Larger Python Projects
Releasing Python Packages
Summary
Chapter 7: Exploring Python’s Frontiers
Drawing Pictures with Python
Doing Science with Python
Playing Games with Python
Going to the Movies
Integrating with Other Languages
Getting Physical
Building Python
Summary
Appendix A: Answers to Exercises
Appendix B: Python Standard Modules
Appendix C: Useful Python Resources
Asking Questions: Mailing Lists and More
Reading Blogs
Studying Tutorials and References
Watching Videos
And Now for Something Completely Different…
References
Title Page
Copyright
Dedication
About the Authors
About the Technical Editors
Credits
Acknowledgments
Advert
Wiley End User License Agreement
Chapter 1
Table 1.1
Table 1.2
Table 1.3
Table 1.4
Table 1.5
Chapter 2
Table 2.1
Chapter 3
Table 3.1
Table 3.2
Table 3.3
Table 3.4
Table 3.5
Table 3.6
Chapter 4
Table 4.1
Chapter 2
Figure 2.1 The toolhire spreadsheet
Chapter 4
Figure 4.1 Module interaction sequence diagram
Figure 4.2 Initial message box screenshot
Figure 4.3 Example of a GUI containment tree
Figure 4.4 demo1.py in action
Figure 4.5 Tic-tac-toe GUI design
Figure 4.6 Final Tkinter GUI
Figure 4.7 A Tix ComboBox
Figure 4.8 A Tix ScrolledText Widget in Action
Figure 4.9 Modifying Text appearance in a text widget
Figure 4.10 Tix Notebook showing two pages
Figure 4.11 Various ttk Themes
Chapter 5
Figure 5.1 Note the two sides of a web app: client-side and server-side. Server-side data is served via a web server such as Apache or Nginx.
Figure 5.2 Structure of a network request
Figure 5.3 The Chrome Developer Tools, illustrating the headers of a server request for a file
Figure 5.4 The DevTools Network tab
Figure 5.5 FigurePreview of the selected file
Figure 5.6 Headers for the selected file
Figure 5.7 The Headers tab in Chrome’s DevTools, illustrating the HTTP response from the USDA website
Figure 5.8 Note how this displays the HTML file we created earlier.
Cover
Table of Contents
Chapter 1
xxv
xxvi
xxvii
xxviii
xxix
xxx
1
3
4
5
6
7
8
10
11
13
14
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
34
35
36
37
38
39
40
41
42
43
44
45
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
70
71
73
74
75
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
96
97
100
101
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
127
128
129
130
131
132
133
135
136
137
138
139
140
142
143
144
145
146
147
148
149
151
152
153
154
155
156
157
158
161
162
163
165
166
167
168
169
170
171
172
173
174
175
176
178
179
180
181
184
185
186
187
188
189
191
193
194
195
196
197
198
199
200
202
204
205
206
207
208
211
213
214
215
217
218
219
220
221
223
224
225
228
231
232
233
234
236
237
238
239
240
241
242
243
244
245
247
248
250
251
252
253
255
257
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
285
288
289
293
294
295
296
297
298
299
300
303
304
306
307
308
309
310
312
313
316
319
320
321
322
323
324
325
326
327
328
329
331
332
333
334
335
336
337
338
339
AFTER A CONFERENCE ONE YEAR, an e-mail went around the PyLadies organizers mailing list asking, “Is anyone interested in writing a Python book?” I had kicked around the idea of a programming book for a while. After teaching for a couple years and mentoring at PyLadies and other coding meetups, I realized there was a need for a new, specific sort of programming book. I didn’t jump to replying to the e-mail, however. I knew that writing a book would be a big process (boy is it!) and that it would take a lot of time and effort on my part, in the way of working on the weekends and holidays (yep, check!). I also knew that I had a full-time job teaching programming, I was the lead organizer for my local PyLadies chapter in Atlanta, Georgia, and I had children that would soon start asking, “Are you writing this weekend?”
All of the above was true (a little more than I originally thought, actually), but I knew that the book was important. There were so many of my students asking me at the end of class, “Now that I know the basics of Python, what do I do?” My answer was always something along the lines of, “You can get involved in open-source projects!” or “Take the advanced Python class!” But none of those answers satisfied them or me. The answer is, “You have to really start looking for something to work on—a problem to solve, a need that must be met.” Because, the only way to really know and understand programming and a programming language is to solve problems with said language.
But then the problem of “But I don’t have a problem that really needs to be solved” cropped up. So while I could send my students off to look at open-source projects that do, in fact, need the help, if they didn’t understand the technology, they’d be lost and give up. Then the community loses yet another programmer who may have brought interesting things to the table. So, that’s when, after lots of talking to friends and family, I realized that this book needed to be written.
For all those people who came up to us and asked, over the years, “What can I do now that I understand Python basics? What things can I learn? Where do I go?” That’s why we wrote this book.
The most chronic problem in programming books that we’ve experienced and that others have also felt they experienced is that it goes from “These are the basics of a language” into very deep concepts that only people who hold Computer Science (CS) degrees would understand. And that’s not cool. Programming should be open to anyone who is interested. We should all be working toward making the bar into programming a little lower. We feel that Python accomplishes this, but we need to take it a step further and begin to understand how people learn abstract ideas and concepts, to help us help them learn.
Think of programming like learning how to build a house, but only understanding that wood is needed and how the wood works to build a house. You still need to understand structural engineering, electrical, plumbing, ventilation, HVAC, etc. The same concept is true for programming. Languages just explain the wood being used in a house. There is plenty more that is happening in harmony with the wood, and we want to help you uncover those concepts.
This book is not for beginners who want to learn Python. Rather, as a reader of this book, you need to already have some Python programming under your belt. That means you’ve done some tutorials. You also understand that whitespace matters in Python and that lists are denoted with hard braces ([ ] ) while dicts (dictionaries) are denoted with curly braces ( { } ). This book is for those people who are still beginners, but who have completed a tutorial or two—folks who understand the basics of Python, but are interested to learn what all they can do with Python.
“Need is the mother of invention” goes the saying, and when you’re learning to program, this is very true. If you need a piece of software that can perform a specific function or task, it’s easy to learn a language around that need. You have a need, the language will help you, you learn the language, you solve the problem, you’ve learned a thing, and you’ve put it to use immediately. This is awesome and fantastic! However, what if you think programming is interesting, but you’re missing the need? What if you don’t know what to make? That’s where this book comes in.
This book will help you to learn the parts of Python that most people don’t think to tell new programmers about. Most of the things covered in this book are tools and technologies that one may only discover when they are faced with working with them. However, for new programmers who don’t have a specific problem to solve, learning these tools can be difficult. Most of the time no one thinks to introduce these topics to programmers because they are used so regularly. We hope to take you on a journey through the power of Python and all of its splendor.
You will learn how to make a web app, how to talk to a database using Python libraries, and which system tools can help speed up your workflow, if you’re a systems administrator. We will briefly touch on topics such as security and best practices. You’ll get an overview of creating graphical user interfaces (GUIs) using Python libraries. We will cover consuming and producing application programming interfaces (APIs) and many other topics that are beneficial to Python programmers.
We hope to take you on a small tour of the basics that are available in the Python ecosystem. We’ll introduce you to many concepts that are usually discovered only while working on a problem to solve. While we can’t put everything into the context of problems that you may need to solve in the future, we hope that we can illustrate the powerful features of the Python language and the available packages and technology that are available to you, the new Python programmer.
We will start out with a brief “crash course” in Python, in case you’ve forgotten anything. We’ll go over the basics, and then you can decide if you want to read that chapter in its entirety or not. Next, we go over Python as a scripting language. You’ll get to get your hands dirty, as it were, by writing small scripts to access parts of your system, using Python. This should illustrate the very basic power you have with the language. Third, we’ll start talking about data, which is what programming is all about—manipulating data. You’ll get to dive in and work through some examples using some of the standard libraries that come out of the box with Python. We’ll even discuss databases so that you can get a quick intro into those. We want you to see and touch every part of a system that you may come in contact with.
After the first three chapters, you’ll dive into desktop applications. While these aren’t incredibly popular in Python, it is a feature of the language, and it could be useful down the line in your tenure as a Python programmer. Next, we will step out of the desktop and onto the Internet with Python as a data communicator. You’ll learn all about HTTP and the Web and how websites work under the hood. You’ll even be able to play with producing and consuming APIs. APIs confuse many new programmers; we hope to have removed much of the mystery with this chapter.
In the final chapters, we’ll show more advanced topics in Python, such as how to work with Python in bigger projects, debugging your code, creating testing-harnesses, handling errors, and even creating your own exceptions and exception handlers! Finally, there are appendices for reference while you are going through the book and after, when you’re spreading your Python wings and programming.
It is a lot of information, and it’s like a huge sandbox of tools and ideas to get you started on your journey with the language. We hope that you try things out and research more on your own time with certain concepts and ideas that interest you. We’ve included plenty of hands-on exercises to help you try out the concepts as they are presented, as well as some challenge questions in most chapters to help you exercise your newfound knowledge.
In order to get the most out of this book, we recommend you have a modern computer running Python 3.3 or later, a good text editor that you are comfortable using, an Internet connection (for some parts of the book), and a healthy dose of patience and wonderment. We also recommend that you utilize Internet searching for any problems that may arise. Professional programmers don’t actually know it all; they usually only know those problems that they deal with day in and day out. A lot of the time of a professional programmer is spent researching and tracking down why a behavior is occurring. No one should ever feel bad for relying on a Google search to solve a problem. Sometimes, your Googling abilities are just as important as your programming ones.
To work through the examples and projects in this book, you will also need the source code. The source code for the samples is available for download from the Wrox website at:
www.wrox.com/go/pythonprojects
To help you get the most from the text and keep track of what’s happening, we’ve used a number of conventions throughout the book.
The Try It Out is an exercise you should work through, following the text in the book.
They usually consist of a set of steps.
Each step has a number.
Follow the steps through with your copy of the database.
After each Try It Out, the code you’ve typed will be explained in detail.
WARNING Warnings hold important, not-to-be-forgotten information that is directly relevant to the surrounding text.
NOTE Notes indicate notes, tips, hints, tricks, or asides to the current discussion.
As for styles in the text:
We highlight new terms and important words when we introduce them.
We show keyboard strokes like this: Ctrl+A.
We show filenames, URLs, and code within the text like so:
persistence.properties
.
We present code in two different ways:
We use a monofont type with no highlighting for most code examples.
We use bold to emphasize code that is particularly important in the present context or to show changes from a previous code snippet.
As you work through the examples in this book, you may choose either to type in all the code manually, or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com. Specifically for this book, the code download is on the Download Code tab at:
www.wrox.com/go/pythonprojects
You can also search for the book at www.wrox.com by ISBN (the ISBN for this book is 978-1-118-90866-2 to find the code. And a complete list of code downloads for all current Wrox books is available at www.wrox.com/dynamic/books/download.aspx.
Most of the code on www.wrox.com is compressed in a .ZIP, .RAR archive, or similar archive format appropriate to the platform. Once you download the code, just decompress it with an appropriate compression tool.
We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time, you will be helping us provide even higher quality information.
To find the errata page for this book, go to:
www.wrox.com/go/pythonprojects
And click the Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors.
If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.
For author and peer discussion, join the P2P forums at http://p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.
At http://p2p.wrox.com, you will find a number of different forums that will help you, not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:
Go to
http://p2p.wrox.com
and click the Register link.
Read the terms of use and click Agree.
Complete the required information to join, as well as any optional information you wish to provide, and click Submit.
You will receive an e-mail with information describing how to verify your account and complete the joining process.
You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.
Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.
WHAT YOU WILL LEARN IN THIS CHAPTER:
The basic features of the Python language
How to use the Python module mechanism
How to create a new module
How to create a new package
WROX.COM CODE DOWNLOADS FOR THIS CHAPTER
You can find the wrox.com downloads for this chapter at www.wrox.com/go/pythonprojects on the Download Code tab. The code is in the Chapter 1 download and individually named according to the names throughout the chapter.
This chapter starts with a brief review of Python—in case you have forgotten some of the basics—and provides a foundation upon which the rest of the book is built. If you are confident in your ability with basic Python coding, feel free to skip ahead until you see content that might be of interest to you. After all, you can always come back to this chapter later if you find you need a refresher.
In this chapter you start off by looking at the Python ecosystem, the data types, and the major control structures and then move on to defining functions and classes. Next, you look at the Python module and package system. And, finally, you create a basic new package of modules.
By the end of this chapter, you should be ready to take the next step and start working with the standard Python modules on real project tasks.
Python is a dynamic but strictly typed programming language. It is both interpreted and compiled in that the original source code is compiled into byte code and then interpreted, but this happens transparently to the user; you do not have to explicitly ask Python to compile your code.
The Python language has several implementations, but the most common is the version written in C, often referred to as CPython. Other implementations include Jython, written in Java, and IronPython, written for the Microsoft .NET platform. CPython is the implementation used in this book.
NOTE At the time of writing, there are two version streams of Python: versions 2.x and 3.x. This book focuses on version 3, and the code has been tested on several releases within that stream—up to release 3.4. Where major compatibility issues arise with 2.x, reference will be made to version 2.7.
Python programs are written in text files that customarily have the extension .py. The Python interpreter, called python (in lowercase) does not actually care about the extension; it is only for the user’s benefit (and in some operating systems to allow the file and interpreter to be linked).
You can also input Python code directly to the interpreter. This method makes for a highly interactive development style where ideas are prototyped or tested in the interpreter and then transferred into a code editor. The Python interpreter is a powerful learning tool when you are starting to use a new concept or code module.
When working in this mode, you start the interpreter by typing python at an operating system command prompt. The system will respond with a message telling you the Python version and some build details, followed by the interactive prompt at which you type code. It looks like this:
ActivePython 3.3.2.0 (ActiveState Software Inc.) based on Python 3.3.2 (default, Sep 16 2013, 23:10:06) [MSC v.1600 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>>
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
