Book Image

Python Programming Blueprints

By : Daniel Furtado, Marcus Pennington
Book Image

Python Programming Blueprints

By: Daniel Furtado, Marcus Pennington

Overview of this book

Python is a very powerful, high-level, object-oriented programming language. It's known for its simplicity and huge community support. Python Programming Blueprints will help you build useful, real-world applications using Python. In this book, we will cover some of the most common tasks that Python developers face on a daily basis, including performance optimization and making web applications more secure. We will familiarize ourselves with the associated software stack and master asynchronous features in Python. We will build a weather application using command-line parsing. We will then move on to create a Spotify remote control where we'll use OAuth and the Spotify Web API. The next project will cover reactive extensions by teaching you how to cast votes on Twitter the Python way. We will also focus on web development by using the famous Django framework to create an online game store. We will then create a web-based messenger using the new Nameko microservice framework. We will cover topics like authenticating users and, storing messages in Redis. By the end of the book, you will have gained hands-on experience in coding with Python.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Dedication
Contributors
Packt Upsell
Preface
Index

Preface

If you have been within the software development industry for the last 20 years, you most certainly have heard of a programming language named Python. Created by Guido van Rossum, Python first appeared in 1991 and has captured the hearts of many software developers across the globe ever since.

However, how is it that a language that is over 20 years old is still around and is gaining more and more popularity every day?

Well, the answer to this question is simple. Python is awesome for everything (or almost everything). Python is a general-purpose programming language, which means that you can create simple terminal applications, web applications, microservices, games, and also complex scientific applications. Even though it is possible to use Python for different purposes, Python is a language that is well known for being easy to learn, which is perfect for beginners as well as people with no computer science background.

Python is a batteries included programming language, which means that most of the time you will not need to make use of any external dependencies when developing your projects. Python's standard library is feature rich and most of the time contains everything you need to create your programs, and just in case you need something that is not in the standard library, the PyPI (Python Package Index) currently contains 117,652 packages.

The Python community is welcoming, helpful, diverse, and extremely passionate about the language, and everyone in the community is always happy to help each other.

If you still not convinced, the popular website StackOverflow published this year's statistics about the popularity of programming languages based on the number of questions the users add to the site, and Python is one of the top languages, only behind JavaScript, Java, C#, and PHP.

It is a perfect time to be a Python developer, so let's get started!

Who this book is for

This book is for software developers who are familiar with Python and want to gain hands-on experience with web and software development projects. Basic knowledge of Python programming is required.

What this book covers

Chapter 1, Implementing the Weather Application, guides you through developing a terminal application that shows the current weather for a specific region and a forecast for the next 5 days. This chapter will introduce you to the basic concepts of Python programming. You will learn how to parse command-line arguments to add more interactivity to programs, and you will finally see how to scrape data from websites using the popular Beautiful Soup framework.

Chapter 2, Creating a Remote-Control Application with Spotify, will teach you how to perform authentication with the Spotify API using OAuth. We will use the curses library to make the application more interesting and user-friendly.

Chapter 3, Casting Votes on Twitter, will teach you how to use the Tkinter library to create beautiful user interfaces using Python. We will use Reactive Extensions for Python to detect when a vote has been made in the backend, after which, we will publish the changes in the user interface.

Chapter 4, Exchange Rates and the Currency Conversion Tool, will enable you to implement a currency converter that will get foreign exchange rates in real time from different sources and use the data to perform currency conversion. We will develop an API that contains helper functions to perform the conversions. To start with, we will use opensource foreign exchange rates and a currency conversion API (http://fixer.io/). The second part of the chapter will teach you how to create a command-line application makes use of our API to fetch data from the data sources and also get the currency conversion results with a few parameters.

Chapter 5Building a Web Messenger with Microservices, will teach you how to use Nameko, a microservice framework for Python. You will also learn how to make dependency providers for external resources such as Redis. This chapter will also touch upon integration testing Nameko services and basic AJAX requests to an API.

Chapter 6Extending TempMessenger with a User Authentication Microservice, will build upon your app from Chapter 5Building a Web Messenger with Microservices. You will create a user authentication microservice that stores users in a Postgres database. Using Bcrypt, you will also learn how to store passwords in a database securely. This chapter also covers creating a Flask web interface and how to utilize cookies to store web session data. By the end of these chapters, you will be well equipped to create scalable and cohesive microservices.

Chapter 7, Online Video Game Store with Django, will enable you to create an online video game store. It will contain features such as browsing video games by category, performing searches using different criteria, viewing detailed information about each game, and finally adding games to a shopping cart and placing an order. Here, you will learn about Django 2.0, the administration UI, the Django data model, and much more.

Chapter 8, Order Microservice, will help you build a microservice that will be responsible for receiving orders from the web application that we developed in the previous chapter. The order microservice also provides other features such as the ability to update the status of orders and provide order information using different criteria.

Chapter 9, Notification Serverless Application, will teach you about Serverless functions architecture and how to build a notification service using Flask and deploy the final application to AWS Lambda using the great project Zappa. You will also learn how to integrate the web application that was developed in Chapter 7Online Video Game Store with Django, and the order microservice developed in Chapter 8, Order Microservice, with the serverless notification application.

To get the most out of this book

In order to execute the code from this book on your local machine, you will need the following:

All other requirements will be installed as we progress through the chapters. All instructions in this chapter are tailored toward macOS or Debian/Ubuntu systems; however, the authors have taken care to only use cross-platform dependencies.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Python-Programming-Blueprints. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."

A block of code is set as follows:

html, body, #map {
 height: 100%; 
 margin: 0;
 padding: 0
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

[default]
exten => s,1,Dial(Zap/1|30)
exten => s,2,Voicemail(u100)
exten => s,102,Voicemail(b100)
exten => i,1,Voicemail(s0)

Any command-line input or output is written as follows:

$ mkdir css
$ cd css

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.