Description
The Phyton language has gained its popularity thanks to the availability of libraries and the lack of difficulties in preparing the work environment and starting programming. Despite its many advantages, the language has many hidden traps that can be difficult if you are not aware of them.
This book will help the reader to take full advantage of the Python language. With realistic examples, the author presents intricacies and little-known idioms that affect the behavior and performance of the code. The reader will learn about the most important issues of the Python language, such as: functions, classes, inheritance, metaclasses or attributes. Moreover, he will learn how to choose the most effective way to achieve the intended effects. He will also learn how to write code that will be easy to understand, maintain and improve.
Each chapter contains detailed descriptions of examples, formulated in a clear, easy to understand way.
The book addresses the following issues:
- Hints on all major programming areas in Python 3.x and 2.x with detailed explanations and examples.
- Best practices for creating features that clearly express intentions, promote multiple use of the same code and help avoid mistakes.
- Discuss the topic of proper expression of program behavior using classes and objects.
- Hints to help avoid pitfalls when using metaclasses and dynamic attributes.
- Presentation of a much more effective approach to concurrency and parallelism.
- Presentation of better techniques and ways of using the Python built-in modules.
- Presentation of tools and best practices used when working together on projects.
- Error recovery, testing and optimization solutions, leading to improved code quality and performance.
Table of contents
Introduction (11)
Thanks (15)
About the author (17)
Chapter 1 Python spirit programming (19)
- Method 1: Determine the Python version used (19)
- Mode 2 Use PEP style 8 (21)
- Mode 3 Differences between types of bytes, str and unicode (23)
- Mode 4: Choose auxiliary functions instead of complex expressions (26)
- Method 5. Divide the sequences quantitatively (29)
- Mode 6 Avoid the use of start, end and step values indices in a single split operation (31)
- Use folding lists instead of map() and filter() functions (33)
- Mode 8 Avoid more than two words in the folding list (35)
- Mode 9 Consider using an expression generator for large folding lists (36)
- Method 10: prefer the enumerate() function to range() (38)
- Method 11: Using zip() to process iterators simultaneously (39)
- Avoid the blocks other than the loops for and while (41)
- Method 13: Using the advantages of all blocks in the try-except-else-finally construction (44)
Chapter 2 Functions (47)
- Mode 14 Prefer exceptions to None (47)
- See how closures interact with the scope of the variable (49)
- Consider using generators instead of returning letters (54)
- Mode 17: Keep a defensive attitude when iterating through arguments (56)
- Mode 18: Reduce visual confusion with a variable number of positional arguments (61)
- Mode 19: Definition of optional behaviour by means of keyword arguments (63)
- Method 20: Using None and docstring to dynamically determine the default arguments (66)
- Mode 21: Force the code to be legible by using keyword arguments only (69)
Chapter 3 Classes and succession (73)
- Mode 22: Prefer auxiliary classes over dictionaries and staples (73)
- Method 23: For simple interfaces, accept functions instead of classes (78)
- Mode 24: Using @classmethod polymorphism for general object creation (82)
- Mode 25: Initialisation of the parent class by means of the super() call (87)
- Use multiple inheritance only in tool classes (91)
- Mode 27: Prefer public over private attributes (95)
- Mode 28 Inheritance from collections.abc in containers of non-standard types (99)
Chapter 4 Metaclasses and attributes (105)
- Use normal attributes instead of getter and setter methods (105)
- Consider using @property instead of refactoring the attributes (109)
- Method 31 Use descriptors to repeatedly use methods decorated by @property (113)
- Use the methods __getattr__(), __getattribute__() and __setattr__() for delayed attributes (117)
- Method 33: check the subclasses with the Metaclas (122)
- Mode 34 Register existing classes together with methaclasses (124)
- Mode 35 Attribute annotations for classes add by means of the metaclas (128)
Chapter 5 Coexistence and parallelism (131)
- Mode 36 Use the subprocess module to manage posterity processes (132)
- Use of threads for input-output blocking operations, avoiding parallelism (136)
- Method 38: Use the Lock class to avoid a race state in threads (140)
- Mode 39 Use Queue class to coordinate work between threads (143)
- Consider using co-programmes to perform multiple functions simultaneously (150)
- Consider using concurrent.futures() to obtain true parallelism (158)
Chapter 6 Built-in modules (163)
- Define function decorators with functools.wraps (163)
- Mode 43 Consider using the contextlib and with commands to achieve multiple use of the try-finally constructed (166)
- Method 44 Reliable use of pickles with copyreg (169)
- Use the datetime module instead of time when handling local time (174)
- Mode 46: Use built-in algorithms and data structures (178)
- When precision is important, use the decimal module (183)
- Way 48. When to look for modules developed by the community? (185)
Chapter 7 Cooperation (187)
- Method 49: Create a docstring for each function, class and module (187)
- Use packages to organize modules and provide stable APIs (191)
- Mode 51: Define the main Exception to isolate the invoking component from the API (196)
- See how to break the circle of relationships (199)
- Use virtual environments for isolated and repetitive relationships (204)
Chapter 8 Production (211)
- Mode 54 Consider using a code with module coverage to configure the deployment environment (211)
- Method 55 Use repr text strings to debug the output (214)
- Method 56. Test everything with unittest (217)
- Method 57: Consider interactive bug fixing with pdb (220)
- Method 58: Before optimization, perform profiling (222)
- Method 59. Use the tracemalloc module, to learn how to use the memory and detect memory leaks (226)
Scorch (229)
|