๐”– Scriptorium
โœฆ   LIBER   โœฆ

๐Ÿ“

Clean Code in Python: Develop maintainable and efficient code, 2nd Edition

โœ Scribed by Mariano Anaya


Publisher
Packt Publishing Ltd
Year
2021
Tongue
English
Leaves
423
Category
Library

โฌ‡  Acquire This Volume

No coin nor oath required. For personal study only.

โœฆ Synopsis


Improve your software engineering practices to tackle inefficiencies, errors, and other perils that emerge due to bad code

Key Features

  • Enhance your coding skills to increase efficiency as well as reflect the new features introduced in Python 3.9
  • Understand how to apply microservices to your legacy systems by implementing practical techniques
  • Learn to implement the refactoring techniques and SOLID principles in Python

Book Description

The Python language is immensely prevalent in numerous areas, such as software construction, systems administration, and data processing. Experienced professionals in every field face the challenges of disorganization, poor readability, and low testability as a result of unstructured code.

With updated code and revised content aligned to the new features of Python 3.9, this second edition of Clean Code in Python will provide you with all the tools you need to overcome these obstacles and manage your projects successfully.

The book begins by describing the basic elements of writing clean code and how it plays a key role in Python programming. You will learn about writing efficient and readable code using the Python standard library and best practices for software design.

The book delves deeply into object-oriented programming in Python and shows you how to use objects with descriptors and generators. It will also show you the design principles of software testing and how to resolve problems by implementing software design patterns in your code. In the final chapter, we break down a monolithic application into a microservices based one starting from the code as the basis for a solid platform.

By the end of this clean code book, you will be proficient in applying industry-approved coding practices to design clean, sustainable, and readable real-world Python code.

What you will learn

  • Set up a productive development environment by leveraging automatic tools
  • Leverage the magic methods in Python to write better code, abstracting complexity away and encapsulating details
  • Create advanced object-oriented designs using unique features of Python, such as descriptors
  • Eliminate duplicated code by creating powerful abstractions using software engineering principles of object-oriented design
  • Create Python-specific solutions using decorators and descriptors
  • Refactor code effectively with the help of unit tests
  • Build the foundations for solid architecture with a clean code base as its cornerstone

Who this book is for

This book will appeal to team leads, software architects, and senior software engineers who want to learn Python coding techniques and work on their legacy systems to save cost and improve efficiency. The book assumes that you have a strong understanding of programming.

โœฆ Table of Contents


Cover
Copyright
Packt Page
Contributors
Table of Contents
Preface
Chapter 1: Introduction, Code Formatting, and Tools
Introduction
The meaning of clean code
The importance of having clean code
Some exceptions
Code formatting
Adhering to a coding style guide on your project
Documentation
Code comments
Docstrings
Annotations
Do annotations replace docstrings?
Tooling
Checking type consistency
Generic validations in code
Automatic formatting
Setup for automatic checks
Summary
References
Chapter 2: Pythonic Code
Indexes and slices
Creating your own sequences
Context managers
Implementing context managers
Comprehensions and assignment expressions
Properties, attributes, and different types of methods for objects
Underscores in Python
Properties
Creating classes with a more compact syntax
Iterable objects
Creating iterable objects
Creating sequences
Container objects
Dynamic attributes for objects
Callable objects
Summary of magic methods
Caveats in Python
Mutable default arguments
Extending built-in types
A brief introduction to asynchronous code
Summary
References
Chapter 3: General Traits of Good Code
Design by contract
Preconditions
Postconditions
Pythonic contracts
Design by contract โ€“ conclusions
Defensive programming
Error handling
Value substitution
Exception handling
Using assertions in Python
Separation of concerns
Cohesion and coupling
Acronyms to live by
DRY/OAOO
YAGNI
KIS
EAFP/LBYL
Inheritance in Python
When inheritance is a good decision
Anti-patterns for inheritance
Multiple inheritance in Python
Method Resolution Order (MRO)
Mixins
Arguments in functions and methods
How function arguments work in Python
How arguments are copied to functions
Variable number of arguments
Positional-only parameters
Keyword-only arguments
The number of arguments in functions
Function arguments and coupling
Compact function signatures that take too many arguments
Final remarks on good practices for software design
Orthogonality in software
Structuring the code
Summary
References
Chapter 4: The SOLID Principles
The single responsibility principle
A class with too many responsibilities
Distributing responsibilities
The open/closed principle
Example of maintainability perils for not following the OCP
Refactoring the events system for extensibility
Extending the events system
Final thoughts about the OCP
Liskov's substitution principle
Detecting LSP issues with tools
Using mypy to detect incorrect method signatures
Detecting incompatible signatures with pylint
More subtle cases of LSP violations
Remarks on the LSP
Interface segregation
An interface that provides too much
The smaller the interface, the better
How small should an interface be?
Dependency inversion
A case of rigid dependencies
Inverting the dependencies
Dependency injection
Summary
References
Chapter 5: Using Decorators to Improve Our Code
What are decorators in Python?
Function decorators
Decorators for classes
Other types of decorator
More advanced decorators
Passing arguments to decorators
Decorators with nested functions
Decorator objects
Decorators with default values
Decorators for coroutines
Extended syntax for decorators
Good uses for decorators
Adapting function signatures
Validating parameters
Tracing code
Effective decorators โ€“ avoiding common mistakes
Preserving data about the original wrapped object
Dealing with side effects in decorators
Incorrect handling of side effects in a decorator
Requiring decorators with side effects
Creating decorators that will always work
Decorators and clean code
Composition over inheritance
The DRY principle with decorators
Decorators and separation of concerns
Analysis of good decorators
Summary
References
Chapter 6: Getting More Out of Our Objects with Descriptors
A first look at descriptors
The machinery behind descriptors
Exploring each method of the descriptor protocol
The get method
The set method
The delete method
The set name method
Types of descriptors
Non-data descriptors
Data descriptors
Descriptors in action
An application of descriptors
A first attempt without using descriptors
The idiomatic implementation
Different forms of implementing descriptors
The issue of shared state
Accessing the dictionary of the object
Using weak references
More considerations about descriptors
Reusing code
An alternative to class decorators
Analysis of descriptors
How Python uses descriptors internally
Functions and methods
Built-in decorators for methods
Slots
Implementing descriptors in decorators
Final remarks about descriptors
Interface of descriptors
Object-oriented design of the descriptors
Type annotations on descriptors
Summary
References
Chapter 7: Generators, Iterators, and Asynchronous Programming
Technical requirements
Creating generators
A first look at generators
Generator expressions
Iterating idiomatically
Idioms for iteration
The next() function
Using a generator
Itertools
Simplifying code through iterators
The iterator pattern in Python
Coroutines
The methods of the generator interface
close()
throw(ex_type[, ex_value[, ex_traceback]])
send(value)
More advanced coroutines
Returning values in coroutines
Delegating into smaller coroutines โ€“ the 'yield from' syntax
Asynchronous programming
Magic asynchronous methods
Asynchronous context managers
Other magic methods
Asynchronous iteration
Asynchronous generators
Summary
References
Chapter 8: Unit Testing and Refactoring
Design principles and unit testing
A note about other forms of automated testing
Unit testing and agile software development
Unit testing and software design
Defining the boundaries of what to test
Tools for testing
Frameworks and libraries for unit testing
unittest
pytest
Code coverage
Mock objects
Refactoring
Evolving our code
Production code isn't the only one that evolves
More about testing
Property-based testing
Mutation testing
Common themes in testing
Boundaries or limit values
Classes of equivalence
Edge cases
A brief introduction to test-driven development
Summary
References
Chapter 9: Common Design Patterns
Design pattern considerations in Python
Design patterns in action
Creational patterns
Factories
Singleton and shared state (monostate)
Builder
Structural patterns
Adapter
Composite
Decorator
Facade
Behavioral patterns
Chain of responsibility
The template method
Command
State
The null object pattern
Final thoughts about design patterns
The influence of patterns over the design
Design patterns as theory
Names in our models
Summary
References
Chapter 10: Clean Architecture
From clean code to clean architecture
Separation of concerns
Monolithic applications and microservices
Abstractions
Software components
Packages
Managing dependencies
Other considerations when managing dependencies
Artifact versions
Docker containers
Use case
The code
Domain models
Calling from the application
Adapters
The services
Analysis
The dependency flow
Limitations
Testability
Intention revealing
Summary
References
Summing it all up
Share your experience
Other Books You May Enjoy
Index


๐Ÿ“œ SIMILAR VOLUMES


Clean Code in Python: Develop maintainab
โœ Mariano Anaya ๐Ÿ“‚ Library ๐Ÿ“… 2021 ๐Ÿ› Packt Publishing ๐ŸŒ English

Code <p><b>Tackle inefficiencies and errors the Pythonic way</b></p><h4>Key Features</h4><ul><li>Enhance your coding skills using the new features introduced in Python 3.9</li><li>Implement the refactoring techniques and SOLID principles in Python</li><li>Apply microservices to your legacy systems

Python Clean Code: Best Practices and Te
โœ Nash Maverick ๐Ÿ“‚ Library ๐Ÿ“… 2023 ๐Ÿ› Vigilant Vaults ๐ŸŒ English

<p><span>"</span><span>Python Clean Code</span><span>" is a comprehensive guidebook for Python developers who want to improve the quality of their code by adopting best practices for writing clean, readable, and maintainable code. The book is authored by </span><span>Nash Maverick</span><span>, who

Python Clean Code: Best Practices and Te
โœ Nash Maverick ๐Ÿ“‚ Library ๐Ÿ“… 2023 ๐Ÿ› Vigilant Vaults ๐ŸŒ English

<p><span>"</span><span>Python Clean Code</span><span>" is a comprehensive guidebook for Python developers who want to improve the quality of their code by adopting best practices for writing clean, readable, and maintainable code. The book is authored by </span><span>Nash Maverick</span><span>, who

Robust Python: Write Clean and Maintaina
โœ Patrick Viafore ๐Ÿ“‚ Library ๐Ÿ“… 2021 ๐Ÿ› O'Reilly Media ๐ŸŒ English

Does it seem like your Python projects are getting bigger and bigger? Are you feeling the pain as your codebase expands and gets tougher to debug and maintain? Python is an easy language to learn and use, but that also means systems can quickly grow beyond comprehension. Thankfully, Python has featu