BUILD BETTER WRITE LESS. CODE MORE. PYTHON 2 AND 3 COVERS . - Free Download PDF

30d ago
43 Views
9 Downloads
4.72 MB
242 Pages
Transcription

WRITE LESS.CODE MORE.BUILD BETTERPROGRAMS.You’ll first learn how to start a project and tackletopics like versioning, coding style, and automatedchecks. Then you’ll look at how to define functionsefficiently, pick the right data structures and libraries,build future-proof programs, package your softwarefor distribution, and optimize your programs down tothe bytecode. You’ll also learn how to: Create and use effective decorators and methods,including abstract, static, and class methodsPYTHON 2 AND 3 Employ Python for functional programming usinggenerators, pure functions, and functional functions Extend flake8 to work with the abstract syntax tree(AST) to introduce more sophisticated automaticchecks Apply dynamic performance analysis to identifybottlenecks in your code Work with relational databases and effectivelymanage and stream data with PostgreSQLTake your Python skills from good to great. Learn fromthe experts and get seriously good at Python withSerious Python!ABOUT THE AUTHORJulien Danjou is a principal software engineer at RedHat and a contributor to OpenStack, the largest existingopen source project written in Python. He has been afree software and open source hacker for the past15 years.w w w.nostarch.com 34.95 ( 45.95 CDN)SHELVE IN:PROGRAMMING LANGUAGES/PYTHONDANJOUT H E F I N E ST I N G E E K E N T E RTA I N M E N T SERIOUS PY THONSharpen your Python skills as you dive deep into thePython programming language with Serious Python.Written for developers and experienced programmers,Serious Python brings together more than 15 years ofPython experience to teach you how to avoid commonmistakes, write code more efficiently, and build betterprograms in less time. You’ll cover a range of advancedtopics like multithreading and memoization, get advicefrom experts on things like designing APIs and dealingwith databases, and learn Python internals to give you adeeper understanding of the language itself.COVERSSERIOUSPY THONB L A C K - B E L TA D V I C EO ND E P L O Y M E N T,S C A L A B I L I T Y, T E S T I N G , A N DJULIEN DANJOUM O R E

Serious Python

SeriousPythonBlack-Belt Advice onDeployment, Scalability,Testing, and Moreb y Ju l i e n D a n j o uSan Francisco

Serious Python. Copyright 2019 by Julien Danjou.All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,electronic or mechanical, including photocopying, recording, or by any information storage or retrievalsystem, without the prior written permission of the copyright owner and the publisher.ISBN-10: 1-59327-878-0ISBN-13: 978-1-59327-878-6Publisher: William PollockProduction Editor: Laurel ChunCover Illustration: Josh EllingsonInterior Design: Octopod StudiosDevelopmental Editors: Liz Chadwick with Ellie BruTechnical Reviewer: Mike DriscollCopyeditor: Paula L. FlemingCompositor: Laurel ChunProofreader: James FraleighFor information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:No Starch Press, Inc.245 8th Street, San Francisco, CA 94103phone: 1.415.863.9900; [email protected] of Congress Cataloging-in-Publication DataNames: Danjou, Julien, author.Title: Serious Python : black-belt advice on deployment, scalability,testing, and more / Julien Danjou.Description: San Francisco, CA : No Starch Press, Inc., [2019].Identifiers: LCCN 2018042631 (print) LCCN 2018050473 (ebook) ISBN9781593278793 (epub) ISBN 1593278799 (epub) ISBN 9781593278786 (print) ISBN 1593278780 (print) ISBN 9781593278793 (ebook) ISBN 1593278799(ebook)Subjects: LCSH: Python (Computer program language)Classification: LCC QA76.73.P98 (ebook) LCC QA76.73.P98 D36 2019 (print) DDC 005.13/3--dc23LC record available at https://lccn.loc.gov/2018042631No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Otherproduct and company names mentioned herein may be the trademarks of their respective owners. Ratherthan use a trademark symbol with every occurrence of a trademarked name, we are using the names onlyin an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of thetrademark.The information in this book is distributed on an “As Is” basis, without warranty. While every precautionhas been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have anyliability to any person or entity with respect to any loss or damage caused or alleged to be caused directly orindirectly by the information contained in it.

About the AuthorJulien Danjou has been a free software hacker for close to twenty yearsand has been developing software with Python for twelve years. He currently works as Project Team Leader for the distributed cloud platformOpenStack, which has the largest existing open-source Python codebaseat 2.5 million lines of Python. Before building clouds, Julien created theawesome window manager and contributed to various software such asDebian and GNU Emacs.About the Technical ReviewerMike Driscoll has been programming with Python for more than adecade. He has been writing about Python on his blog, The Mouse vs.The Python , for many years. Mike is the author of several Python booksincluding Python 101, Python Interviews, and ReportLab: PDF Processingwith Python. You can find Mike on Twitter or GitHub via his handle:@driscollis.

Brief ContentsAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Chapter 1: Starting Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Chapter 2: Modules, Libraries, and Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Chapter 3: Documentation and Good API Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Chapter 4: Handling Timestamps and Time Zones . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Chapter 5: Distributing Your Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57Chapter 6: Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Chapter 7: Methods and Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99Chapter 8: Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Chapter 9: The Abstract Syntax Tree, Hy, and Lisp-like Attributes . . . . . . . . . . . . . . . . 135Chapter 10: Performances and Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151Chapter 11: Scaling and Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177Chapter 12: Managing Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187Chapter 13: Write Less, Code More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

Conte nt s in De ta ilAcknowledgmentsxvIntroduction1Who Should Read This Book and Why . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Starting Your ProjectVersions of Python . . . . . . . . . . . . . . . .Laying Out Your Project . . . . . . . . . . .What to Do . . . . . . . . . . . . .What Not to Do . . . . . . . . .Version Numbering . . . . . . . . . . . . . . .Coding Style and Automated Checks . . .Tools to Catch Style Errors . . .Tools to Catch Coding Errors .Joshua Harlow on Python . . . . . . . . . . .5.2Modules, Libraries, and Frameworks15The Import System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The sys Module . . . . . . . . . . . . . . . . . . . . . . . . .Import Paths . . . . . . . . . . . . . . . . . . . . . . . . . . .Custom Importers . . . . . . . . . . . . . . . . . . . . . . . .Meta Path Finders . . . . . . . . . . . . . . . . . . . . . . .Useful Standard Libraries . . . . . . . . . . . . . . . . . . . . . . . . . .External Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The External Libraries Safety Checklist . . . . . . . . .Protecting Your Code with an API Wrapper . . . . .Package Installation: Getting More from pip . . . . . . . . . . . . .Using and Choosing Frameworks . . . . . . . . . . . . . . . . . . . .Doug Hellmann, Python Core Developer, on Python Libraries .3Documentation and Good API PracticeDocumenting with Sphinx . . . . . . . . . . . . . . . . .Getting Started with Sphinx and reST .Sphinx Modules . . . . . . . . . . . . . . . .Writing a Sphinx Extension . . . . . . . .Managing Changes to Your APIs . . . .Numbering API Versions . . . . . . . . . . 5. 7. 7. 8. 810111213.16171818192022232324262733.343536394041

Documenting Your API Changes . . . . . . . . . . . . . . . . . .Marking Deprecated Functions with the warnings Module .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Christophe de Vienne on Developing APIs . . . . . . . . . . . . . . . . . . .4Handling Timestamps and Time ZonesThe Problem of Missing Time Zones . . . . . . . . . .Building Default datetime Objects . . . . . . . . . . .Time Zone–Aware Timestamps with dateutil . . . .Serializing Time Zone–Aware datetime Objects .Solving Ambiguous Times . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . .49.5Distributing Your SoftwareA Bit of setup.py History . . . . . . . . . . . .Packaging with setup.cfg . . . . . . . . . . . .The Wheel Format Distribution Standard .Sharing Your Work with the World . . . . .Entry Points . . . . . . . . . . . . . . . . . . . . .Visualizing Entry Points . . . . . .Using Console Scripts . . . . . . .Using Plugins and Drivers . . . .Summary . . . . . . . . . . . . . . . . . . . . . . .Nick Coghlan on Packaging . . . . . . . . .50505254555657.6Unit TestingThe Basics of Testing . . . . . . . . . . . . . . . . . . . . . . . . .Some Simple Tests . . . . . . . . . . . . . . . . . . .Skipping Tests . . . . . . . . . . . . . . . . . . . . . .Running Particular Tests . . . . . . . . . . . . . . .Running Tests in Parallel . . . . . . . . . . . . . . .Creating Objects Used in Tests with Fixtures .Running Test Scenarios . . . . . . . . . . . . . . . .Controlled Tests Using Mocking . . . . . . . . . .Revealing Untested Code with coverage . . . .Virtual Environments . . . . . . . . . . . . . . . . . . . . . . . . .Setting Up a Virtual Environment . . . . . . . . .Using virtualenv with tox . . . . . . . . . . . . . . .Re-creating an Environment . . . . . . . . . . . . .Using Different Python Versions . . . . . . . . . .Integrating Other Tests . . . . . . . . . . . . . . . .Testing Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Robert Collins on Testing . . . . . . . . . . . . . . . . . . . . . .xii   Contents in Detail414345455860616467686971737475.

Mike Driscoll has been programming with Python for more than a decade. He has been writing about Python on his blog, The Mouse vs. The Python, for many years. Mike is the author of several Python books including Python 101, Python Interviews, and ReportLab: PDF Processing with Python. You can find Mike on Twitter or GitHub via his handle ...