Book Image

Python Unlocked

By : Arun Tigeraniya
Book Image

Python Unlocked

By: Arun Tigeraniya

Overview of this book

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, it’s 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, strategy pattern, all of which will help make programming with Python much more efficient. Finally, 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.
Table of Contents (15 chapters)
Python Unlocked
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 1, Objects in Depth, discusses object properties, attributes, creation and how calling objects work.

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

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

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

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

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

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

Chapter 8, Scaling 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.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.