39,59 €
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.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 396
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Aaron LazarAcquisition Editor: Chaitanya NairContent Development Editor: Rohit SinghTechnical Editor: Romy DiasCopy Editor: Safis EditingProject Coordinator: Vaidehi SawantProofreader: Safis EditingIndexer: Mariammal ChettiyarGraphics:Jason MonteiroProduction Coordinator: Deepika Naik
First published: August 2018
Production reference: 1300818
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78883-082-9
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Alessandro Molina has been a Python developer since 2001, and has always been interested in Python as a web development platform. He has worked as a CTO and a team leader of Python teams for the past 10 years and is currently the core developer of the TurboGears2 web framework and maintainer of Beaker Caching/Session framework. He authored the DEPOT file storage framework and the DukPy JavaScript interpreter for Python and has collaborated with various Python projects related to web development, such as FormEncode, ToscaWidgets, and the Ming MongoDB ORM.
Simone Marzola is a software engineer and technical lead with 10 years of experience. He is passionate about Python and machine learning, which lead him to be an active contributor in open source communities such as Mozilla Services and Pylons Project, and involved in European conferences as a speaker. Simone has been a lecturer on the Big Dive data science and machine learning course. He is currently a CTO and Scrum Master at Oval Money.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Modern Python Standard Library Cookbook  
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Conventions used
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Get in touch
Reviews
Containers and Data Structures
Introduction
Counting frequencies
How to do it...
How it works...
There's more...
Dictionary with fallback
How to do it...
How it works...
There's more...
Unpacking multiple keyword arguments
How to do it...
How it works...
There's more...
Ordered dictionaries
How to do it...
How it works...
There's more...
MultiDict
How to do it...
How it works...
There's more...
Prioritizing entries
How to do it...
How it works...
There's more...
Bunch
How to do it...
How it works...
There's more...
Enumerations
How to do it...
How it works...
There's more...
Text Management
Introduction
Pattern matching
How to do it...
There's more...
Text similarity
How to do it...
There's more...
Text suggestion
How to do it...
Templating
How to do it...
How it works...
There's more...
Splitting strings and preserving spaces
How to do it...
How it works...
Cleanup text
How to do it...
How it works...
Normalizing text
How to do it...
How it works...
Aligning text
How to do it...
How it works...
Command Line
Introduction
Basic logging
How to do it...
How it works...
There's more...
Logging to file
How to do it...
How it works...
Logging to Syslog
Getting ready
How to do it...
There's more...
Parsing arguments
How to do it...
How it works...
There's more...
Interactive shells
How to do it...
How it works...
Sizing terminal text
How to do it...
How it works...
Running system commands
How to do it...
How it works...
There's more...
Progress bar
How to do it...
How it works...
Message boxes
Getting ready
How to do it...
How it works...
Input box
Getting ready
How to do it...
How it works...
Filesystem and Directories
Introduction
Traversing folders
How to do it...
How it works...
Working with paths
How to do it...
There's more...
Expanding filenames
How to do it...
Getting file information
How to do it...
Named temporary files
How to do it...
Memory and disk buffer
How to do it...
How it works...
Managing filename encoding
How to do it...
How it works...
Copying a directory
How to do it...
How it works...
Safely replacing file's content
How to do it...
How it works...
Date and Time
Introduction
Time-zone-aware datetime
How to do it...
How it works...
There's more...
Parsing dates
How to do it...
How it works...
There's more...
Saving dates
How to do it...
How it works...
From timestamps to datetimes
How to do it...
There's more...
Displaying dates in user format
How to do it...
How it works...
There's more...
Going to tomorrow
How to do it...
How it works...
Going to next month
How to do it...
How it works...
Weekdays
How to do it...
How it works...
Workdays
How to do it...
How it works...
Combining dates and times
How to do it...
There's more...
Read/Write Data
Introduction
Reading and writing text data
How to do it...
How it works...
There's more...
Reading lines of text
How to do it...
How it works...
Reading and writing binary data
How to do it...
How it works...
There's more...
Zipping a directory
How to do it...
How it works...
Pickling and shelving
How to do it...
How it works...
There's more...
Reading configuration files
How to do it...
How it works...
There's more...
Writing XML/HTML content
How to do it...
How it works...
There's more...
Reading XML/HTML content
How to do it...
How it works...
There's more...
Reading and writing CSV
How to do it...
There's more...
Reading/writing a database
How to do it...
How it works...
There's more...
Algorithms
Introduction
Searching, sorting, filtering
How to do it...
How it works...
There's more...
Getting the nth element of any iterable
How to do it...
How it works...
Grouping similar items
How to do it...
How it works...
Zipping
How to do it...
How it works...
Flattening a list of lists
How to do it...
How it works...
There's more...
Producing permutations and combinations
How to do it...
Accumulating and reducing
How to do it...
There's more...
Memoizing
How to do it...
How it works...
Operators to functions
How to do it...
How it works...
Partials
How to do it...
How it works...
Generic functions
How to do it...
How it works...
Proper decoration
How to do it...
There's more...
Context managers
How to do it...
Applying variable context managers
How to do it...
Cryptography
Introduction
Asking for passwords
How to do it...
How it works...
Hashing passwords
How to do it...
How it works...
Verifying a file's integrity
How to do it...
How it works...
Verifying a message's integrity
How to do it...
How it works...
Concurrency
Introduction
ThreadPools
How to do it...
How it works...
There's more...
Coroutines
How to do it...
How it works...
There's more...
Processes
How to do it...
How it works...
There's more...
Futures
How to do it...
How it works...
There's more...
Scheduled tasks
How to do it...
How it works...
Sharing data between processes
How to do it...
How it works...
There's more...
Networking
Introduction
Sending emails
How to do it...
How it works...
Fetching emails
How to do it...
How it works...
There's more...
FTP
How to do it...
How it works...
There's more...
Sockets
How to do it...
How it works...
AsyncIO
How to do it...
How it works...
Remote procedure calls
How to do it...
How it works...
There's more...
Web Development
Introduction
Treating JSON
How to do it...
How it works...
There's more...
Parsing URLs
How to do it...
There's more...
Consuming HTTP
How to do it...
How it works...
There's more...
Submitting forms to HTTP
How to do it...
How it works...
There's more...
Building HTML
How to do it...
How it works...
Serving HTTP
How to do it...
How it works...
There's more...
Serving static files
How to do it...
How it works...
There's more...
Errors in web applications
How to do it...
How it works...
There's more...
Handling forms and files
How to do it...
How it works...
REST API
How to do it...
How it works...
Handling cookies
How to do it...
How it works...
There's more...
Multimedia
Introduction
Determining the type of a file
How to do it...
How it works...
Detecting image types
How to do it...
How it works...
There's more...
Detecting image sizes
How to do it...
How it works...
Playing audio/video/images
How to do it...
How it works...
Graphical User Interfaces
Introduction
Alerts
How to do it...
How it works...
Dialog boxes
How to do it...
How it works...
ProgressBar dialog
How to do it...
How it works...
Lists
How to do it...
How it works...
Menus
How to do it...
How it works...
Development Tools
Introduction
Debugging
How to do it...
How it works...
There's more...
Testing
How to do it...
How it works...
There's more...
Mocking
How it works...
How it works...
There's more...
Reporting errors in production
How to do it...
How it works...
Benchmarking
How to do it...
There's more...
Inspection
How to do it...
How it works...
There's more...
Code evaluation
How to do it...
Tracing code
How to do it...
How it works...
There's more...
Profiling
How to do it...
How it works...
Other Books You May Enjoy
Leave a review - let other readers know what you think
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.
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.
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.
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.
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:
Log in or register at
www.packtpub.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
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!
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:
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.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
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.
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.
In this chapter, we will cover the following recipes:
Counting frequencies—count occurrences of any hashable value
Dictionary with fallback—have a fallback value for any missing key
Unpacking multiple
—
keyword arguments—how to use
**
more than once
Ordered dictionaries
—
maintaining order of keys in a dictionary
MultiDict
—
dictionary with multiple values per key
Prioritizing entries
—
efficiently get the top of sorted entries
Bunch
—
dictionaries that behave like objects
Enumerations
—
handle a known set of states
Python has a very easy and flexible set of built-in containers. As a Python developer, there is little you can't achieve with a dict or a list. The convenience of Python dictionaries and lists is such that developers often forget that those have limits. Like any data structure, they are optimized and designed for specific use cases and might be inefficient in some conditions, or even unable to handle them.
Ever tried to put a key in a dictionary twice? Well you can't, because Python dictionaries are designed as hash tables with unique keys, but the MultiDict recipe will show you how to do that. Ever tried to grab the lowest/highest values out of a list without traversing it whole? The list itself can't, but in the Prioritized entries recipe, we will see how to achieve that.
The limits of standard Python containers are well known to Python experts. For that reason, the standard library has grown over the years to overcome those limits, and frequently there are patterns so common that their name is widely recognized, even though they are not formally defined.
A very common need in many kinds of programs is to count the occurrences of a value or of an event, which means counting frequency. Be it the need to count words in text, count likes on a blog post, or track scores for players of a video game, in the end counting frequency means counting how many we have of a specific value.
The most obvious solution for such a need would be to keep around counters for the things we need to count. If there are two, three, or four, maybe we can just track them in some dedicated variables, but if there are hundreds, it's certainly not feasible to keep around such a large amount of variables and we will quickly end up with a solution based on a container to collect all those counters.
Our counting code relies on the fact that Counter is just a special kind of dictionary, and that dictionaries can be built by providing an iterable. Each entry in the iterable will be added to the dictionary.
In the case of a counter, adding an element means incrementing its count; for every "word" in our list, we add that word multiple times (one every time it appears in the list), so its value in the Counter continues to get incremented every time the word is encountered.
The ChainMap class receives multiple dictionaries as arguments; whenever a key is requested to ChainMap, it's actually going through the provided dictionaries one by one to check whether the key is available in any of them. Once the key is found, it is returned, as if it was a key owned by ChainMap itself.
The default value for options that are not provided is implemented by having defaultdict as the last dictionary provided to ChainMap. Whenever a key is not found in any of the previous dictionaries, it gets looked up in defaultdict, which uses the provided factory function to return a default value for all keys.
Frequently, you ended up in a situation where you had to provide arguments to a function from a dictionary. If you've ever faced that need, you probably also ended up in a case where you had to take the arguments from multiple dictionaries.
Generally, Python functions accept arguments from a dictionary through unpacking (the ** syntax), but so far, it hasn't been possible to use unpacking twice in the same call, nor was there an easy way to merge two dictionaries.
As we already know from the previous recipe, ChainMap looks up keys in all the provided dictionaries, so it's like the sum of all the dictionaries. The unpacking operator (**) works by inviting all keys to the container and then providing an argument for each key.
As ChainMap has keys resulting from the sum of all the provided dictionaries keys, it will provide the keys contained in all the dictionaries to the unpacking operator, thus allowing us to provide keyword arguments from multiple dictionaries.
Python 3.6 introduced a guarantee of preserving the order of dictionary keys as a side effect of some changes to dictionaries, but it was considered an internal implementation detail and not a language guarantee. Since Python 3.7, it became an official feature of the language so it's actually safe to rely on dictionary ordering if you are using Python 3.6 or newer.
If you have ever need to provide a reverse mapping, you have probably discovered that Python lacks a way to store more than a value for each key in a dictionary. This is a very common need, and most languages provide some form of multimap container.
Python tends to prefer having a single way of doing things, and as storing multiple values for the key means just storing a list of values for a key, it doesn't provide a specialized container.
The issue with storing a list of values is that to be able to append to values to our dictionary, the list must already exist.
MultiDict works by storing a list for each key. Whenever a key is accessed, the list containing all the values for that key is retrieved.
In the case of missing keys, an empty list will be provided so that values can be added for that key.
This works because every time defaultdict faces a missing key, it will insert it with a value generated by calling list. And calling list will actually provide an empty list. So, doing rd[v] will always provide a list, empty or not, depending on whether v was an already existing key or not. Once we have our list, adding a new value is just a matter of appending it.
Picking the first/top entry of a set of values is a pretty frequent need; this usually means defining one value that has priority over the other and involves sorting.
But sorting can be expensive and re-sorting every time you add an entry to your values is certainly not a very convenient way to pick the first entry out of a set of values with some kind of priority.
PriorityQueue works by storing everything in an heap. Heaps are particularly efficient at retrieving the top/first element of a sorted set without having to actually sort the whole set.
Our priority queue stores all the values in a three-element tuple: priority, time.time(), and value.
The first entry of our tuple is priority (lower is better). In the example, we recorded f1 with a better priority than f2, which ensures than when we use heap.heappop to fetch tasks to process, we get f1 and then f2, so that we end up with the hello world message and not world hello.
The second entry, timestamp, is used to ensure that tasks that have the same priority are processed in their insertion order. The oldest task will be served first as it will have the smallest timestamp.
Then, we have the value itself, which is the function we want call for our task.
