Previous Next

The Pythonic Way (Sonal Raj) (z-library.sk, 1lib.sk, z-lib.sk)

Author: Sonal Raj

技术

Learn to build and manage better software with clean, intuitive, scalable, maintainable, and high-performance Python code. KEY FEATURES ● Comparative analysis of regular and Pythonic coding constructs. ● Illustrates application design paradigms for Python projects. ● Detailed pointers on optimal data processing and application design. ● Highlights accepted conventions for testing and managing production code. DESCRIPTION ‘The Pythonic Way' acquaints you with Python's capabilities beyond basic syntax. This book will help you understand widely accepted Pythonic constructs and procedures, thus enabling you to write reliable, optimized, and modular applications. You'll learn about Pythonic data structures, class and object creation, and more. The book then delves into some of Python's lesser-known but incredibly powerful functionalities such as meta-programming, decorators, context managers, generators, and iterators. Additionally, you'll learn how to accelerate computations by using Pandas Series and Dataframes. You will be introduced to various design patterns that work well with Python applications. Finally, we'll discuss testing frameworks and best practices for testing, packaging, launching, and publishing applications in production environments. This book will empower you as you transition from beginner or competitive Python coding to industry-standard Python software development. Intermediate Python developers will gain a deeper understanding of the language's nuances, enabling them to create better software. WHAT YOU WILL LEARN ● Understand common practices for writing scalable and legible Python code. ● Create robust and maintainable production codebases for time and space performant applications. ● Master effective data processing practices and features like generators and decorators to improve complex computations on large datasets. ● Get familiar with Pythonic design patterns for secure, large-scale applications. ● Learn to organize your project’s code into

📄 File Format: PDF
💾 File Size: 6.0 MB
7
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
The Pythonic Way An Architect’s Guide to Conventions and Best Practices for the Design, Development, Testing and Management of Enterprise Python Code Sonal Raj www.bpbonline.com
📄 Page 3
FIRST EDITION 2022 Copyright © BPB Publications, India ISBN: 978-93-91030-12-4 All Rights Reserved. No part of this publication may be reproduced, distributed or transmitted in any form or by any means or stored in a database or retrieval system, without the prior written permission of the publisher with the exception to the program listings which may be entered, stored and executed in a computer system, but they can not be reproduced by the means of publication, photocopy, recording, or by any electronic and mechanical means. LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY The information contained in this book is true to correct and the best of author’s and publisher’s knowledge. The author has made every effort to ensure the accuracy of these publications, but publisher cannot be held responsible for any loss or damage arising from any information in this book. All trademarks referred to in the book are acknowledged as properties of their respective owners but BPB Publications cannot guarantee the accuracy of this information.
📄 Page 4
www.bpbonline.com
📄 Page 5
Dedicated to My parents, Jugal and Sanjukta, All thanks to you. And to my lovely wife, Srishti, You are the light of my life.
📄 Page 6
About the Author Sonal Raj is an Engineer, Mathematician, Data Scientist, and Python evangelist from India, who has carved a niche in the Financial Services domain. He is a Goldman Sachs and D.E.Shaw alumnus who currently heads the data analytics and research efforts for a high frequency trading firm. Sonal holds dual masters in Computer Science and Business Management and is a former research fellow of the Indian Institute of Science. His areas of research range from image processing, real-time graph computations to electronic trading algorithms. He is a doctoral candidate in data science at the Swiss School of Business Management, Geneva. Over the years, he has implemented low latency platforms, trading strategies, and market signal models. With more than a decade of hands-on experience, he is a community speaker and also a Python and data science mentor to the newcomers in the field. When not engrossed in reading fiction or playing symphonies on the piano, he spends far too much time watching rockets lift off. Loving son, husband, and a custodian of his personal library.
📄 Page 7
About the Reviewer Shubham Sharma is serving as a Senior Remote Sensing Scientist at GeoSpoc, Bengaluru, India. For the past five years, he has been working on Utilization of Python for Satellite Image Processing Applications and worked on the related projects with the Indian Space Research Organization (ISRO). He has been a presenter at leading Python conferences such as PyCon India and has mentored at conferences such as SciPy. In addition, he conducts workshops related to Python programming for Satellite Image Processing. He enjoys Python programming and takes a great interest in outreach of Python programming amongst the community and exploring the scientific frontiers through Python.
📄 Page 8
Acknowledgements There are a few people I want to thank, without whose ideas and motivations, writing this book would not have been possible. I thank my adorable wife, Srishti; her support, tolerance, and dedication has kept me going. Thanks to my parents for always being the pillars of support, and for instilling in me, the insatiable thirst for learning. A warm thanks to my brother, Saswat and all my cousins for the inspiration, love, and humor that they bring into my life. Thanks to my friends, who have helped me in all my endeavours. This book would have been impossible without all of you. I am eternally grateful to my colleagues and associates in the Python, Fintech, and data science communities, who constantly challenge the status quo, and make Python the powerful tool it is today. Special thanks to Travis Oliphant, the founder of NumFOCUS and Anaconda, whose work and words have helped me develop a social aspect to learning, in the few times we have interacted. I am also grateful to the reviewer of this book, Shubham Sharma, who has provided me with valuable advice to make this book better.
📄 Page 9
Special thanks to the BPB Publications team, especially Nrip Jain, Sourabh Dwivedi, Anugraha Parthipan, Surbhi Saxena and Shali Deeraj their support, advice, and assistance in creating and publishing this book.
📄 Page 10
Preface What makes Python a great language? The idea is that it gets the ‘need to know balance’ right. When I use the term “need to know”, I think of how the military uses the term. The intent is to achieve focus. In a military establishment, every individual needs to make frequent life-or-death choices. The more time you spend making these choices, the more likely you are choosing death. Having to consider the full range of ethical factors into every decision is very inefficient. Since no army wants to lose their own men, they delegate decision-making up through a series of ranks. By the time the individuals are in the field, the most critical decisions are already made, and the soldier has very less room to make their own decisions. They can focus on exactly what they “need to know”, trusting that their superiors have taken into account everything else that they don’t need to know. Software Libraries and abstractions are fundamentally the same. Another developer has taken the broader context into account, and has provided you – the end-developer – with only what you need to know. You get to focus on your work, trusting that the rest has been taken care of effectively. Memory Management is probably the simplest example. Languages that decide how the memory management is going to work (such as through a garbage collector) have taken that decision for you. You don’t need to know. You get to use the time you would have been thinking about deallocation, to focus on the actual tasks.
📄 Page 11
Does to ever fail? Of course, it does. Sometimes you need more context in order to make a good decision. In a military organization, there are conventions for requesting more information, ways to get promoted into positions with more context for complex decisions, and systems for refusing to follow orders or protest. In software, to breaks down when you need some functionality that isn’t explicitly exposed or documented, when you need to debug the library or the runtime code, or just deal with something that is not behaving as it claims it should. When these situations arise, not being able to incrementally increase what you know, becomes a serious blocker. A good balance of “need to know” will actively help you focus on getting your job done, while also providing the escape hatches necessary to handle the times you need to know more. Python gets this balance right. A seemingly complex technical concept in Python is tuple unpacking, but all that the user needs to know here is that they’re getting multiple return values. The fact that there’s really only a single return value and that the unpacking is performed by the assignment and isn’t a necessary or useful knowledge. Python very effectively hides unnecessary details from the users. But when “need to know” starts breaking down, Python has some of the best escape hatches in the entire software industry. To begin with, there are truly no private members. All the code you use in your Python program belongs to you. You can read everything, mutate everything, wrap everything, proxy everything,
📄 Page 12
and nobody can stop you; because it’s your program. Duck Typing makes a heroic appearance here, enabling new ways to overcome the limiting abstractions that would be fundamentally impossible in the other languages. Should you make a habit of doing this? Of course not! You’re using the libraries for a reason – to help you focus on your own code by delegating the “need to know” decisions to someone else. If you are going to regularly question and ignore their decisions, you ruin any advantage you may have received. However, Python allows you to rely on someone else’s code without becoming a hostage to their choices. Today, the Python ecosystem is almost entirely publicly visible code. You don’t need to know how it works, but you have the option to find out. And you can find out by following the same patterns that you’re familiar with, rather than having to learn completely new skills. Reading the Python code, or interactively inspecting the live object graphs, are exactly what you were doing with your own code. Compare Python to the languages that tend towards sharing compiled, minified, packaged, or obfuscated code, and you’ll have a very different experience figuring out how things really (don’t) work. Compare Python to the languages that emphasize privacy, information hiding, encapsulation, and nominal typing, and you’ll have a very different experience, overcoming a broken or limiting abstraction.
📄 Page 13
People often cite Python’s ecosystem as the main reason for its popularity. Others claim the language’s simplicity or expressiveness as the primary reason. I would argue that the Python language has an incredibly well-balanced sense of what the developers need to know; better than any other language I’ve used. Most developers get to write incredibly functional and focused code with just a few syntax constructs. Some developers produce reusable functionality that is accessible through simple syntax. A few developers manage incredible complexity to provide powerful new semantics without leaving the language. By actively helping the library developers write complex code that is not complex to use, Python has been able to build an amazing ecosystem. And that amazing ecosystem is driving the popularity of the language. The book consists of eleven chapters, in which the reader will learn not just how to code in Python, but also how to use the true Python constructs in their code. Chapter 1 is the introductory chapter, which aims to introduce you to what is widely accepted as the ‘Pythonic’ Conventions which bring out the ease and power of Python, including the conventions for comments, docstrings, naming, code layout, and control structures among others. Chapter 2 will help understand some deeper nuances of the Pythonic data structures, where you will learn how to effectively
📄 Page 14
use the data structures for the time and space performant applications. Chapter 3 will outline how to make the Python code adhere to the object oriented paradigm, and what some of the unique features are that Python as a language provides to enhance the code reusability, modularity, and data containerization. Chapter 4 aims to introduce you to the concepts for organizing your project’s code better into the encapsulated modules, making better use of imports to improve performance of your code, and avoid introducing bugs and latency with imports. Chapter 5 will provide insights into how to effectively integrate and implement the Decorators and Context Managers in your code, and help you think through how to create safer Python code in production. Chapter 6 will be touching upon the useful data science tools, and will be diving into some of the best practices that are followed day-to-day by most data scientists to optimize, speed up, automate, and refine their processing tools and applications. Chapter 7 presents some lesser-known perspectives on Iterators, Generators, and Co-routines. Chapter 8 deals with the intricacies of the data and non-data descriptors, and helps understand how to use them for creating your own library or API.
📄 Page 15
Chapter 9 helps understand and use the design patterns to create scalable architectures and discuss the intricacies of the event- based, microservice, and API architectures. Chapter 10 will be taking a deep dive into the test frameworks in Python and the best practices that one should follow in effectively testing the code. Chapter 11 will be discussing how to improve the quality of your work in Python, and help you ship more robust code for the production environment.
📄 Page 16
Downloading the code bundle and coloured images: Please follow the link to download the Code Bundle and the Coloured Images of the book: https://rebrand.ly/2c7e7b Errata We take immense pride in our work at BPB Publications and follow best practices to ensure the accuracy of our content to provide with an indulging reading experience to our subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve upon human errors, if any, that may have occurred during the publishing processes involved. To let us maintain the quality and help us reach out to any readers who might be having difficulties due to any unforeseen errors, please write to us at : errata@bpbonline.com Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’ Family.
📄 Page 17
Did you know that BPB offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.bpbonline.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at business@bpbonline.com for more details. At you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on BPB books and eBooks.
📄 Page 18
BPB is searching for authors like you If you're interested in becoming an author for BPB, please visit www.bpbonline.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. The code bundle for the book is also hosted on GitHub at In case there's an update to the code, it will be updated on the existing GitHub repository. We also have other code bundles from our rich catalog of books and videos available at Check them out! PIRACY If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at business@bpbonline.com with a link to the material. If you are interested in becoming an author
📄 Page 19
If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit REVIEWS Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at BPB can understand what you think about our products, and our authors can see your feedback on their book. Thank you! For more information about BPB, please visit
📄 Page 20
Table of Contents 1. Introduction to Pythonic Code Structure Objectives The importance of clean Python code Adding comments to your code Block comments Inline comments Writing Docstrings Using annotations Pythonic naming Code layout in Python Blank lines Maximum line length and line breaking Indentation – tabs vs. spaces Indentation following line breaks Positioning the closing braces Handling whitespaces in expressions and statements Control structures in Python Complex list comprehensions Lambda usage Should I use a generator or a list comprehension? Can we use an else with loops? Using enhanced range in Python 3 Clean Python code patterns Liberal use of assertions Where to place the commas? The ‘with’ statement for context managers Dunders, underscores, and other features
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now

Recommended for You

Loading recommended books...
Failed to load, please try again later
Back to List