O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Secret Recipes of the Python Ninja

Book Description

Test your Python programming skills by solving real-world problems

About This Book
  • Access built-in documentation tools and improve your code.
  • Discover how to make the best use of decorator and generator functions
  • Enhance speed and improve concurrency by conjuring tricks from the PyPy project
Who This Book Is For

Whether you've been working with Python for a few years or you're a seasoned programmer, you'll have a lot of new tricks to walk away with.

What You Will Learn
  • Know the differences between .py and .pyc files
  • Explore the different ways to install and upgrade Python packages
  • Understand the working of the PyPI module that enhances built-in decorators
  • See how coroutines are different from generators and how they can simulate multithreading
  • Grasp how the decimal module improves floating point numbers and their operations
  • Standardize sub interpreters to improve concurrency
  • Discover Python's built-in docstring analyzer
In Detail

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.

Style and approach

Recipe based approach where each problem is solved with the help of step by step instructions.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Secret Recipes of the Python Ninja
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Working with Python Modules
    1. Introduction
    2. Using and importing modules and namespaces
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Implementing virtual Python environments
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Python package installation options
      1. How to do it...
      2. How it works...
    5. Utilizing requirement files and resolving conflicts
      1. How to do it...
      2. How it works...
      3. There's more...
    6. Using local patches and constraint files
      1. How to do it...
      2. How it works...
      3. There's more...
    7. Working with packages
      1. How to do it...
      2. How it works...
      3. There's more...
    8. Creating wheels and bundles
      1. How to do it...
      2. How it works...
      3. There's more...
    9. Comparing source code to bytecode
      1. How to do it...
      2. How it works...
      3. There's more...
    10. How to create and reference module packages
      1. How to do it...
      2. How it works...
      3. There's more...
    11. Operating system-specific binaries
      1. How to do it...
      2. There's more...
    12. How to upload programs to PyPI
      1. Getting ready
      2. How to do it...
      3. How it works...
    13. Project packaging
      1. How to do it...
    14. Uploading to PyPI
      1. Getting ready
      2. How to do it...
      3. How it works...
  7. Utilizing the Python Interpreter
    1. Introduction
    2. Launching Python environments
      1. How to do it...
      2. How it works...
    3. Utilizing Python command options
      1. How to do it...
      2. How it works...
        1. Interface options
        2. Generic options
        3. Miscellaneous options
      3. See also...
    4. Working with environment variables
      1. How to do it...
      2. How it works...
    5. Making scripts executable
      1. How to do it...
      2. There's more...
    6. Modifying interactive interpreter startup
      1. How to do it...
      2. See also
    7. Alternative Python implementations
      1. How to do it...
      2. There's more...
    8. Installing Python on Windows
      1. Getting ready
      2. How to do it...
    9. Using the Windows Python launcher
      1. How to do it...
    10. Embedding Python with other applications
      1. How to do it...
      2. How it works...
    11. Using alternative Python shells – IPython
      1. Getting ready
      2. How to do it...
      3. There's more...
    12. Using alternative Python shells – bpython
      1. Getting ready
      2. How to do it...
      3. There's more...
    13. Using alternative Python shells – DreamPie
      1. Getting ready
      2. How to do it...
      3. There's more...
  8. Working with Decorators
    1. Introduction
    2. Reviewing functions
      1. How to do it...
      2. How it works...
    3. Introducing decorators
      1. How to do it...
      2. How it works...
    4. Using function decorators
      1. How to do it...
      2. How it works...
    5. Using class decorators
      1. How to do it...
    6. Examples of decorators
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    7. Using the decorators module
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
  9. Using Python Collections
    1. Introduction
    2. Reviewing containers
      1. How to do it...
      2. There's more...
        1. Lists and tuples
        2. Dictionaries
        3. Sets
    3. Implementing namedtuple
      1. How to do it...
      2. There's more...
    4. Implementing deque
      1. How to do it...
    5. Implementing ChainMap
      1. How to do it...
    6. Implementing Counters
      1. How to do it...
      2. There's more...
    7. Implementing OrderedDict
      1. How to do it...
    8. Implementing defaultdict
      1. How to do it...
    9. Implementing UserDict
      1. How to do it...
    10. Implementing UserList
      1. How to do it...
      2. There's more...
    11. Implementing UserString
      1. How to do it...
    12. Improving Python collections
      1. How to do it...
        1. Default dictionaries
        2. Named tuples
        3. Ordered dictionaries
    13. Looking at the collections – extended module
      1. Getting ready
      2. How to do it...
        1. setlist
        2. bags
        3. RangeMap
        4. Bijection
  10. Generators, Coroutines, and Parallel Processing
    1. How iteration works in Python
      1. How to do it...
    2. Using the itertools module
      1. How to do it...
        1. Infinite iterators
        2. Combinatoric iterators
        3. Terminating iterators
    3. Using generator functions
      1. How to do it...
      2. How it works...
      3. There's more...
    4. Simulating multithreading with coroutines
      1. How to do it...
      2. There's more...
    5. When to use parallel processing
      1. How to do it...
      2. There's more...
    6. Forking processes
      1. How to do it...
      2. How it works...
      3. There's more...
    7. How to implement multithreading
      1. How to do it...
      2. There's more...
        1. Advantages
        2. Disadvantages
    8. How to implement multiprocessing
      1. How to do it...
      2. There's more...
  11. Working with Python's Math Module
    1. Using the math module's functions and constants
      1. How to do it...
    2. Working with complex numbers
      1. How to do it...
    3. Improving decimal numbers
      1. How to do it...
    4. Increasing accuracy with fractions
      1. How to do it...
    5. Working with random numbers
      1. How to do it...
    6. Using the secrets module
      1. How to do it...
    7. Implementing basic statistics
      1. How to do it...
    8. Improving functionality with comath
      1. Getting ready
      2. How to do it...
  12. Improving Python Performance with PyPy
    1. Introduction
    2. What is PyPy?
      1. Getting ready
      2. How to do it...
      3. There's more...
    3. What is RPython?
      1. How to do it...
        1. Flow restrictions
        2. Object restrictions
        3. Integer types
      2. There's more...
    4. Some real-world examples
      1. How to do it...
      2. There's more...
  13. Python Enhancement Proposals
    1. Introduction
    2. What are PEPs?
      1. How to do it...
      2. There's more...
    3. PEP 556 – Threaded garbage collection
      1. Getting ready
      2. How to do it...
      3. There's more...
    4. PEP 554 – Multiple subinterpreters
      1. How to do it...
      2. How it works...
        1. Channels
      3. There's more...
    5. PEP 551 – Security transparency
      1. Getting ready
        1. General security
        2. Python and security
      2. How to do it...
    6. PEP 543 – Unified TLS API
      1. How to do it...
      2. There's more...
  14. Documenting with LyX
    1. Introduction
    2. Python documentation tools and techniques
      1. How to do it...
    3. Inline comments and the dir command
    4. Using docstrings
      1. How to do it...
      2. There's more...
    5. Using PyDoc help
      1. How to do it...
    6. HTML reports
      1. How to do it...
    7. Using reStructuredText files
      1. Getting ready
      2. How to do it...
    8. Using the Sphinx documentation program
      1. Getting ready
      2. How to do it...
    9. Using LaTeX and LyX document preparation programs
      1. Getting ready
      2. How to do it...
      3. There's more...
  15. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think