Book Image

Secret Recipes of the Python Ninja

Book Image

Secret Recipes of the Python Ninja

Overview of this book

This book covers the unexplored secrets of Python, delve into its depths, and uncover its mysteries. You’ll unearth secrets related to the implementation of the standard library, by looking at how modules actually work. You’ll understand the implementation of collections, decimals, and fraction modules. If you haven’t used decorators, coroutines, and generator functions much before, as you make your way through the recipes, you’ll learn what you’ve been missing out on. We’ll cover internal special methods in detail, so you understand what they are and how they can be used to improve the engineering decisions you make. Next, you’ll explore the CPython interpreter, which is a treasure trove of secret hacks that not many programmers are aware of. We’ll take you through the depths of the PyPy project, where you’ll come across several exciting ways that you can improve speed and concurrency. Finally, we’ll take time to explore the PEPs of the latest versions to discover some interesting hacks.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Packt Upsell
Foreword
Contributors
Preface
Index

Improving functionality with comath


PyPi provides the comath module, which adds additional math functionality to Python.

Getting ready

The module can be installed using or downloading and installing the wheel package or .tar.gz file from https://pypi.python.org/pypi/comath/0.0.3:

pip install comath

How to do it...

Note that not all functions in comath are displayed here, as some require additional packages such as NumPy, which are beyond the scope of this book, or are modified versions of existing math functions:

  1. array.percentile(sorted_list, percent [, key=lambda x: x]) function returns the desired percentile (as defined by percent) from a sorted list of numbers:
    • For the list of numbers, line 4 returns the 10th percentile.
    • The line 5 returns the 30th percentile.
    • The line 6 returns the 50th percentile.
    • The line 7 returns the 75th percentile.
    • The line 8 returns the 99th percentile.
  1. func.get_smooth_step_function(min_val, max_val, switch_point, smooth_factor) function returns a function that moves smoothly...