O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Modern Python Cookbook

Book Description

The latest in modern Python recipes for the busy modern programmer

About This Book

  • Develop succinct, expressive programs in Python
  • Learn the best practices and common idioms through carefully explained and structured recipes
  • Discover new ways to apply Python for the new age of development

Who This Book Is For

The book is for web developers, programmers, enterprise programmers, engineers, big data scientist, and so on. If you are a beginner, Python Cookbook will get you started. If you are experienced, it will expand your knowledge base. A basic knowledge of programming would help.

What You Will Learn

  • See the intricate details of the Python syntax and how to use it to your advantage
  • Improve your code readability through functions in Python
  • Manipulate data effectively using built-in data structures
  • Get acquainted with advanced programming techniques in Python
  • Equip yourself with functional and statistical programming features
  • Write proper tests to be sure a program works as advertised
  • Integrate application software using Python

In Detail

Python is the preferred choice of developers, engineers, data scientists, and hobbyists everywhere. It is a great scripting language that can power your applications and provide great speed, safety, and scalability. By exposing Python as a series of simple recipes, you can gain insight into specific language features in a particular context. Having a tangible context helps make the language or standard library feature easier to understand.

This book comes with over 100 recipes on the latest version of Python. The recipes will benefit everyone ranging from beginner to an expert. The book is broken down into 13 chapters that build from simple language concepts to more complex applications of the language.

The recipes will touch upon all the necessary Python concepts related to data structures, OOP, functional programming, as well as statistical programming. You will get acquainted with the nuances of Python syntax and how to effectively use the advantages that it offers. You will end the book equipped with the knowledge of testing, web services, and configuration and application integration tips and tricks.

The recipes take a problem-solution approach to resolve issues commonly faced by Python programmers across the globe. You will be armed with the knowledge of creating applications with flexible logging, powerful configuration, and command-line options, automated unit tests, and good documentation.

Style and approach

This book takes a recipe-based approach, where each recipe addresses specific problems and issues. The recipes provide discussions and insights and an explanation of the problems.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Modern Python Cookbook
    1. Modern Python Cookbook
    2. Credits
    3. About the Author
    4. About the Reviewers
    5. www.PacktPub.com
      1. Why subscribe?
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    7. 1. Numbers, Strings, and Tuples
      1. Introduction
      2. Creating meaningful names and using variables
        1. Getting ready
        2. How to do it...
          1. Choosing names wisely
          2. Assigning names to objects
        3. How it works...
        4. There's more...
        5. See also
      3. Working with large and small integers
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Choosing between float, decimal, and fraction
        1. Getting ready
        2. How to do it...
          1. Doing currency calculations
          2. Fraction calculations
          3. Floating-point approximations
          4. Converting numbers from one type to another
        3. How it works...
        4. There's more...
        5. See also
      5. Choosing between true division and floor division
        1. Getting ready
        2. How to do it...
          1. Doing floor division
          2. Doing true division
          3. Rational fraction calculations
        3. How it works...
        4. See also
      6. Rewriting an immutable string
        1. Getting ready
        2. How to do it...
          1. Slicing a piece of a string
          2. Updating a string with a replacement
          3. Making a string all lowercase
          4. Removing extra punctuation marks
        3. How it works...
        4. There's more...
        5. See also
      7. String parsing with regular expressions
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Building complex strings with "template".format()
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Building complex strings from lists of characters
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more
        5. See also
      10. Using the Unicode characters that aren't on our keyboards
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      11. Encoding strings – creating ASCII and UTF-8 bytes
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      12. Decoding bytes – how to get proper characters from some bytes
        1. Getting ready
        2. How to do it..
        3. How it works...
        4. See also
      13. Using tuples of items
        1. Getting ready
        2. How to do it...
          1. Creating tuples
          2. Extracting items from a tuple
        3. How it works...
        4. There's more
        5. See also...
    8. 2. Statements and Syntax
      1. Introduction
      2. Writing Python script and module files – syntax basics
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Writing long lines of code
        1. Getting ready
        2. How to do it...
          1. Using backslash to break a long statement into logical lines
          2. Using the () characters to break a long statement into sensible pieces
          3. Using string literal concatenation
          4. Assigning intermediate results to separate variables
        3. How it works...
        4. There's more...
        5. See also
      4. Including descriptions and documentation
        1. Getting ready
        2. How to do it...
          1. Writing docstrings for scripts
          2. Writing docstrings for library modules
        3. How it works...
        4. There's more...
        5. See also
      5. Writing better RST markup in docstrings
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using directives
          2. Using inline markup
        5. See also
      6. Designing complex if...elif chains
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Designing a while statement which terminates properly
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      8. Avoiding a potential problem with break statements
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Leveraging the exception matching rules
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Avoiding a potential problem with an except: clause
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      11. Chaining exceptions with the raise from statement
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      12. Managing a context using the with statement
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    9. 3. Function Definitions
      1. Introduction
      2. Designing functions with optional parameters
        1. Getting ready
        2. How to do it...
          1. Particular to General Design
          2. General to Particular design
        3. How it works...
        4. There's more...
        5. See also
      3. Using super flexible keyword parameters
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Forcing keyword-only arguments with the * separator
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Writing explicit types on function parameters
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Picking an order for parameters based on partial functions
        1. Getting ready
        2. How to do it...
          1. Wrapping a function
          2. Creating a partial function with keyword parameters
          3. Creating a partial function with positional parameters
        3. How it works...
        4. There's more...
        5. See also
      7. Writing clear documentation strings with RST markup
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Designing recursive functions around Python's stack limits
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Writing reusable scripts with the script library switch
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    10. 4. Built-in Data Structures – list, set, dict
      1. Introduction
      2. Choosing a data structure
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Building lists – literals, appending, and comprehensions
        1. Getting ready
        2. How to do it...
          1. Building a list with the append() method
          2. Writing a list comprehension
          3. Using the list function on a generator expression
        3. How it works...
        4. There's more...
          1. Other ways to extend a list
        5. See also
      4. Slicing and dicing a list
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Deleting from a list – deleting, removing, popping, and filtering
        1. Getting ready
        2. How to do it...
          1. Deleting items from a list
          2. The remove() method
          3. The pop() method
          4. The filter() function
        3. How it works...
        4. There's more...
        5. See also
      6. Reversing a copy of a list
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Using set methods and operators
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Removing items from a set – remove(), pop(), and difference
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Creating dictionaries – inserting and updating
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Removing from dictionaries – the pop() method and the del statement
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      11. Controlling the order of dict keys
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      12. Handling dictionaries and sets in doctest examples
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      13. Understanding variables, references, and assignment
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
      14. Making shallow and deep copies of objects
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      15. Avoiding mutable default values for function parameters
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    11. 5. User Inputs and Outputs
      1. Introduction
      2. Using features of the print() function
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Using input() and getpass() for user input
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Input string parsing
          2. Interaction via the cmd module
        5. See also
      4. Debugging with "format".format_map(vars())
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Using argparse to get command-line input
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Using cmd for creating command-line applications
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Using the OS environment settings
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    12. 6. Basics of Classes and Objects
      1. Introduction
      2. Using a class to encapsulate data and processing
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Designing classes with lots of processing
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Designing classes with little unique processing
        1. Getting ready
        2. How to do it...
          1. Stateless objects
          2. Stateful objects with a new class
          3. Stateful objects using an existing class
        3. How it works...
        4. There's more...
        5. See also
      5. Optimizing small objects with __slots__
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Using more sophisticated collections
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Extending a collection – a list that does statistics
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Using properties for lazy attributes
        1. Getting ready...
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also...
      9. Using settable properties to update eager attributes
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Initialization
          2. Calculation
        5. See also
    13. 7. More Advanced Class Design
      1. Introduction
      2. Choosing between inheritance and extension – the is-a question
        1. Getting ready
        2. How to do it...
          1. Wrapping – aggregation and composition
          2. Extending - inheritance
        3. How it works...
        4. There's more...
        5. See also
      3. Separating concerns via multiple inheritance
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Leveraging Python's duck typing
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Managing global and singleton objects
        1. Getting ready
        2. How to do it...
          1. Module global variable
          2. Class-level static variable
        3. How it works...
        4. There's more...
      6. Using more complex structures – maps of lists
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Creating a class that has orderable objects
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Defining an ordered collection
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Deleting from a list of mappings
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    14. 8. Functional and Reactive Programming Features
      1. Introduction
      2. Writing generator functions with the yield statement
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Using stacked generator expressions
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Namespace instead of list
        5. See also
      4. Applying transformations to a collection
        1. Getting ready...
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also...
      5. Picking a subset - three ways to filter
        1. Getting ready...
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also...
      6. Summarizing a collection – how to reduce
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Maxima and minima
          2. Potential for abuse
      7. Combining map and reduce transformations
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Implementing "there exists" processing
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. The itertools module
      9. Creating a partial function
        1. Getting ready
        2. How to do it...
          1. Using functools.partial()
          2. Creating a lambda object
        3. How it works...
        4. There's more...
      10. Simplifying complex algorithms with immutable data structures
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      11. Writing recursive generator functions with the yield from statement
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    15. 9. Input/Output, Physical Format, and Logical Layout
      1. Introduction
      2. Using pathlib to work with filenames
        1. Getting ready
        2. How to do it...
          1. Making the output filename by changing the input suffix
          2. Making a number of sibling output files with distinct names
          3. Creating a directory and a number of files
          4. Comparing file dates to see which is newer
          5. Removing a file
          6. Finding all files that match a given pattern
        3. How it works...
        4. There's more...
        5. See also
      3. Reading and writing files with context managers
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Replacing a file while preserving the previous version
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Reading delimited files with the CSV module
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Reading complex formats using regular expressions
        1. Getting ready
        2. How to do it...
          1. Defining the parse function
          2. Using the parse function
        3. How it works...
        4. There's more...
        5. See also
      7. Reading JSON documents
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Serializing a complex data structure
          2. Deserializing a complex data structure
        5. See also
      8. Reading XML documents
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Reading HTML documents
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Upgrading CSV from DictReader to namedtuple reader
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      11. Upgrading CSV from a DictReader to a namespace reader
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      12. Using multiple contexts for reading and writing files
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    16. 10. Statistical Programming and Linear Regression
      1. Introduction
      2. Using the built-in statistics library
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      3. Average of values in a Counter
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Computing the coefficient of a correlation
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      5. Computing regression parameters
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      6. Computing an autocorrelation
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Long-term model
        5. See also
      7. Confirming that the data is random – the null hypothesis
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Locating outliers
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Analyzing many variables in one pass
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using map()
        5. See also
    17. 11. Testing
      1. Introduction
      2. Using docstrings for testing
        1. Getting ready
        2. How to do it...
          1. Writing examples for stateless functions
          2. Writing examples for stateful objects
        3. How it works...
        4. There's more...
        5. See also
      3. Testing functions that raise exceptions
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Handling common doctest issues
        1. Getting ready
        2. How to do it...
          1. Writing doctest examples for mapping or set values
          2. Writing doctest examples for floating-point values
        3. How it works...
        4. There's more...
        5. See also
      5. Creating separate test modules and packages
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Some other assertions
          2. Separate tests directory
        5. See also
      6. Combining unittest and doctest tests
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Testing things that involve dates or times
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Testing things that involve randomness
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Mocking external resources
        1. Getting ready
          1. Creating an entry document in the entrylog collection
          2. Seeing a typical response
          3. Client class for database access
        2. How to do it...
        3. How it works...
          1. Creating a context manager
          2. Creating a dynamic, stateful test
          3. Mocking a complex object
          4. Using the load_tests protocol
        4. There's more...
        5. See also
    18. 12. Web Services
      1. Introduction
      2. Implementing web services with WSGI
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Using the Flask framework for RESTful APIs
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Parsing the query string in a request
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Making REST requests with urllib
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. The OpenAPI (Swagger) specification
          2. Adding Swagger to the server
        5. See also
      6. Parsing the URL path
        1. Getting ready
        2. How to do it...
          1. Server
          2. Client
        3. How it works...
          1. Deck slicing
          2. Client side
        4. There's more...
          1. Providing a Swagger specification
          2. Using a Swagger specification
        5. See also
      7. Parsing a JSON request
        1. Getting ready
        2. How to do it...
          1. Swagger specification
          2. Server
          3. Client
        3. How it works...
        4. There's more...
          1. Location header
          2. Additional resources
          3. Query for a specific player
          4. Exception handling
        5. See also
      8. Implementing authentication for web services
        1. Getting ready
          1. Configuring SSL
          2. Users and credentials
          3. Flask view function decorator
        2. How to do it...
          1. Defining the User class
          2. Defining a view decorator
          3. Creating the server
          4. Creating an example client
        3. How it works...
        4. There's more...
          1. Creating a command-line interface
          2. Building the Authentication header
        5. See also
    19. 13. Application Integration
      1. Introduction
      2. Finding configuration files
        1. Getting ready
          1. Why so many choices?
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Using YAML for configuration files
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Using Python for configuration files
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Using class-as-namespace for configuration
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Configuration representation
        5. See also
      6. Designing scripts for composition
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Designing as a class hierarchy
        5. See also
      7. Using logging for control and audit output
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      8. Combining two applications into one
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Refactoring
          2. Concurrency
          3. Logging
        5. See also
      9. Combining many applications using the Command design pattern
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Managing arguments and configuration in composite applications
        1. Getting ready
        2. How to do it...
        3. How it works...
          1. The Command design pattern
        4. There's more...
        5. See also
      11. Wrapping and combining CLI applications
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Unit test
        5. See also
      12. Wrapping a program and checking the output
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      13. Controlling complex sequences of steps
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Building conditional processing
        5. See also