Clean Python: elegant coding in Python
â Scribed by Kapil, Sunil
- Publisher
- Apress
- Year
- 2019
- Tongue
- English
- Leaves
- 274
- Category
- Library
No coin nor oath required. For personal study only.
⌠Synopsis
Discover the right way to code in Python. This book provides the tips and techniques you need to produce cleaner, error-free, and eloquent Python projects.
Your journey to better code starts with understanding the importance of formatting and documenting your code for maximum readability, utilizing built-in data structures and Python dictionary for improved maintainability, and working with modules and meta-classes to effectively organize your code. You will then dive deep into the new features of the Python language and learn how to effectively utilize them. Next, you will decode key concepts such as asynchronous programming, Python data types, type hinting, and path handling. Learn tips to debug and conduct unit and integration tests in your Python code to ensure your code is ready for production. The final leg of your learning journey equips you with essential tools for version management, managing live code, and intelligent code completion.
After reading and using this book, you will be proficient in writing clean Python code and successfully apply these principles to your own Python projects.
What You'll Learn
Use the right expressions and statements in your Python code
Create and assess Python Dictionary
Work with advanced data structures in Python
Write better modules, classes, functions, and metaclasses
Start writing asynchronous Python immediately
Discover new features in Python
Who This Book Is For
Readers with a basic Python programming knowledge who want to improve their Python programming skills by learning right way to code in Python.
⌠Table of Contents
Table of Contents......Page 4
About the Author......Page 10
About the Technical Reviewer......Page 11
Acknowledgments......Page 12
Introduction......Page 13
Chapter 1: Pythonic Thinking......Page 14
Variables and Functions......Page 15
Function and Method Arguments......Page 18
Expressions and Statements in Your Code......Page 19
Embrace the Pythonic Way to Write Code......Page 22
Consider Using is and is not Whenever You Need to Compare with None......Page 23
Consider Using a Function Instead of a Lambda When Binding to an Identifier......Page 24
Be Consistent with the return Statement......Page 25
Prefer Using ââ.startswith() and ââ.endswith()......Page 26
Write Explicit Code for Context Manager......Page 27
Use Linting Tools to Improve Python Code......Page 29
Using Docstrings......Page 31
Module-Level Docstrings......Page 34
Make the Class Docstring Descriptive......Page 36
Function Docstrings......Page 37
Some Useful Docstring Tools......Page 38
Use List Comprehensions......Page 39
Donât Make Complex List Comprehension......Page 41
Should You Use a Lambda?......Page 43
When to Use Generators vs. List Comprehension......Page 44
Why Not to Use else with Loops......Page 45
Why range Is Better in Python 3......Page 48
Frequently Raised Exceptions......Page 50
Leverage finally to Handle Exceptions......Page 52
Create Your Own Exception Class......Page 54
Handle Only Specific Exceptions......Page 56
Watch Out for Third-Party Exceptions......Page 58
Prefer to Have Minimum Code Under try......Page 59
Summary......Page 61
Common Data Structures......Page 62
Use Sets for Speed......Page 63
Access the Data......Page 65
Return the Data......Page 66
Understanding str, Unicode, and byte......Page 68
Use Lists Carefully and Prefer Generators......Page 70
Use zip to Process a List......Page 73
math......Page 75
sys and os......Page 76
future......Page 77
When to Use a Dictionary vs. Other Data Structures......Page 78
Counter......Page 79
deque......Page 80
defaultdict......Page 81
ordereddict......Page 82
Ordered Dictionary vs. Default Dictionary vs. Normal Dictionary......Page 83
switch Statement Using Dictionary......Page 85
Ways to Merge Two Dictionaries......Page 86
Pretty Printing a Dictionary......Page 87
Summary......Page 88
Functions......Page 89
Create Small Functions......Page 90
Return Generators......Page 92
Raise Exceptions Instead of Returning None......Page 94
Add Behavior Using the default and keyword Arguments......Page 97
Do Not Return None Explicitly......Page 98
Logging......Page 101
Unit Test......Page 103
Use a Lambda as a Single Expression......Page 104
Right Size of Class?......Page 106
Class Structure......Page 107
init......Page 109
Instance Methods......Page 110
Right Ways to Use @property......Page 111
When to Use Static Methods?......Page 114
Use Abstract Class Inheritance the Pythonic Way......Page 116
Use @classmethod to Access Class State......Page 118
Use the public Attribute Instead of private......Page 119
Summary......Page 121
Modules and Metaclasses......Page 122
How Modules Can Help to Organize Code......Page 124
Take Advantage of the init File......Page 127
Import Functions and Classes from Modules in the Right Way......Page 130
Use all to Prevent Imports......Page 132
When to Use Metaclasses......Page 134
Use new for Validating Subclasses......Page 135
Why slots Are Useful......Page 138
Change Class Behavior Using Metaclasses......Page 141
Learn About Python Descriptors......Page 145
Summary......Page 147
Chapter 5: Decorators and Context Managers......Page 149
What Are Decorators, and Why Are They Useful?......Page 150
Understanding Decorators......Page 151
Modify Behavior Using Decorators......Page 154
Using Multiple Decorators......Page 157
Decorators Accept Arguments......Page 158
Consider Using a Library for Decorators......Page 159
Class Decorators for Maintaining State and Validating Parameters......Page 162
Context Managers and Their Usefulness......Page 165
Understanding Context Managers......Page 167
Using contextlib to Build a Context Manager......Page 170
Accessing a Database......Page 171
Writing Tests......Page 172
Shared Resource......Page 173
Remote Connection......Page 174
Summary......Page 175
Understanding Iterators......Page 176
What Are Generators?......Page 179
When to Use Iterators......Page 180
permuations()......Page 183
count()......Page 184
Why Generators Are Useful......Page 185
Take Advantage of the yield Keyword......Page 186
yield from......Page 188
Summary......Page 189
Chapter 7: Utilize New Python Features......Page 190
Asynchronous Programming......Page 191
Introducing async in Python......Page 192
How It Works......Page 196
await......Page 197
Tasks......Page 201
Coroutines......Page 203
Tasks......Page 205
Futures......Page 206
Timeouts......Page 208
Async Generators......Page 209
Async Comprehensions......Page 210
Async Iterators......Page 211
Curio......Page 214
Trio......Page 216
Typing in Python......Page 219
Types in Python......Page 220
Union......Page 221
Tuple......Page 222
Optional......Page 223
How Typing Helps to Write Better Code......Page 224
Typing Pitfalls......Page 225
Type Hinting......Page 226
f-string......Page 227
Keyword Only Arguments......Page 228
Summary......Page 229
Chapter 8: Debugging and Testing Python Code......Page 230
pdb......Page 231
ipdb......Page 232
pudb......Page 233
Use the Logging Module Instead of print in Production Code......Page 236
Classes and Functions in Logging......Page 238
Use the metrics Library for Identifying Bottlenecks......Page 243
How IPython Is Helpful......Page 244
Why Testing Is Important......Page 247
Pytest vs. UnitTest......Page 248
Property Testing......Page 253
How to Create a Report for Testing......Page 254
Automate Unit Tests......Page 255
Run Unit and Integration Tests in Python......Page 256
Use Linting to Make Code Consistent......Page 257
Use Code Coverage to Check for Tests......Page 258
Use virtualenv for Your Project......Page 259
Summary......Page 260
Sphinx......Page 261
Coverage......Page 263
pre-commit......Page 264
Jupyter Lab......Page 265
Flake8/Pylint......Page 266
Index......Page 268
đ SIMILAR VOLUMES
Discover the right way to code in Python. This book provides the tips and techniques you need to produce cleaner, error-free, and eloquent Python projects.<br /><br />Your journey to better code starts with understanding the importance of formatting and documenting your code for maximum readability,
Discover the right way to code in Python. This book provides the tips and techniques you need to produce cleaner, error-free, and eloquent Python projects. Your journey to better code starts with understanding the importance of formatting and documenting your code for maximum readability, utilizing