Preface

The Python programming language reconciles many apparent contradictions: both elegant and pragmatic, both simple and powerful, it’s very high-level yet doesn’t get in your way when you need to fiddle with bits and bytes, and it’s suitable for programming novices as well as great for experts, too.

This book is aimed at programmers with some previous exposure to Python, as well as experienced programmers coming to Python for the first time from other languages. The book is a quick reference to Python itself, the most commonly used parts of its vast standard library, and a few of the most popular and useful third-party modules and packages, covering a wide range of application areas, including web and network programming, XML handling, database interactions, and high-speed numeric computing. The book focuses on Python’s cross-platform capabilities and covers the basics of extending Python and embedding it in other applications.

How This Book Is Organized

This book has five parts, as follows.

Part I, Getting Started with Python

Chapter 1, Introduction to Python

Covers the general characteristics of the Python language, its implementations, where to get help and information, how to participate in the Python community, and how to obtain and install Python on your computer(s).

Chapter 2, The Python Interpreter

Covers the Python interpreter program, its command-line options, and how to use it to run Python programs and in interactive sessions. The chapter mentions text editors for editing Python programs and auxiliary programs for checking your Python sources, along with some full-fledged integrated development environments, including IDLE, which comes free with standard Python. The chapter also covers running Python programs from the command line.

Part II, Core Python Language and Built-ins

Chapter 3, The Python Language

Covers Python syntax, built-in data types, expressions, statements, control flow, and how to write and call functions.

Chapter 4, Object-Oriented Python

Covers object-oriented programming in Python.

Chapter 5, Exceptions

Covers how to use exceptions for errors and special situations, and logging.

Chapter 6, Modules

Covers how Python lets you group code into modules and packages, how to define and import modules, and how to install third-party Python packages. The chapter also covers working with virtual environments to isolate project dependencies.

Chapter 7, Core Built-ins and Standard Library Modules

Covers built-in data types and functions, and some of the most fundamental modules in the standard Python library (roughly, the set of modules supplying functionality that, in some other languages, is built into the language itself).

Chapter 8, Strings and Things

Covers Python’s string-processing facilities, including Unicode strings, byte strings, and string literals.

Chapter 9, Regular Expressions

Covers Python’s support for regular expressions.

Part III, Python Library and Extension Modules

Chapter 10, File and Text Operations

Covers how to deal with files and text processing using many modules from Python’s standard library and platform-specific extensions for rich text I/O. The chapter also covers issues of internationalization and localization, and the specific task of defining interactive text-mode command sessions with Python.

Chapter 11, Persistence and Databases

Covers Python’s serialization and persistence mechanisms, as well as Python’s interfaces to DBM databases and relational (SQL-based) databases, particularly the handy SQLite that comes with Python’s standard library.

Chapter 12, Time Operations

Covers how to deal with times and dates in Python, using the standard library and popular third-party extensions.

Chapter 13, Controlling Execution

Helps you to achieve advanced execution control in Python, including execution of dynamically generated code and control of garbage collection. The chapter also covers some Python internal types, and the specific issue of registering “clean-up” functions to execute at program-termination time.

Chapter 14, Threads and Processes

Covers Python’s functionality for concurrent execution, both via multiple threads running within one process and via multiple processes running on a single machine. The chapter also covers how to access the process’s environment, and how to access files via memory-mapping mechanisms.

Chapter 15, Numeric Processing

Shows Python’s features for numeric computations, both in standard library modules and in third-party extension packages; in particular, the chapter covers how to use decimal numbers or fractions, instead of the default binary floating-point numbers. The chapter also covers how to get and use pseudorandom and truly random numbers, and how to speedily process whole arrays (and matrices) of numbers.

Chapter 16, Testing, Debugging, and Optimizing

Deals with Python tools and approaches that help ensure your programs are correct (i.e., that your programs do what they’re meant to do), find and fix errors in your programs, and check and enhance your programs’ performance. The chapter also covers the concept of “warning” and the Python library module that deals with it.

Part IV, Network and Web Programming

Chapter 17, Networking Basics

Covers the basics of networking with Python.

Chapter 18, Asynchronous Alternatives

Covers alternatives for asynchronous (event-driven) programming, particularly modern coroutine-based architectures and lower-level multiplexing, with standard library modules.

Chapter 19, Client-Side Network Protocol Modules

Covers modules in Python’s standard library to write network client programs, particularly for dealing with various network protocols from the client side, sending and receiving emails, and handling URLs.

Chapter 20, Serving HTTP

Covers how to serve HTTP for “web applications” in Python, using popular third-party lightweight Python frameworks leveraging Python’s WSGI standard interface to web servers.

Chapter 21, Email, MIME, and Other Network Encodings

Covers how to process email messages, and other network-structured and encoded documents, in Python.

Chapter 22, Structured Text: HTML

Covers popular third-party Python extension modules to process, modify, and generate HTML documents.

Chapter 23, Structured Text: XML

Covers Python library modules and popular extensions to process, modify, and generate XML documents.

Part V, Extending, Distributing, v2/v3 Migration

Chapter 24, Extending and Embedding Classic Python

Covers how to code Python extension modules using Python’s C API and Cython.

Chapter 25, Distributing Extensions and Programs

Covers popular tools and modules to package and distribute Python extensions, modules, and applications.

Chapter 26, v2/v3 Migration and Coexistence

Covers modules and best practices to help you code for both v2 and v3, how to best migrate existing code from v2 to v3, and how to retain portability between versions.

Conventions Used in This Book

The following conventions are used throughout this book.

Reference Conventions

In the function/method reference entries, when feasible, each optional parameter is shown with a default value using the Python syntax name=value. Built-in functions need not accept named parameters, so parameter names may not be significant. Some optional parameters are best explained in terms of their presence or absence, rather than through default values. In such cases, we indicate that a parameter is optional by enclosing it in brackets ([]). When more than one argument is optional, brackets can be nested.

Version Conventions

This book covers Python 3.5, referred to as v3, and Python 2.7, referred to as v2. Highlights of Python 3.6 (released as this book was about to go to print) are called out as 3.6. All examples in the book assume you have run from __future__ import print_function if you are using v2 (see the note Imports from __future__).

Typographic Conventions

Italic

Used for filenames, program names, URLs, and to introduce new terms.

Bold italic

Used for New in 3.6 highlights.

Constant width

Used for code examples, as well as for all items that appear in code, including keywords, methods, functions, classes, and modules.

Constant width italic

Used to show text to be replaced with user-supplied values in code examples.

Constant width bold

Used for commands to be typed at a system command line and to indicate code output.

How to Contact Us

We have tested and verified the information in this book to the best of our ability, but you may find that features have changed (or even that we have made mistakes!). Please let the publisher know about any errors you find, as well as your suggestions for future editions, by writing to:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/pythonNutshell_3E.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

O’Reilly Safari

Note

Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, government, educators, and individuals.

Members have access to thousands of books, training videos, Learning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others.

For more information, please visit http://oreilly.com/safari.

Acknowledgments

Thanks to our thorough and conscientious reviewers and editor! All errors that remain are entirely our own.

Many thanks to our editor, Dawn Schanafelt, for gently guiding, nudging, and pushing us along to get this done! Also, thanks to Kim Cofer, our excellent copyeditor, whose eye for detail caught not just grammatical issues, but also some technical errors.

Thanks to our hard-working tech reviewers: Brett Cannon, Luciano Ramalho, and Victoria Wong, who read through every explanation and example in the book’s draft. Without them, this book would not have been nearly as clear or accurate.

Thanks as well to our several unofficial reviewers: Tim Golden, for his invaluable help with Python for Windows coverage; Nick Coghlan and Donald Stufft, for their help untangling the intricacies of Python packaging; Łukasz Langa, for his copious feedback on asynchronous programming; Peter Norvig, for his precious, detailed feedback and advice about the entire book; Daniel Pyrathon, for his review of the chapter on HTTP serving frameworks; and George Gillan, for reporting many typos and unclear explanations in Early Release editions of this book.

And finally, thanks to Google for its useful Hangouts app and other G Suite online collaboration tools, without which our intense communication and coordination among authors in different continents would have been far less easy and efficient.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset