Book Image

Clean Code in Python

By : Mariano Anaya
2 (1)
Book Image

Clean Code in Python

2 (1)
By: Mariano Anaya

Overview of this book

Python is currently used in many different areas such as software construction, systems administration, and data processing. In all of these areas, experienced professionals can find examples of inefficiency, problems, and other perils, as a result of bad code. After reading this book, readers will understand these problems, and more importantly, how to correct them. The book begins by describing the basic elements of writing clean code and how it plays an important role in Python programming. You will learn about writing efficient and readable code using the Python standard library and best practices for software design. You will learn to implement the SOLID principles in Python and use decorators to improve your code. The book delves more 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 software problems by implementing design patterns in your code. In the final chapter we break down a monolithic application to a microservice one, starting from the code as the basis for a solid platform. By the end of the book, you will be proficient in applying industry approved coding practices to design clean, sustainable and readable Python code.
Table of Contents (12 chapters)

What this book covers

Chapter 1, Introduction, Code Formatting, and Tools, is an introduction to the main tools you need to set up a development environment in Python. We cover the basics a Python developer is recommended to know to start working with the language, as well as some guidelines for maintaining readable code in the project, such as tools for static analysis, documentation, type checking, and code formatting.

Chapter 2, Pythonic Code, looks at the first idioms in Python, which we will continue to use in the following chapters. We cover the particular features of Python, how they should be used, and we start building knowledge around the idea that Pythonic code is in general much better quality code.

Chapter 3, General Traits of Good Code, reviews general principles of software engineering that focus on writing maintainable code. We explore the idea and apply the concepts with the tools in the language.

Chapter 4, The SOLID Principles, covers a set of design principles for object-oriented software design. This acronym is part of the language or jargon of software engineering, and we see how each one of them can be applied to Python. Arguably not all of them are entirely applicable due to the nature of the language.

Chapter 5, Using Decorators to Improve Our Code, looks at one of the greatest features of Python. After understanding how to create decorators (for functions and classes), we put them in action for reusing code, separating responsibilities, and creating more granular functions.

Chapter 6, Getting More Out of Our Objects with Descriptors, explores descriptors in Python, which take object-oriented design to a new level. While this is a feature more related to frameworks and tools, we can see how to improve the readability of our code with descriptors, and also reuse code.

Chapter 7, Using Generators, shows that generators are probably the best feature of Python. The fact that iteration is a core component of Python could make us think that it leads to a new programming paradigm. By using generators and iterators in general, we can think about the way we write our programs. With the lessons learned from generators, we go further and learn about coroutines in Python, and the basics of asynchronous programming.

Chapter 8, Unit Testing and Refactoring, discusses the importance of unit tests in any code base that claims to be maintainable. The chapter reviews the importance of unit tests, and we explore the main frameworks for this (unittest and pytest).

Chapter 9, Common Design Patterns, reviews how to implement the most common design patterns in Python, not from the point of view of solving a problem, but by examining how they solve problems by leveraging a better and more maintainable solution. The chapter mentions the peculiarities of Python that have made some of the design patterns invisible and takes a pragmatic approach to implement some of them.

Chapter 10, Clean Architecture, focuses on the idea that clean code is the base of a good architecture. All those details we mentioned in the first chapter, and everything else revisited along the way, will play a critical role in the entire design when the system is deployed.