Python Unlocked (2015)

Preface

Python is a versatile programming language that can be used for a wide range of technical tasks—computation, statistics, data analysis, game development, and more. Though Python is easy to learn, its range of features means there are many aspects of it that even experienced Python developers don't know about. Even if you're confident with the basics, its logic and syntax, by digging deeper you can work much more effectively with Python—and get more from the language.

Python Unlocked walks you through the most effective techniques and best practices for high performance Python programming—showing you how to make the most of the Python language. You'll get to know objects and functions inside and out, and will learn how to use them to your advantage in your programming projects. You will also find out how to work with a range of design patterns, including abstract factory, singleton, and the strategy pattern, all of which will help make programming with Python much more efficient. As the process of writing a program is never complete without testing it, you will learn to test threaded applications and run parallel tests.

If you want the edge when it comes to Python, use this book to unlock the secrets of smarter Python programming.

What this book covers

Chapter 1Objects in Depth, discusses object properties, attributes, creation and how calling objects work.

Chapter 2Namespaces and Classes, discusses namespaces, how imports work, class multiple inheritance, MRO, Abstract classes, and protocols.

Chapter 3Functions and Utilities, teaches function definitions, decorators, and some utilities.

Chapter 4Data Structures and Algorithms, discusses in-built, library, third party data structures and algorithms.

Chapter 5Elegance with Design Patterns, covers many important design patterns.

Chapter 6Test-Driven Development, discusses mock objects, parameterization, creating custom test runners, testing threaded applications, and running testcases in parallel.

Chapter 7Optimization Techniques, covers optimization techniques, profiling, using fast libraries, and compiling C modules.

Chapter 8Scaling Python, covers multithreading, multiprocessing, asynchronization, and scaling horizontally.

What you need for this book

You should have a working installation of Python, preferably greater than 3.4. You can use this with Python 2 as well but the book uses Python 3 and introduces its many new features.

Who this book is for

If you are a Python developer and you think that you do not fully understand the language, then this book is for you. This book will unlock mysteries and reintroduce the hidden features of Python to write efficient programs, making optimal use of the language.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "So, we can change an object's type by changing its __class__ attribute."

A block of code is set as follows:

def __init__(self, name):

        self.name = name

        self._observers = weakref.WeakSet()

    def register_observer(self, observer):

        """attach the observing object for this subject

        """

        self._observers.add(observer)

        print("observer {0} now listening on {1}".format(

            observer.name, self.name))

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

        self.assertFalse(assign_if_free(mworker, {}))

    def test_worker_free(self,):

        mworker = create_autospec(IWorker)

        mworker.configure_mock(**{'is_busy.return_value':False})

        self.assertTrue(assign_if_free(mworker, {}))

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Let's take an example of an object iC instance of the C class with the str and lst attributes."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.