Book Image

Modern Python Standard Library Cookbook

By : Alessandro Molina
Book Image

Modern Python Standard Library Cookbook

By: Alessandro Molina

Overview of this book

The Python 3 Standard Library is a vast array of modules that you can use for developing various kinds of applications. It contains an exhaustive list of libraries, and this book will help you choose the best one to address specific programming problems in Python. The Modern Python Standard Library Cookbook begins with recipes on containers and data structures and guides you in performing effective text management in Python. You will find Python recipes for command-line operations, networking, filesystems and directories, and concurrent execution. You will learn about Python security essentials in Python and get to grips with various development tools for debugging, benchmarking, inspection, error reporting, and tracing. The book includes recipes to help you create graphical user interfaces for your application. You will learn to work with multimedia components and perform mathematical operations on date and time. The recipes will also show you how to deploy different searching and sorting algorithms on your data. By the end of the book, you will have acquired the skills needed to write clean code in Python and develop applications that meet your needs.
Table of Contents (21 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Preface

Python is a very powerful and widespread language, with a fully-featured standard library. It's said to come with batteries included, which means that most of what you will have to do will be available in the standard library.

Such a big set of functions might make developers feel lost, and it's not always clear which of the available tools are the best for solving a specific task. For many of these tasks, external libraries will also be available that you can install to solve the same problem. So, you might not only find yourself wondering which class or function to use from all the features provided by the standard library, but you will also wonder when it's best to switch to an external library to achieve your goals.

This book tries to provide a general overview of the tools available in the Python standard library to solve many common tasks and recipes to leverage those tools to achieve specific results. For cases where the solution based on the standard library might get too complex or limited, it will also try to suggest tools out of the standard library that can help you do the next step. 

Who this book is for

This book is well suited for developers who want to write expressive, highly responsive, manageable, scalable, and resilient code in Python. Prior programming knowledge of Python is expected.

What this book covers

Chapter 1, Containers and Data Structures, covers less obvious cases of data structures and containers provided by the standard library. While more basic containers such as list and dict are taken for granted, the chapter will dive into less common containers and more advanced usages of the built-in containers.

Chapter 2, Text Management, covers text manipulation, string comparison, matching, and the most common needs when formatting output for text-based software.

Chapter 3, Command Line, covers how to write terminal/shell based software, parsing arguments, writing interactive shells, and implement logging.

 

Chapter 4, Filesystem and Directories, covers how to work with directories and files, traverse filesystems and work with multiple encoding types related to filesystems and filenames.

Chapter 5, Date and Time, covers how to parse dates and times, format them, and apply math over dates to compute past and future dates.

Chapter 6, Read/Write Data, covers how to read and write data in common file formats, such as CSV, XML, and ZIP, and how to properly manage encoding text files.

Chapter 7, Algorithms, covers some of the most common algorithms for sorting, searching, and zipping, and common operations that you might have to apply on any kind of sets of data.

Chapter 8, Cryptography, covers security-related functions that are provided by the standard library or that can be implemented with the hashing functions available in the standard library.

Chapter 9, Concurrency, covers the various concurrency models provided by the standard library, such as threads, processes, and coroutines, with a specific focus on the orchestration of those executors.

Chapter 10, Networking, covers features provided by the standard library to implement networking-based applications, how to read from some common protocols, such as FTP and IMAP, and how to implement general-purpose TCP/IP applications.

Chapter 11, Web Development, covers how to implement HTTP-based applications, simple HTTP servers, and fully-featured web applications. It will also cover how to interact with third-party software through HTTP.

Chapter 12, Multimedia, covers basic operations on detecting file types, checking images, and generating sounds.

Chapter 13, Graphical User Interfaces, covers the most common building blocks of UI-based applications that can be combined to create simple applications for desktop environments.

Chapter 14, Development Tools, covers tools provided by the standard library to help developers in their everyday work, such as writing tests and debugging software.

 

To get the most out of this book

Readers are expected to already have prior knowledge of Python and programming. Developers that come from other languages or that are intermediate with Python will get the most out of this book.

This book takes for granted that readers have a working installation of Python 3.5+, and most of the recipes show examples for Unix-based systems (such as macOS or Linux) but are expected to work on Windows system too. Windows users can rely on the Windows subsystem for Linux to perfectly reproduce the examples.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Modern-Python-Standard-Library-Cookbook. We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "We can also get rid of the last.getcall by combiningChainMapwithdefaultdict."

A block of code is set as follows:

for word in 'hello world this is a very nice day'.split():
    if word in counts:
        counts[word] += 1

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

class Bunch(dict):
    def __init__(self, **kwds):
        super().__init__(**kwds)
        self.__dict__ = self

Any command-line input or output is written as follows:

>>> print(population['japan'])
127

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example:"If a continuous integration system is involved"

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

 

Sections

In this book, you will find several headings that appear frequently (Getting ready,How to do it..., How it works..., There's more..., and Seealso).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

 

 

 

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.