Functional Python Programming: Use a functional approach to write succinct, expressive, and efficient Python code, 3rd Edition

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Python isn't all about object-oriented programming. Discover a valuable way of thinking about code design through a function-first approach - and learn when you need to use it. Now with detailed exercises at the end of every chapter!

Purchase of the print or Kindle book includes a free eBook in PDF format.

Key Features

  • Learn how, when, and why to adopt functional elements in your projects
  • Explore the Python modules essential to functional programming, like itertools and functools
  • Revised to cover new features of Python 3.10, exercises at the end of every chapter, and more

Book Description

Not enough developers understand the benefits of functional programming, or even what it is. Author Steven Lott demystifies the approach, teaching you how to improve the way you code in Python and make gains in memory use and performance.

Starting from the fundamentals, this book shows you how to apply functional thinking and techniques in a range of scenarios, with examples centered around data cleaning and exploratory data analysis. You'll learn how to use generator expressions, list comprehensions, and decorators to your advantage. You don't have to abandon object-oriented design completely, though - you'll also see how Python's native object-orientation is used in conjunction with functional programming techniques.

By the end of this book, you'll be well versed in the essential functional programming features of Python, and understand why and when functional thinking helps. You'll also have all the tools you need to pursue any additional functional topics that are not part of the Python language.

What you will learn

  • Use Python's libraries to avoid the complexities of state-changing classes
  • Leverage built-in higher-order functions to avoid rewriting common algorithms
  • Write generator functions to create lazy processing
  • Design and implement decorators for functional composition
  • Make use of Python type annotations to describe parameters and results of functions
  • Apply functional programming to concurrency and web services
  • Explore the PyMonad library for stateful simulations

Who this book is for

The functional paradigm is very useful for programmers working in data science, but any Python developer who wants to create more reliable, succinct, and expressive code will have much to learn from this book.

No prior knowledge of functional programming is required to get started, though Python programming knowledge is assumed. A running Python environment is essential.

Table of Contents

  1. Understanding Functional Programming
  2. Introducing Essential Functional Concepts
  3. Functions, Iterators, and Generators
  4. Working with Collections
  5. Higher-Order Functions
  6. Recursions and Reductions
  7. Complex Stateless Objects
  8. The Itertools Module
  9. Itertools for Combinatorics - Permutations and Combinations
  10. The Functools Module
  11. The Toolz Package
  12. Decorator Design Techniques
  13. The PyMonad Library
  14. The Multiprocessing, Threading, and Concurrent.Futures Modules
  15. A Functional Approach toWeb Services
  16. A Chi-Squared Case Study

Author(s): Steven F. Lott
Edition: 3
Publisher: Packt Publishing
Year: 2022

Language: English
Pages: 576

Cover
Copyright
Contributors
Table of Contents
Preface
Chapter 1: Understanding Functional Programming
The functional style of programming
Comparing and contrasting procedural and functional styles
Using the functional paradigm
Using a functional hybrid
The stack of turtles
A classic example of functional programming
Exploratory data analysis
Summary
Exercises
Convert an imperative algorithm to functional code
Convert step-wise computation to functional code
Revise the sqrt() function
Data cleansing steps
(Advanced) Optimize this functional code
Chapter 2: Introducing Essential Functional Concepts
Functions as first-class objects
Pure functions
Higher-order functions
Immutable data
Strict and non-strict evaluation
Lazy and eager evaluation
Recursion instead of an explicit loop state
Functional type systems
Familiar territory
Learning some advanced concepts
Summary
Exercises
Apply map() to a sequence of values
Function vs. lambda design question
Optimize a recursion
Chapter 3: Functions, Iterators, and Generators
Writing pure functions
Functions as first-class objects
Using strings
Using tuples and named tuples
Using generator expressions
Exploring the limitations of generators
Combining generator expressions
Cleaning raw data with generator functions
Applying generators to built-in collections
Generators for lists, dicts, and sets
Using stateful mappings
Using the bisect module to create a mapping
Using stateful sets
Summary
Exercises
Rewrite the some_function() function
Alternative Mersenne class definition
Alternative algorithm implementations
Map and filter
Dictionary comprehension
Raw data cleanup
Chapter 4: Working with Collections
An overview of function varieties
Working with iterables
Parsing an XML file
Parsing a file at a higher level
Pairing up items from a sequence
Using the iter() function explicitly
Extending an iteration
Applying generator expressions to scalar functions
Using any() and all() as reductions
Using len() and sum() on collections
Using sums and counts for statistics
Using zip() to structure and flatten sequences
Unzipping a zipped sequence
Flattening sequences
Structuring flat sequences
Structuring flat sequences – an alternative approach
Using sorted() and reversed() to change the order
Using enumerate() to include a sequence number
Summary
Exercises
Palindromic numbers
Hands of cards
Replace legs() with pairwise()
Chapter 5: Higher-Order Functions
Using max() and min() to find extrema
Using Python lambda forms
Lambdas and the lambda calculus
Using the map() function to apply a function to a collection
Working with lambda forms and map()
Using map() with multiple sequences
Using the filter() function to pass or reject data
Using filter() to identify outliers
The iter() function with a sentinel value
Using sorted() to put data in order
Overview of writing higher-order functions
Writing higher-order mappings and filters
Unwrapping data while mapping
Wrapping additional data while mapping
Flattening data while mapping
Structuring data while filtering
Building higher-order functions with callables
Assuring good functional design
Review of some design patterns
Summary
Exercises
Classification of state
Classification of state, Part II
Optimizing a file parser
Chapter 6: Recursions and Reductions
Simple numerical recursions
Implementing manual tail-call optimization
Leaving recursion in place
Handling difficult tail-call optimization
Processing collections through recursion
Tail-call optimization for collections
Using the assignment (sometimes called the "walrus") operator in recursions
Reductions and folding a collection from many items to one
Tail-call optimization using deques
Group-by reduction from many items to fewer
Building a mapping with Counter
Building a mapping by sorting
Grouping or partitioning data by key values
Writing more general group-by reductions
Writing higher-order reductions
Writing file parsers
Parsing CSV files
Parsing plain text files with headers
Summary
Exercises
Multiple recursion and caching
Refactor the all_print() function
Parsing CSV files
Classification of state, Part III
Diesel engine data
Chapter 7: Complex Stateless Objects
Using tuples to collect data
Using NamedTuple to collect data
Using frozen dataclasses to collect data
Complicated object initialization and property computations
Using pyrsistent to collect data
Avoiding stateful classes by using families of tuples
Computing Spearman's rank-order correlation
Polymorphism and type pattern matching
Summary
Exercises
Frozen dictionaries
Dictionary-like sequences
Revise the rank_xy() function to use native types
Revise the rank_corr() function
Revise the legs() function to use pyrsistent
Chapter 8: The Itertools Module
Working with the infinite iterators
Counting with count()
Counting with float arguments
Re-iterating a cycle with cycle()
Using cycle() for data sampling
Repeating a single value with repeat()
Using the finite iterators
Assigning numbers with enumerate()
Running totals with accumulate()
Combining iterators with chain()
Partitioning an iterator with groupby()
Merging iterables with zip_longest() and zip()
Creating pairs with pairwise())
Filtering with compress()
Picking subsets with islice()
Stateful filtering with dropwhile() and takewhile()
Two approaches to filtering with filterfalse() and filter()
Applying a function to data via starmap() and map()
Cloning iterators with tee()
The itertools recipes
Summary
Exercises
Optimize the find_first() function
Compare Chapter 4 with the itertools.pairwise() recipe
Compare Chapter 4 with itertools.tee() recipe
Splitting a dataset for training and testing purposes
Rank ordering
Chapter 9: Itertools for Combinatorics – Permutations and Combinations
Enumerating the Cartesian product
Reducing a product
Computing distances
Getting all pixels and all colors
Performance improvements
Rearranging the problem
Combining two transformations
Permuting a collection of values
Generating all combinations
Combinations with replacement
Recipes
Summary
Exercises
Alternative distance computations
Actual domain of pixel color values
Cribbage hand scoring
Chapter 10: The Functools Module
Function tools
Memoizing previous results with cache
Defining classes with total ordering
Applying partial arguments with partial()
Reducing sets of data with the reduce() function
Combining map() and reduce()
Using the reduce() and partial() functions
Using the map() and reduce() functions to sanitize raw data
Using the groupby() and reduce() functions
Avoiding problems with reduce()
Handling multiple types with singledispatch
Summary
Exercises
Compare string.join() and reduce()
Extend the comma_fix() function
Revise the clean_sum() function
Chapter 11: The Toolz Package
The itertools star map function
Reducing with operator module functions
Using the toolz package
Some itertoolz functions
Some dicttoolz functions
Some functoolz functions
Summary
Exercises
Replace true division with a fraction
Color file parsing
Anscombe's quartet parsing
Waypoint computations
Waypoint geofence
Callable object for the row_counter() function
Chapter 12: Decorator Design Techniques
Decorators as higher-order functions
Using the functools update_wrapper() function
Cross-cutting concerns
Composite design
Preprocessing bad data
Adding a parameter to a decorator
Implementing more complex decorators
Complicated design considerations
Summary
Exercises
Datetime conversions
Optimize a decorator
None-tolerant functions
Logging
Dry-run check
Chapter 13: The PyMonad Library
Downloading and installing
Functional composition and currying
Using curried higher-order functions
Functional composition with PyMonad
Functors – making everything a function
Using the lazy ListMonad() monad
Monad bind() function
Implementing simulation with monads
Additional PyMonad features
Summary
Exercises
Revise the arctangent series
Statistical computations
Data validation
Multiple models
Chapter 14: The Multiprocessing, Threading, and Concurrent.Futures Modules
Functional programming and concurrency
What concurrency really means
The boundary conditions
Sharing resources with process or threads
Where benefits will accrue
Using multiprocessing pools and tasks
Processing many large files
Parsing log files – gathering the rows
Parsing log lines into named tuples
Parsing additional fields of an Access object
Filtering the access details
Analyzing the access details
The complete analysis process
Using a multiprocessing pool for concurrent processing
Using apply() to make a single request
More complex multiprocessing architectures
Using the concurrent.futures module
Using concurrent.futures thread pools
Using the threading and queue modules
Using async functions
Designing concurrent processing
Summary
Exercises
Lazy parsing
Filter access path details
Add @cache decorators
Create sample data
Change the pipeline structure
Chapter 15: A Functional Approach to Web Services
The HTTP request-response model
Injecting state through cookies
Considering a server with a functional design
Looking more deeply into the functional view
Nesting the services
The WSGI standard
Raising exceptions during WSGI processing
Pragmatic web applications
Defining web services as functions
Flask application processing
The data access tier
Applying a filter
Serializing the results
Serializing data with JSON or CSV formats
Serializing data with XML and HTML
Tracking usage
Summary
Exercises
WSGI application: welcome
WSGI application: demo
Serializing data with XML
Serializing data with HTML
Packt page
Other Books You Might Enjoy
Index