Python True Book by Jon Rulta.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Python True Book Python ideas for Programming Jon Rulta Python True Book Copyright © 2021 However Publishing All rights reserved. Bombaone - Mudozvone. 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 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. Producer: Priyanka Dhadke Content Development Editors: Alex Patterson, Bhavesh Amin Technical Editor: Karan Sonawane Project Editor: Janice Gonsalves Copy Editor: Safis Editing Proofreader: Safis Editing Indexer: Priyanka Dhadke Presentation Designer: Sandip Tadge Production reference: 1270520 Published by However Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-80020-708-0 Subscribe to our online digital library for full access to over 7,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 • Learn better with Skill Plans built especially for you • Get a free eBook or video every month • Fully searchable for easy access to vital information • Copy and paste, print, and bookmark content Contributors About the author Jaime Buelta has been a professional programmer for 20 years and a full-time Python developer for over 10. During that time, he has been exposed to many different technologies. He has developed software for different industries, including aerospace, networking, industrial systems, video game online services, and finance services. In these industries, he has worked in various areas, such as marketing, management, sales, and game design, helping companies achieve their goals. He is a strong proponent of automating everything and making computers do most of the heavy lifting so users can focus on the important stuff. He is currently living in Dublin, Ireland, and is a regular speaker at PyCon Ireland. This book could not have happened without the support and encouragement of my amazing wife, Dana. For this second edition I've worked closely with more people in the Packt team, and their help has been invaluable. Also, great thanks to the reviewers – their comments have improved this book. Finally, I'd like to thank the whole Python community. I can't overstate what a joy it is to be a developer in the Python world. About the reviewer Michal Jaworski has more than 10 years of professional experience in writing software using various programming languages. Michał has spent most of his career writing high-performance and highly distributed backend services for web applications. His beloved language of choice was always Python. He has had various roles at different companies, from an ordinary full-stack developer, through software architect, to VP of engineering in a fast-paced start-up. He is currently a full-time senior software engineer at Showpad. Whenever he finds any free time, he provides Python consulting for local start-ups. Michał is also an active contributor to many open source Python projects and authored the last two editions of Python Expert Programming. Table of Contents Preface v Chapter 1: Let's Begin Our Automation Journey 1 Activating a virtual environment 2 Installing third-party packages 8 Creating strings with formatted values 11 Manipulating strings 14 Extracting data from structured strings 19 Using a third-party tool—parse 23 Introducing regular expressions 28 Going deeper into regular expressions 34 Adding command-line arguments 38 Chapter 2: Automating Tasks Made Easy 45 Preparing a task 46 Setting up a cron job 53 Capturing errors and problems 59 Sending email notifications 65 Chapter 3: Building Your First Web Scraping Application 71 Downloading web pages 72 Parsing HTML 75 Crawling the web 79 Subscribing to feeds 85 Accessing web APIs 89 Interacting with forms 93 Using Selenium for advanced interaction 99 Accessing password-protected pages 103 [ i ] Table of Contents Speeding up web scraping 106 Chapter 4: Searching and Reading Local Files 113 Crawling and searching directories 114 Reading text files 117 Dealing with encodings 120 Reading CSV files 124 Reading log files 127 Reading file metadata 130 Reading images 132 Reading PDF files 139 Reading Word documents 143 Scanning documents for a keyword 147 Chapter 5: Generating Fantastic Reports 151 Creating a simple report in plain text 152 Using templates for reports 155 Formatting text in Markdown 159 Writing a basic Word document 162 Styling a Word document 165 Generating structure in Word documents 169 Adding pictures to Word documents 175 Writing a simple PDF document 179 Structuring a PDF 182 Aggregating PDF reports 188 Watermarking and encrypting a PDF 190 Chapter 6: Fun with Spreadsheets 197 Writing a CSV spreadsheet 198 Updating CSV files 200 Reading an Excel spreadsheet 203 Updating an Excel spreadsheet 206 Creating new sheets in an Excel spreadsheet 209 Creating charts in Excel 213 Working with cell formats in Excel 216 Creating a macro in LibreOffice 220 Chapter 7: Cleaning and Processing Data 229 Preparing a CSV spreadsheet 230 Appending currency based on location 235 Standardizing the date format 240 Aggregating results 245 Processing data in parallel 252 Processing data with Pandas 259 [ ii ] Table of Contents Chapter 8: Developing Stunning Graphs 267 Plotting a simple sales graph 268 Drawing stacked bars 272 Plotting pie charts 277 Displaying multiple lines 280 Drawing a scatter plot 286 Visualizing maps 290 Adding legends and annotations 297 Combining graphs 302 Saving charts 307 Chapter 9: Dealing with Communication Channels 311 Working with email templates 312 Sending an individual email 316 Reading an email 321 Adding subscribers to an email newsletter 325 Sending notifications via email 329 Producing SMS messages 334 Receiving SMS 339 Creating a Telegram bot 347 Chapter 10: Why Not Automate Your Marketing Campaign? 355 Introduction 355 Detecting opportunities 356 Creating personalized coupon codes 360 Sending a notification to a customer on their preferred channel 366 Preparing sales information 373 Generating a sales report 379 Chapter 11: Machine Learning for Automation 387 Introduction 387 Analyzing images with Google Cloud Vision AI 389 Extracting text from images with Google Cloud Vision AI 403 Analyzing text with Google Cloud Natural Language 411 Creating your own custom machine learning model to classify text 419 Chapter 12: Automatic Testing Routines 433 Introduction 433 Writing and executing test cases 435 Testing external code 439 Testing using dependency mocking 442 Testing using HTTP call mocking 450 Preparing testing scenarios 458 [ iii ] Table of Contents Running tests selectively 466 Chapter 13: Debugging Techniques 475 Introduction 475 Learning Python interpreter basics 477 Debugging through logging 481 Debugging with breakpoints 485 Improving your debugging skills 490 Other Books You May Enjoy 499 Index 503 [ iv ] Preface We are all probably spending time doing small manual tasks that don't add much value. It may be scanning through information sources in search of the small bits of relevant information, working with spreadsheets to generate the same graph over and over, or searching files one by one until we find the data we're looking for. Some—probably most—of those tasks are, in fact, automatable. There's an investment upfront, but for the tasks that get repeated over and over, we can use computers to do these kinds of menial tasks and focus our own efforts instead on what humans are good for—high-level analysis and decision making based on the result. This book will explain how to use the Python language to automate common business tasks that can be greatly sped up if a computer is doing them. Given the expressiveness and ease of use of Python, it's surprisingly simple to start making small programs to perform these actions and combine them into more integrated systems. Throughout the book, we will show small, easy-to-follow recipes that can be adapted to your specific needs, and we will combine them to perform more complex actions. We will perform common actions, such as detecting opportunities by scraping the web, analyzing information to generate automatic spreadsheet reports with graphs, communicating with automatically generated emails, getting notifications via text messages, and learning how to run tasks while your mind is focused on other more important stuff. Though some Python knowledge is required, the book is written with non- programmers in mind, giving clear and instructive recipes that will further the reader's proficiency while being oriented to specific day-to-day goals. [ v ] Preface Who this book is for This book is for Python beginners, not necessarily developers, that want to use and expand their knowledge to automate tasks. Most of the examples in the book are aimed at marketing, sales, and other non-tech areas. The reader needs to know a little of the Python language, including its basic concepts. What this book covers Chapter 1, Let's Begin Our Automation Journey, presents some basic content that will be used all through the book. It describes how to install and manage third-party tools through virtual environments, how to do effective string manipulation, how to use command-line arguments, and introduces you to regular expressions and other methods of text processing.