Kapil | Clean Python | E-Book | www2.sack.de
E-Book

E-Book, Englisch, 274 Seiten

Kapil Clean Python

Elegant Coding in Python
1. ed
ISBN: 978-1-4842-4878-2
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark

Elegant Coding in Python

E-Book, Englisch, 274 Seiten

ISBN: 978-1-4842-4878-2
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark



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 metaclassesStart writing asynchronous Python immediatelyDiscover new features in Python   Who This Book Is ForReaders with a basic Python programming knowledge who want to improve their Python programming skills by learning right way to code in Python. 



Sunil Kapil has been in the software profession for the last ten years, writing production code in Python and several other languages. He has worked as a software engineer primarily on back-end services for web and mobile. He has developed, deployed, and maintained small to big projects in production that are being loved and used by millions of users. He has completed these projects with small to big teams in different professional environments for very well-known software companies around the world. He is also a passionate advocate of open source and continuously contributes to projects such as Zulip Chat and Black. He also works with non-profit organizations and contributes to their software projects on a volunteer basis.

Kapil Clean Python jetzt bestellen!

Autoren/Hrsg.


Weitere Infos & Material


1;Table of Contents;4
2;About the Author;10
3;About the Technical Reviewer;11
4;Acknowledgments;12
5;Introduction;13
6;Chapter 1: Pythonic Thinking;14
6.1;Write Pythonic Code;15
6.1.1;Naming;15
6.1.1.1;Variables and Functions;15
6.1.1.2;Classes;18
6.1.1.3;Constants;18
6.1.1.4;Function and Method Arguments;18
6.1.2;Expressions and Statements in Your Code;19
6.1.3;Embrace the Pythonic Way to Write Code;22
6.1.3.1;Prefer join Instead of In-Place String Concatenation;23
6.1.3.2;Consider Using is and is not Whenever You Need to Compare with None;23
6.1.3.3;Prefer Using is not Instead of not … is;24
6.1.3.4;Consider Using a Function Instead of a Lambda When Binding to an Identifier;24
6.1.3.5;Be Consistent with the return Statement;25
6.1.3.6;Prefer Using ““.startswith() and ””.endswith();26
6.1.3.7;Use the isinstance() Method Instead of type() for Comparison;27
6.1.3.8;Pythonic Way to Compare Boolean Values;27
6.1.3.9;Write Explicit Code for Context Manager;27
6.1.3.10;Use Linting Tools to Improve Python Code;29
6.2;Using Docstrings;31
6.2.1;Module-Level Docstrings;34
6.2.2;Make the Class Docstring Descriptive;36
6.2.3;Function Docstrings;37
6.2.4;Some Useful Docstring Tools;38
6.3;Write Pythonic Control Structures;39
6.3.1;Use List Comprehensions;39
6.3.2;Don’t Make Complex List Comprehension;41
6.3.3;Should You Use a Lambda?;43
6.3.4;When to Use Generators vs. List Comprehension;44
6.3.5;Why Not to Use else with Loops;45
6.3.6;Why range Is Better in Python 3;48
6.4;Raising Exceptions;50
6.4.1;Frequently Raised Exceptions;50
6.4.2;Leverage finally to Handle Exceptions;52
6.4.3;Create Your Own Exception Class;54
6.4.4;Handle Only Specific Exceptions;56
6.4.5;Watch Out for Third-Party Exceptions;58
6.4.6;Prefer to Have Minimum Code Under try;59
6.5;Summary;61
7;Chapter 2: Data Structures;62
7.1;Common Data Structures;62
7.1.1;Use Sets for Speed;63
7.1.2;Use namedtuple for Returning and Accessing Data;65
7.1.2.1;Access the Data;65
7.1.2.2;Return the Data;66
7.1.3;Understanding str, Unicode, and byte;68
7.1.4;Use Lists Carefully and Prefer Generators;70
7.1.5;Use zip to Process a List;73
7.1.6;Take Advantage of Python’s Built-in Functions;75
7.1.6.1;collections;75
7.1.6.2;csv;75
7.1.6.3;datetime and time;75
7.1.6.4;math;75
7.1.6.5;re;76
7.1.6.6;tempfile;76
7.1.6.7;itertools;76
7.1.6.8;functools;76
7.1.6.9;sys and os;76
7.1.6.10;subprocess;77
7.1.6.11;logging;77
7.1.6.12;json;77
7.1.6.13;pickle;77
7.1.6.14;__future__;77
7.2;Take Advantage of Dictionary;78
7.2.1;When to Use a Dictionary vs. Other Data Structures;78
7.2.2;collections;79
7.2.2.1;Counter;79
7.2.2.2;deque;80
7.2.2.3;defaultdict;81
7.2.2.4;namedtuple;82
7.2.2.5;ordereddict;82
7.2.3;Ordered Dictionary vs. Default Dictionary vs. Normal Dictionary;83
7.2.4;switch Statement Using Dictionary;85
7.2.5;Ways to Merge Two Dictionaries;86
7.2.6;Pretty Printing a Dictionary;87
7.3;Summary;88
8;Chapter 3: Writing Better Functions and Classes;89
8.1;Functions;89
8.1.1;Create Small Functions;90
8.1.2;Return Generators;92
8.1.3;Raise Exceptions Instead of Returning None;94
8.1.4;Add Behavior Using the default and keyword Arguments;97
8.1.5;Do Not Return None Explicitly;98
8.1.6;Be Defensive While Writing a Function;101
8.1.6.1;Logging;101
8.1.6.2;Unit Test;103
8.1.7;Use a Lambda as a Single Expression;104
8.2;Classes;106
8.2.1;Right Size of Class?;106
8.2.2;Class Structure;107
8.2.2.1;Class Variables;109
8.2.2.2;__init__;109
8.2.2.3;Special Python Methods;110
8.2.2.4;Class Methods;110
8.2.2.5;Static Methods;110
8.2.2.6;Instance Methods;110
8.2.2.7;Private Methods;111
8.2.3;Right Ways to Use @property;111
8.2.4;When to Use Static Methods?;114
8.2.5;Use Abstract Class Inheritance the Pythonic Way;116
8.2.6;Use @classmethod to Access Class State;118
8.2.7;Use the public Attribute Instead of private;119
8.3;Summary;121
9;Chapter 4: Working with Modules and Metaclasses;122
9.1;Modules and Metaclasses;122
9.2;How Modules Can Help to Organize Code;124
9.3;Take Advantage of the __init__ File;127
9.4;Import Functions and Classes from Modules in the Right Way;130
9.4.1;Use __all__ to Prevent Imports;132
9.5;When to Use Metaclasses;134
9.6;Use __new__ for Validating Subclasses;135
9.7;Why __slots__ Are Useful;138
9.8;Change Class Behavior Using Metaclasses;141
9.9;Learn About Python Descriptors;145
9.10;Summary;147
10;Chapter 5: Decorators and Context Managers;149
10.1;Decorators;150
10.1.1;What Are Decorators, and Why Are They Useful?;150
10.1.2;Understanding Decorators;151
10.1.3;Modify Behavior Using Decorators;154
10.1.4;Using Multiple Decorators;157
10.1.5;Decorators Accept Arguments;158
10.1.6;Consider Using a Library for Decorators;159
10.1.7;Class Decorators for Maintaining State and Validating Parameters;162
10.2;Context Manager;165
10.2.1;Context Managers and Their Usefulness;165
10.2.2;Understanding Context Managers;167
10.2.3;Using contextlib to Build a Context Manager;170
10.2.4;Some Practical Examples of Using a Context Manager;171
10.2.4.1;Accessing a Database;171
10.2.4.2;Writing Tests;172
10.2.4.3;Shared Resource;173
10.2.4.4;Remote Connection;174
10.3;Summary;175
11;Chapter 6: Generators and Iterators;176
11.1;Take Advantage of Iterators and Generators;176
11.1.1;Understanding Iterators;176
11.1.2;What Are Generators?;179
11.1.3;When to Use Iterators;180
11.1.4;Using itertools;183
11.1.4.1;combinations();183
11.1.4.2;permuations();183
11.1.4.3;product();184
11.1.4.3.1;count();184
11.1.4.3.2;groupby();185
11.1.5;Why Generators Are Useful;185
11.1.6;List Comprehension vs. Iterators;186
11.2;Take Advantage of the yield Keyword;186
11.2.1;yield from;188
11.2.2;yield Is Faster Compared to a Data Structure;189
11.3;Summary;189
12;Chapter 7: Utilize New Python Features;190
12.1;Asynchronous Programming;191
12.1.1;Introducing async in Python;192
12.1.2;How It Works;196
12.1.2.1;Coroutine Function;197
12.1.2.2;Coroutine Object;197
12.1.2.3;asyncio.run();197
12.1.2.4;await;197
12.1.2.5;Tasks;201
12.1.2.6;Awaitable Objects;203
12.1.2.6.1;Coroutines;203
12.1.2.6.2;Tasks;205
12.1.2.6.3;Futures;206
12.1.2.7;Timeouts;208
12.1.3;Async Generators;209
12.1.3.1;Async Comprehensions;210
12.1.3.2;Async Iterators;211
12.1.3.3;Third-Party Libraries to Consider for Async Code;214
12.1.3.3.1;Curio;214
12.1.3.3.2;Trio;216
12.2;Typing in Python;219
12.2.1;Types in Python;220
12.2.2;typing Module;221
12.2.2.1;Union;221
12.2.2.2;Any;222
12.2.2.3;Tuple;222
12.2.2.4;TypeVar and Generics;223
12.2.2.5;Optional;223
12.2.3;Do Data Types Slow Code?;224
12.2.4;How Typing Helps to Write Better Code;224
12.2.5;Typing Pitfalls;225
12.3;super() Method;226
12.4;Type Hinting;226
12.5;Better Path Handling Using pathlib;227
12.6;print() Is a Function Now;227
12.7;f-string;227
12.8;Keyword Only Arguments;228
12.9;Preserving the Order of a Dictionary;229
12.10;Iterable Unpacking;229
12.11;Summary;229
13;Chapter 8: Debugging and  Testing Python Code;230
13.1;Debugging;231
13.1.1;Debugging Tools;231
13.1.1.1;pdb;231
13.1.1.2;ipdb;232
13.1.1.3;pudb;233
13.1.2;breakpoint;236
13.1.3;Use the Logging Module Instead of print in Production Code;236
13.1.3.1;Classes and Functions in Logging;238
13.1.4;Use the metrics Library for Identifying Bottlenecks;243
13.1.5;How IPython Is Helpful;244
13.2;Testing;247
13.2.1;Why Testing Is Important;247
13.2.2;Pytest vs. UnitTest;248
13.2.3;Property Testing;253
13.2.4;How to Create a Report for Testing;254
13.2.5;Automate Unit Tests;255
13.2.6;Getting Your Code Ready for Production;256
13.2.7;Run Unit and Integration Tests in Python;256
13.2.7.1;Use Linting to Make Code Consistent;257
13.2.7.1.1;flake8;258
13.2.7.1.2;pylint;258
13.2.7.2;Use Code Coverage to Check for Tests;258
13.2.7.3;Use virtualenv for Your Project;259
13.3;Summary;260
14;Appendix: Some Awesome Python Tools;261
14.1;Sphinx;261
14.2;Coverage;263
14.3;pre-commit;264
14.4;Pyenv for virtualenv;265
14.5;Jupyter Lab;265
14.6;Pycharm/VSCode/Sublime;266
14.7;Flake8/Pylint;266
15;Index;268



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.