Top 10 Python Books on Amazon.Com

Here we have listed top 10 python programming books on Amazon.

Books are selected based on user rating (count and star). Books are listed in no particular order.

– Automate the Boring Stuff with Python

– Python Tricks: A Buffet of Awesome Python Features

– Python Programming: An Introduction to Computer Science, 3rd Ed

– Python Programming for the Absolute Beginner, 3rd Edition

– Python Programming: An Introduction to Computer Science

– Learn Python the Hard Way

– Learning Python, 5th Edition by Mark Lutz

– Python Crash Course: A Hands-On, Project-Based Introduction to Programming

– A Smarter Way to Learn Python: Learn it faster. Remember it longer.

– Invent Your Own Computer Games with Python

Please suggest if any other book should be in list.

 

Feature image source: https://goldenharebooks.com/

Encryption-Decryption in Python Django

Sometimes we need to encrypt critical information in out Django App. For example client might ask you to store the user information in encrypted format for extra security. Or you might be required to pass some data in URL in encrypted format.

Here we will see how can we encrypt and decrypt the information in Django.

Once you are done with initial setup of project and added the first app, create a new directory or add a new python package with the name utility  in your app.

Create _init__.py  file in the utility directory.

Add a new file, name it encryption_util.py  in utility directory.

Encryption:

Add a new function to encrypt the provided content. Use below code. Each line is explained by the accompanying comment.

Please note that:
– ENCRYPT_KEY should be kept safe. Keep it in settings_production.py  file and do not commit it to git.
– We are also converting the encoded string to urlsafe base64 format because we might required to pass the encoded data to URL.
– If there is any error, log it and return null.

Decryption:

To decrypt any encrypted text, we will just reverse the process.

Dependencies:

You need to install below python module.

– cryptography

Key:

You need to generate the ENCRYPT_KEY using below process.

– Open terminal in your virtual environment where cryptography python module is installed.
– Import Fernet.

– Generate key.

– Keep the key in settings file.

 

encryption decryption in django

Usage:

In the utility package we created in first step, we created the __init__.py  file. Add the below statement in this file.

Now use the encryption and decryption methods in your views.

Complete code:

Complete code for encryption_util.py  is below.

 

Host your Django App for Free.

How to Track Email Opens Sent From Django App

In this article we will see how to track the Email opens for the emails sent from your Django App. This will help us in accumulating the information such as:

– who opened the email
– when was email opened
– what is the open rate, i.e. how many user opened the email
– what is the time when most of the users check their email

Sending Email:

We have covered this part in detail in below articles. You may use any of these methods to send an email.

Sending bulk emails using mailgun api
Sending email using office 365
Sending email using Gmail

Create the HTML body for email:

In the email template email.html , at the end of the content, insert the below code.

This is an image of 0 by 0 pixels, which will not be visible in email but will hit the URL image_load  on our server when email is loaded.

Image src URL :

This is the URL which will be used as source of the image in email and will receive the hit whenever email will be opened.

Create a url in urls.py  file of your app.

Create a view image_load  in views.py  file.

This view is creating and returning an image as response to url image_load .

Testing:

Now send the email and open it. As soon as you open the email, you will receive a hit on the image_load  url.

This will NOT work on localhost. So I created a test app for free on pythonanywhere server (it took me just 5 minutes) and used the URL from there.

image hit url - email tracking

When I opened the test email, I could see the output printed in server logs.

image hit url - email tracking

 

When sending emails to multiple users, append the encrypted userId or emailId to the image source URL so that you can track who opened the email.

Try this and let us know your views.

 

 

Sending Emails Using Python and Gmail

We already know how to send email using office 365 or how to send bulk emails using mailgun api.

Here we will see how to send email from your python script or Django App using Gmail account.

Gmail have some daily limits on the number of emails you can send in a day. If you need to send bulk emails, use mailgun api.

Code:

Import dependencies:

Create message:

Create message body and attach to message:

Send the message:

 

Complete code with comments:
 

Available on Gitlab as well. Please comment your views.

Host your Django Application for free on PythonAnyWhere Servers.

 

Python Tip 1: Accessing localhost Django webserver over the Internet

We use Django development webserver to test our application on localhost.

We can start the development webserver with below command:

Now one problem we face with this approach is that we can access/browse the project over the browser only in the local network to which our system is connected.

What if you want to show your project to someone at another location, in another city over the Internet?

How would you generate a public URL for your localhost webserver?

 

For such situations, there is one software available. ngrok.

How to use it:

– Download the ngrok from its download page .

ngrok download

–  Follow the documentation to unzip the downloaded content and to add the authentication token.

– Go to your project directory and activate your virtual environment.

–  Now start your Django development server at port lets say 8080.

– You can access your project on localhost:8080. We will generate a public URL for this localhost URL by creating a tunnel using ngrok.

– Go to the directory where you unzipped ngrok and run below command.

– You will see something like this:

ngrok tunnel

– Now you can access your project over the public address http://9f134cf4.ngrok.io  .

–  You can access the web interface on http://127.0.0.1:4040 to see which URL got how many hits.

Public URL for localhost Django project

 

You can host your Django App for free on PythonAnyWhere server.

5 common mistakes made by beginner python programmers

During the initial days as python programmer, everyone of us face some or other type of weird bug in our code which, after spending multiple painful hours on Stackoverflow, turns out to be not a bug but python feature. That’s how things works in python. So below are the 5 most common mistakes most of the beginner python programmers make. Lets know a bit about them so that we can save few hours of asking questions on Facebook pages and groups.

 

1. Creating a copy of dictionary or lists.

Whenever you need to make a copy of a dictionary or list, do not simply use the assignment operator.

wrong way:

Now if you edit/update the dict_b , dict_a  will also be updated because by using assignment operator, you are trying to say that dict_b  will point to the same object to which dict_a  is pointing.

Right way:

Use the copy()  or deepcopy()  method.

See the difference between copy and deepcopy.

2. Dictionary keys.

Lets say we put the below values in a dictionary.

If we try to print the dictionary, what will be the output. Let’s see.

what just happened? where is the key  True .

Remember Boolean class is the subclass of Integer. Integer equivalent of True is 1 and that of False is 0. Hence value of key 1 is overwritten.

 

3. Updating lists or dictionaries.

Lets say you want to append an item to the list.

Try to update a dictionary.

Ok, lets try to sort a list.

Why nothing is being printed? What are we doing wrong?

Most the sequence object methods like sort, update, append, add etc works in place to increase performance by avoiding to create a separate copy un-necessarily.

Do not try to assign the output of such methods to variable.

Right way:

 

4.  Interned Strings.

In some cases, python try to reuse the existing immutable objects. String interning is one such case.

Here we tried to create two different string objects. But when checked if both the objects are same, it returned True. This is because python didn’t created another object b  but pointed the b to the first value “gmail”.

All strings of length 1 are interned. String having anything except ASCII characters, digits and underscore in them are not interned. Let’s see.

Also remember ==  is different than is  operator. ==  checks if values are equal or not while is  checks if both variable are pointing to same object.

So keep the above point in mind while using immutable strings or ==  or is  operator.

 

5. Default arguments are evaluated only once.

Consider below example.

What do you think will be the output of above two print statements.

Lets try to run it.

Why the output is [1,2]  in second case. Shouldn’t it be just [2] .

So the catch here is, default arguments of a function are evaluated on once. On first call i.e  func(1) , list lst  is evaluated and is found empty hence 1 is appended to it. But on second call, list is already having one element hence output is [1,2]

 

Bonus: Don’t mix spaces and tabs. Just don’t. You will cry.

Please comment if you find something is not correct.

Few useful python scripts.

 

How to upload and process the Excel file in Django

In this article we will discuss how to upload an Excel file and then process the content without storing file on server. One approach could be uploading the file, storing it in upload directory and then reading the file. Another approach could be uploading file and reading it directly from post data without storing it in memory and displaying the data.

We will work with the later approach here.

You may create a new project or work on existing code.

If you are setting up a new project then create a new virtual environment and install Django 2.0 and openpyxl modules in virtual environment using pip.

 

Continue reading “How to upload and process the Excel file in Django”

Creating sitemap of Dynamic URLs in your Django Application

A site map is a list of a website’s content designed to help both users and search engines navigate the site. A site map can be a hierarchical list of pages, an organization chart, or an XML document that provides instructions to search engine crawl bots.

Why sitemaps is required:

XML Sitemaps are important for SEO because they make it easier for Google to find your site’s pages—this is important because Google ranks web PAGES not just websites. There is no downside of having an XML Sitemap and having one can improve your SEO, so we highly recommend them.

Example:

Sitemap for this blog can be found at http://thepythondjango.com/sitemap_index.xml .

example sitemap
Example sitemap

Steps to add Sitemaps to your Django Application:

Create a file sitemap.py  in your app.

Create two different classes in sitemap.py file, one for static pages and another for Dynamic URLs.

Lets assume your website sell some product where product details are stored in database. Once a new product is added to database, you want that product page to be searchable by search engines. We need to add all such product pages/urls to sitemaps.

Static Sitemap:

Define a class StaticSitemap  in your sitemap.py  file. Define the mandatory function  items  in it which will return the list of objects. These objects will be passed to location method which will create URL from these objects.

Here in items function, we are returning appname:url_name  which will be used by location method to convert into absolute URL. Refer you app’s urls.py file for url names.

Dynamic Sitemap:

Similarly we will create Dynamic sitemap by fetching values from DB.

Here we are getting all products from database and generating URLs like  http:example.com/product/12 .

Adding sitemaps in URLconf:

Now add these sitemap class in URLconf. Edit the project’s urls.py  file and add below code in it.

 

Now reload your server and go to localhost:8000/sitemap.xml  and you will be able to see your sitemap there.

 

Reference : https://docs.djangoproject.com/en/2.0/ref/contrib/sitemaps/

 

Host your Django App for Free.

Adding Robots.txt file to Django Application

Robots.txt is a standard used by websites to communicate with web crawlers and other web robots. The standard specifies how to inform the web robot about which areas of the website should not be processed or scanned.

Why robots.txt is important:

Before a search engine crawls your site, it will look at your robots.txt file as instructions on where they are allowed to crawl/visit and index on the search engine results. If you want search engines to ignore any  pages on your website, you mention it in your robots.txt file.

Basic Format:
Example:
Steps to add robots.txt in Your Django Project:

Lets say your project’s name is myproject.

Create a directory templates in root location of your project. Create another directory with the same name as your project inside templates directory.

Place a text file robots.txt in it.

Your project structure should look something like this.

Add user-agent and disallow URL in it.

 

Now go to your project’s urls.py file and add below import statement

Add below URL pattern.

Now restart the server and go to localhost:8000/robots.txt in your browser and you will be able to see the robots.txt file.

Serving robots.txt from web server:

You can serve robots.txt directly from your web server. Below is the sample configuration for apache.

Quick Tips:
  1. robots.txt is case sensitive. The file must be named robots.txt, not Robots.txt or robots.TXT.
  2. robots.txt file must be placed in a website’s top-level directory.
  3. Make sure you’re not blocking any content or sections of your website you want crawled as this will not be good for SEO.

 

Host your Django App for Free.

Python Script 3: Validate, format and Beautify JSON string Using Python

As per official JSON website, JSON is a light-weight data interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999.

In this small article we will see how to validate and format the JSON string using python.

Format JSON string:
output:
Continue reading “Python Script 3: Validate, format and Beautify JSON string Using Python”