Luke Sneeringer

Professional Python


Скачать книгу

Luke Sneeringer

      Professional Python

      Introduction

      This book introduces the reader to more advanced Python programming by providing an intermediate course in the Python language.

      Recently, Python has become more and more frequently the developer's language of choice. It is used all over the world, for myriad purposes. As adoption continues to increase, more and more developers are spending their days writing Python.

      Python has grown so steadily precisely because it is a very powerful language, and even many seasoned Python developers have only scratched the surface of what the language is capable of doing.

      Who This Book Is For

      This book is for developers who have already worked in Python, are already familiar with the language, and desire to learn more about it. This book assumes that readers have already done most basic tasks involved with developing in Python (such as having used the Python interactive terminal).

      If you are a reader who seeks a general survey of intermediate to advanced Python language features, you should read this book from start to finish.

      Alternatively, you may be a reader who has used some more-advanced language features in passing, or potentially needs to maintain code that uses such features. Consider using this book as a reference guide or index to flesh out your understanding when you are grappling with a particular implementation.

      What This Book Covers

      This book covers all recent versions of Python (including both Python 2 and Python 3). At the time of this writing, the most recent version available is Python 3.4, and Python 3.5 is in beta. This book primarily covers Python 2.6, 2.7, 3.3, and 3.4. Most code is provided in a manner that will run on both Python 2 and Python 3, with Python 2 code specifically noted as such.

      Additionally, this book includes a chapter with a deep dive into distinctions between Python 2 and Python 3, which provides advice on writing code to run on multiple versions of Python, as well as porting over to Python 3.

      This book primarily focuses on two areas. The first is features of the language itself. For example, this book includes several chapters about various aspects of how Python's class and object model works. The second area is modules provided as part of the standard library. For example, this book includes a chapter each on modules such as asyncio, unittest, and argparse.

      How This Book Is Structured

      This book is essentially divided into four parts.

      The first three chapters in the book are fundamentally about functions in Python. This part includes a chapter each on decorators and context managers, which are reusable ways to modify or wrap functions to add functionality. It also includes a chapter on generators, which are a way to design functions that yield values one at a time, rather than creating an entire list of values in advance and returning them in one block.

      The second part comprises the next four chapters, and they are all related somehow to Python classes and the language's object model. There is a chapter on magic methods. Then, there is a chapter each on metaclasses and class factories, which are two approaches to constructing classes in powerful ways. Finally, a chapter on abstract base classes explains the abc module and how to make classes declare patterns that they implement.

      The third part comprises two chapters about strings and data. There is a chapter on how to navigate using Unicode strings (as opposed to byte strings) in Python, which also covers in detail how strings differ between Python 2 and Python 3. There is also a chapter on regular expressions, which covers the Python re module as well as how to write regular expressions.

      Finally, the fourth part covers everything that does not neatly fit into one of the first three parts. This part begins with an in-depth look at the distinctions between Python 2 and Python 3, and how to write code that is interoperable with both. There is a chapter on unit testing, focusing on the unittest module. A chapter on command-line interface (CLI) tools teaches you about both optparse and argparse, which are Python's modules for writing command-line tools. There is a chapter on asyncio, which is a new asynchronous programming library that was added to the standard library in Python 3.4. Finally, the book closes with a chapter on style.

      What You Need to Use This Book

      You will, first and foremost, need a machine running Python.

      Although it does not make a difference in most chapters, this book is slightly Linux-focused in its approach (this will be most relevant in the chapter on CLI tools). Examples were run in a Linux environment, and output may vary slightly on Windows.

      Conventions

      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.

      Warning

      Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.

      Note

      Notes, tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.

      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 as follows:

      Errata

      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 to provide even higher quality information.

      To find the errata page for this book, go to http://www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page, you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list (including links to each book's errata) is also available at www.wrox.com/misc-pages/booklist.shtml.

      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.

      p2p.wrox.com

      For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based system for you to post messages relating to Wrox books and related technologies, and to 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. Some 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 most Wrox books, but also as you develop your own applications. To join the forums, just follow these steps:

      1. Go to p2p.wrox.com and click the Register link.

      2. Read