Book Image

NumPy Cookbook - Second Edition

By : Ivan Idris
Book Image

NumPy Cookbook - Second Edition

By: Ivan Idris

Overview of this book

<p>NumPy has the ability to give you speed and high productivity. High performance calculations can be done easily with clean and efficient code, and it allows you to execute complex algebraic and mathematical computations in no time.</p> <p>This book will give you a solid foundation in NumPy arrays and universal functions. Starting with the installation and configuration of IPython, you'll learn about advanced indexing and array concepts along with commonly used yet effective functions. You will then cover practical concepts such as image processing, special arrays, and universal functions. You will also learn about plotting with Matplotlib and the related SciPy project with the help of examples. At the end of the book, you will study how to explore atmospheric pressure and its related techniques. By the time you finish this book, you'll be able to write clean and fast code with NumPy.</p>
Table of Contents (19 chapters)
NumPy Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Testing code with mocks


Mocks are objects created as substitutes for real objects with the purpose of testing part of the behavior of the real objects. If you have seen the movie Body Snatchers, you might already have an understanding of the basic idea. Generally speaking, mocking is useful only when the real objects under test are expensive to create, such as a database connection, or when testing could have undesirable side effects; for instance, we might not want to write to the file system or a database.

In this recipe, we will test a nuclear reactor—not a real one, of course! This nuclear reactor class performs a factorial calculation that could, in theory, cause a chain reaction with a nuclear disaster as consequence. We will mock the factorial computation with a mock, using the mock package.

How to do it...

First, we will install the mock package; after that, we will create a mock and test a piece of code:

  1. To install the mock package, execute the following command:

    $ sudo easy_install...