Table Of ContentMastering Python
Master the art of writing beautiful and powerful Python
by using all of the features that Python 3.5 offers
Rick van Hattem
BIRMINGHAM - MUMBAI
Mastering Python
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2016
Production reference: 1270416
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-972-9
www.packtpub.com
Credits
Author Project Coordinator
Rick van Hattem Suzanne Coutinho
Reviewers Proofreader
Randall Degges Safis Editing
Dave de Fijter
I. de Hoogt Indexer
Mariammal Chettiyar
Commissioning Editor
Sarah Crofton Production Coordinator
Nilesh Mohite
Acquisition Editor
Reshma Raman Cover Work
Nilesh Mohite
Content Development Editor
Arun Nadar
Technical Editors
Ryan Kochery
Tanmayee Patil
Copy Editor
Vikrant Phadke
About the Author
Rick van Hattem is an experienced programmer, entrepreneur, and
software/database architect with over 20 years of programming experience,
including 15 with Python. Additionally, he has a lot of experience with
high-performance architectures featuring large amounts of concurrent users
and/or data.
Rick has founded several start-ups and has done consulting for many companies,
including a few Y Combinator start-ups and several large companies. One of the
startups he founded, Fashiolista.com, is one of the largest social networks for fashion
in the world, featuring millions of users and the performance challenges
to accompany those.
Rick was one of the reviewers on the book PostgreSQL Server Programming,
Packt Publishing.
Thanks to my family, in particular Marloes, who supported me every
step of the way; and my mother and sister, who have always been
there for me.
About the Reviewers
Randall Degges is a happy programmer, speaker, author, and amateur
bodybuilder living in California.
Growing up in Los Angeles, he was intensely interested in building command-
line programs and writing quality software. His love of programming eventually
propelled him into a successful career in software development.
Randall has been a life-long open source developer and has contributed to
hundreds of popular projects in Python, Node.js, and Go. He's also the author of
several popular libraries, which you can find on his public GitHub account
at https://github.com/rdegges.
At 23, he cofounded an extremely popular API service in the telephony industry:
OpenCNAM (https://www.opencnam.com). At 25, he joined Stormpath
(https://stormpath.com) as the head of developer evangelism, whereby he
writes open source security libraries full time and travels the world giving technical
talks about building secure software.
In his free time, Randall writes and edits technical books, runs a security podcast
called Stormcast (https://www.stormca.st), posts blogs on his personal website
(https://www.rdegges.com), and tries to spend time with his high-school
sweetheart, Samantha.
Dave de Fijter is a Python developer from the Netherlands. He always knew he
would end up "doing something with computers." At a young age, he went to the
library to read books about them even though he had no computer at that time.
This obsession never really ended. In 2001, aged 14, he started his first part-time job,
creating dynamic websites in PHP for a local web development company, and there
he found his calling.
In 2007, he finished his bachelor's degree in ICT while already working full time as a
PHP developer for over a year. In 2008, he switched from PHP to Python and Django
for web development and loved this new technology stack so much that he never
looked back.
After working as a Python developer for various start-ups and established
companies, Dave used this experience to start his own business called Indentity
(https://indentity.nl) in 2010, focusing on Python/Django development and
advice. Up until now, he runs this company and mainly spends his time helping out
start-ups with designing and building technologically advanced web applications
from the ground up as an interim CTO/technical cofounder.
I. de Hoogt, with some basic experience wrought from university assignments in the
field of modeling of multi-phase flows, got himself started in software development.
His main experience in programming in Python stems from an internship at a
company dealing in 3D printing software, where a package resulting in optimized
object orientation and guaranteed mathematical mesh validity was created.
Other projects that he's been involved with have dealt with control systems such as
self-parking cars, multi-legged robots, and quadcopters, but his current job is in the
field of data analysis.
www.PacktPub.com
eBooks, discount offers, and more
Did you know that Packt offers eBook versions of every book published, with PDF
and ePub files available? You can upgrade to the eBook version at www.PacktPub.
com and as a print book customer, you are entitled to a discount on the eBook copy.
Get in touch with us at customercare@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign
up for a range of free newsletters and receive exclusive discounts and offers on Packt
books and eBooks.
TM
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital
book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser
Table of Contents
Preface xi
Chapter 1: Getting Started – One Environment per Project 1
Creating a virtual Python environment using venv 2
Creating your first venv 3
venv arguments 4
Differences between virtualenv and venv 6
Bootstrapping pip using ensurepip 7
ensurepip usage 7
Manual pip install 7
Installing C/C++ packages 8
Debian and Ubuntu 9
Red Hat, CentOS, and Fedora 9
OS X 9
Windows 10
Summary 11
Chapter 2: Pythonic Syntax, Common Pitfalls, and Style Guide 13
Code style – or what is Pythonic code? 14
Formatting strings – printf-style or str.format? 15
PEP20, the Zen of Python 15
Beautiful is better than ugly 16
Explicit is better than implicit 17
Simple is better than complex 18
Flat is better than nested 20
Sparse is better than dense 20
Readability counts 21
Practicality beats purity 21
Errors should never pass silently 22
In the face of ambiguity, refuse the temptation to guess 24
One obvious way to do it 24
Now is better than never 25
[ i ]