ebook img

Python Programming Blueprints: Build nine projects by leveraging powerful frameworks such as Flask, Nameko, and Django PDF

456 Pages·2017·5.93 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Python Programming Blueprints: Build nine projects by leveraging powerful frameworks such as Flask, Nameko, and Django

Python Programming Blueprints Build nine projects by leveraging powerful frameworks such as Flask, Nameko, and Django Daniel Furtado Marcus Pennington BIRMINGHAM - MUMBAI Python Programming Blueprints Copyright © 2018 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 authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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. Commissioning Editor: Merint Mathew Acquisition Editor: Sandeep Mishra Content Development Editor: Lawrence Veigas Technical Editor: Mehul Singh Copy Editor: Safis Editing Project Coordinator: Prajakta Naik Proofreader: Safis Editing Indexer: Rekha Nair Graphics: Jisha Chirayil Production Coordinator: Arvindkumar Gupta First published: February 2018 Production reference: 1260218 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78646-816-1 www.packtpub.com I dedicate this book to my family—my sister, Camila, my mother, Silvia, and my father, Simão, who have done everything in their power to help me achieve all my dreams. There are no words to express how grateful and lucky I feel for being their child. To my lovely Maria, who every day gives me strength, encouragement, inspiration, and love. I wouldn't have made it without you. I love you! Also, to my loyal French bulldog friends, Edit and Ella. –Daniel Furtado My parents, Dawn and Robert, who over my lifetime have always pushed me to do my best. They instilled in me the ability to accomplish anything I put my mind to. Fabrizio Romano for convincing me to contribute to this book. He is the greatest mentor an aspiring developer could ask for. And finally, my partner, Emily, for always being there for me. –Marcus Pennington Contributors About the authors Daniel Furtado is a software developer with over 20 years of experience in different technologies such as Python, C, .NET, C#, and JavaScript. He started programming at the age of 13 on his ZX Spectrum. He joined the Bioinformatics Laboratory of the Human Cancer Genome Project in Brazil, where he developed web applications and tools in Perl and Python to help researchers analyze data. He has never stopped developing in Python ever since. Daniel has worked on various open source projects; the latest one is a PyTerrier web micro- framework. Marcus Pennington started his journey into computer science at Highams Park Sixth Form College where he took a Cisco CCNA course. He then went to the University of Hertfordshire, where he graduated with a degree in Computer Science with Artificial Intelligence. Since then, he has had the privilege of working with some of the best developers and learning the benefits and pitfalls of many of the software practices we use today. He has a passion for writing clean, cohesive, and beautiful code. I would like to acknowledge Tom Viner for giving my chapters a thorough review; his insights not only improved the quality of my chapters but also taught me a great deal. Julio Trigo, an expert at using PostgreSQL with Python; his knowledge supplemented my own when creating the database dependency. Edward Melly, a JavaScript and React craftsman, for reviewing the frontend code in my chapters. About the reviewers Tom Viner is a senior software developer living in London. He has over 10 years of experience in building web applications and has been using Python and Django for 8 years. He has special interests in open source software, web security, and Test-driven development. Tom has given two conference talks, Testing with two failure seeking missiles: fuzzing and property based testing and Exploring unit-testing, unittest v pytest: FIGHT! Tom works for Sohonet in central London and sometimes goes backpacking around the world. I would like to thank Marcus Pennington for inviting me to review this book. Radovan Kavický is the principal data scientist and president at GapData Institute based in Bratislava, Slovakia, harnessing the power of data and wisdom of economics for public good. He has an academic background in macroeconomics and is a consultant and data scientist by profession. Radovan is also an instructor at DataCamp and a founder of PyData Bratislava, R <- Slovakia & SK/CZ Tableau User Group (skczTUG). Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content PacktPub.com 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 [email protected] 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. Table of Contents Preface 1 Chapter 1: Implementing the Weather Application 7 Setting up the environment 8 Core functionality 10 Loading parsers dynamically 10 Creating the application's model 12 Fetching data from the weather website 19 Getting the user's input with ArgumentParser 20 Creating the parser 27 Getting today's weather forecast 29 Adding helper methods 30 Implementing today's weather forecast 32 Getting five- and ten-day weather forecasts 37 Getting the weekend weather forecast 43 Summary 47 Chapter 2: Creating a Remote-Control Application with Spotify 48 Setting up the environment 49 Creating a Spotify app 52 The application's configuration 56 Creating a configuration file 57 Implementing a configuration file reader 58 Authenticating with Spotify's web API 63 Implementing the client credentials flow 64 Implementing the authorization code flow 68 Authorizing our application with authorization code flow 71 Querying Spotify's web API 77 Creating the player 85 Adding menus for albums and track selection 88 Implementing the menu panel 89 Creating the DataManager class 95 Table of Contents Time to listen to music! 99 Summary 111 Chapter 3: Casting Votes on Twitter 112 Setting up the environment 113 Creating a Twitter application 116 Adding the configuration file 119 Performing authentication 123 Creating the Flask application 124 Creating the application routes 126 Building the Twitter voting application 132 Enhancing our code 137 Summary 152 Chapter 4: Exchange Rates and the Currency Conversion Tool 153 Setting up the environment 154 Creating the API wrapper 161 Adding the database helper class 164 Creating the command line parser 167 Creating the currency enumeration 172 Creating the command line parser 173 Basic validation 177 Adding the application's entry point 179 Testing our application 182 Summary 184 Chapter 5: Building a Web Messenger with Microservices 185 TempMessenger Goals 186 Requirements 186 What is Nameko? 187 RPCs 187 How Nameko uses AMQP 188 RabbitMQ 188 Starting a RabbitMQ container 189 Installing Python requirements 189 Creating your first Nameko microservice 191 Making a call to our service 193 [ ii ] Table of Contents Unit-testing a Nameko microservice 193 Exposing HTTP entrypoints 194 Integration testing Nameko microservices 196 Storing messages 197 An introduction to Redis 197 Starting a Redis container 197 Installing the Python Redis client 198 Using Redis 198 Nameko Dependency Providers 199 Adding a Redis Dependency Provider 200 Designing the Client 200 Creating the Dependency Provider 201 Creating our Message Service 202 Putting it all together 203 Saving messages 204 Adding a save message method to our Redis client 204 Adding a save message RPC 205 Retrieving all messages 206 Adding a get all messages method to our Redis client 206 Adding a get all messages RPC 207 Putting it all together 208 Displaying messages in the web browser 208 Adding a Jinja2 Dependency Provider 208 Creating the template renderer 209 Creating our homepage template 210 Creating the Dependency Provider 211 Making a HTML response 211 Putting it all together 212 Sending messages via POST requests 214 Adding a send messages POST request 215 Adding an AJAX POST request in jQuery 216 Expiring messages in Redis 219 Sorting messages 220 Browser polling for messages 222 Polling with JavaScript 223 Summary 225 [ iii ]

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.