0%

Book Description

Complete recipes spread across 15 chapters to help you overcome commonly faced issues by Python for everybody across the globe. Each recipe takes a problem-solution approach to resolve for effective Python.

Key Features

  • Develop expressive and effective Python programs
  • Best practices and common idioms through carefully explained recipes
  • Discover new ways to apply Python for data-focused development
  • Make use of Python's optional type annotations

Book Description

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

This book comes with 133 recipes on the latest version of Python 3.8. The recipes will benefit everyone, from beginners just starting out with Python to experts. You'll not only learn Python programming concepts but also how to build complex applications.

The recipes will touch upon all necessary Python concepts related to data structures, object oriented programming, functional programming, and statistical programming. You will get acquainted with the nuances of Python syntax and how to effectively take advantage of it.

By the end of this Python book, you will be equipped with knowledge of testing, web services, configuration, and application integration tips and tricks. You will be armed with the knowledge of how to create applications with flexible logging, powerful configuration, command-line options, automated unit tests, and good documentation.

What you will learn

  • See the intricate details of the Python syntax and how to use it to your advantage
  • Improve your coding with Python readability through functions
  • 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

Who this book is for

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

Table of Contents

  1. Preface
    1. What you need for this book
    2. Who this book is for
    3. What this book covers
    4. To get the most out of this book
    5. Get in touch
  2. Numbers, Strings, and Tuples
    1. Working with large and small integers
    2. Choosing between float, decimal, and fraction
    3. Choosing between true division and floor division
    4. Rewriting an immutable string
    5. String parsing with regular expressions
    6. Building complex strings with f-strings
    7. Building complicated strings from lists of characters
    8. Using the Unicode characters that aren't on our keyboards
    9. Encoding strings – creating ASCII and UTF-8 bytes
    10. Decoding bytes – how to get proper characters from some bytes
    11. Using tuples of items
    12. Using NamedTuples to simplify item access in tuples
  3. Statements and Syntax
    1. Writing Python script and module files – syntax basics
    2. Writing long lines of code
    3. Including descriptions and documentation
    4. Writing better RST markup in docstrings
    5. Designing complex if...elif chains
    6. Saving intermediate results with the := "walrus"
    7. Avoiding a potential problem with break statements
    8. Leveraging exception matching rules
    9. Avoiding a potential problem with an except: clause
    10. Concealing an exception root cause
    11. Managing a context using the with statement
  4. Function Definitions
    1. Function parameters and type hints
    2. Designing functions with optional parameters
    3. Designing type hints for optional parameters
    4. Using super flexible keyword parameters
    5. Forcing keyword-only arguments with the * separator
    6. Defining position-only parameters with the / separator
    7. Writing hints for more complex types
    8. Picking an order for parameters based on partial functions
    9. Writing clear documentation strings with RST markup
    10. Designing recursive functions around Python's stack limits
    11. Writing testable scripts with the script-library switch
  5. Built-In Data Structures Part 1: Lists and Sets
    1. Choosing a data structure
    2. Building lists – literals, appending, and comprehensions
    3. Slicing and dicing a list
    4. Deleting from a list – deleting, removing, popping, and filtering
    5. Writing list-related type hints
    6. Reversing a copy of a list
    7. Building sets – literals, adding, comprehensions, and operators
    8. Removing items from a set – remove(), pop(), and difference
    9. Writing set-related type hints
  6. Built-In Data Structures Part 2: Dictionaries
    1. Creating dictionaries – inserting and updating
    2. Removing from dictionaries – the pop() method and the del statement
    3. Controlling the order of dictionary keys
    4. Writing dictionary-related type hints
    5. Understanding variables, references, and assignment
    6. Making shallow and deep copies of objects
    7. Avoiding mutable default values for function parameters
  7. User Inputs and Outputs
    1. Using the features of the print() function
    2. Using input() and getpass() for user input
    3. Debugging with f"{value=}" strings
    4. Using argparse to get command-line input
    5. Using cmd to create command-line applications
    6. Using the OS environment settings
  8. Basics of Classes and Objects
    1. Using a class to encapsulate data and processing
    2. Essential type hints for class definitions
    3. Designing classes with lots of processing
    4. Using typing.NamedTuple for immutable objects
    5. Using dataclasses for mutable objects
    6. Using frozen dataclasses for immutable objects
    7. Optimizing small objects with __slots__
    8. Using more sophisticated collections
    9. Extending a built-in collection – a list that does statistics
    10. Using properties for lazy attributes
    11. Creating contexts and context managers
    12. Managing multiple contexts with multiple resources
  9. More Advanced Class Design
    1. Choosing between inheritance and composition – the "is-a" question
    2. Separating concerns via multiple inheritance
    3. Leveraging Python's duck typing
    4. Managing global and singleton objects
    5. Using more complex structures – maps of lists
    6. Creating a class that has orderable objects
    7. Improving performance with an ordered collection
    8. Deleting from a list of complicated objects
  10. Functional Programming Features
    1. Introduction
    2. Writing generator functions with the yield statement
    3. Applying transformations to a collection
    4. Using stacked generator expressions
    5. Picking a subset – three ways to filter
    6. Summarizing a collection – how to reduce
    7. Combining the map and reduce transformations
    8. Implementing "there exists" processing
    9. Creating a partial function
    10. Simplifying complex algorithms with immutable data structures
    11. Writing recursive generator functions with the yield from statement
  11. Input/Output, Physical Format, and Logical Layout
    1. Using pathlib to work with filenames
    2. Replacing a file while preserving the previous version
    3. Reading delimited files with the CSV module
    4. Using dataclasses to simplify working with CSV files
    5. Reading complex formats using regular expressions
    6. Reading JSON and YAML documents
    7. Reading XML documents
    8. Reading HTML documents
    9. Refactoring a .csv DictReader as a dataclass reader
  12. Testing
    1. Test tool setup
    2. Using docstrings for testing
    3. Testing functions that raise exceptions
    4. Handling common doctest issues
    5. Unit testing with the unittest module
    6. Combining unittest and doctest tests
    7. Unit testing with the pytest module
    8. Combining pytest and doctest tests
    9. Testing things that involve dates or times
    10. Testing things that involve randomness
    11. Mocking external resources
  13. Web Services
    1. Defining the card model
    2. Using the Flask framework for RESTful APIs
    3. Parsing the query string in a request
    4. Making REST requests with urllib
    5. Parsing the URL path
    6. Parsing a JSON request
    7. Implementing authentication for web services
  14. Application Integration: Configuration
    1. Finding configuration files
    2. Using YAML for configuration files
    3. Using Python for configuration files
    4. Using class-as-namespace for configuration
    5. Designing scripts for composition
    6. Using logging for control and audit output
  15. Application Integration: Combination
    1. Combining two applications into one
    2. Combining many applications using the Command Design Pattern
    3. Managing arguments and configuration in composite applications
    4. Wrapping and combining CLI applications
    5. Wrapping a program and checking the output
    6. Controlling complex sequences of steps
  16. Statistical Programming and Linear Regression
    1. Using the built-in statistics library
    2. Average of values in a counter
    3. Computing the coefficient of a correlation
    4. Computing regression parameters
    5. Computing an autocorrelation
    6. Confirming that the data is random – the null hypothesis
    7. Locating outliers
    8. Analyzing many variables in one pass
  17. Other Books You May Enjoy
  18. Index
34.239.150.167