Posted on Leave a comment

Creating Beautiful Heatmaps with Seaborn

Heatmaps are a specific type of plot which exploits the combination of color schemes and numerical values for representing complex and articulated datasets. They are largely used in data science application that involves large numbers, like biology, economics and medicine.

In this video we will see how to create a heatmap for representing the total number of COVID-19 cases in the different USA countries, in different days. For achieving this result, we will exploit Seaborn, a Python package that provides lots of fancy and powerful functions for plotting data.

Here’s the code to be discussed:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns #url of the .csv file
url = r"path of the .csv file" # import the .csv file into a pandas DataFrame
df = pd.read_csv(url, sep = ';', thousands = ',') # defining the array containing the states present in the study
states = np.array(df['state'].drop_duplicates())[:40] #extracting the total cases for each day and each country
overall_cases = []
for state in states: tot_cases = [] for i in range(len(df['state'])): if df['state'][i] == state: tot_cases.append(df['tot_cases'][i]) overall_cases.append(tot_cases[:30]) data = pd.DataFrame(overall_cases).T
data.columns = states #Plotting
fig = plt.figure()
ax = fig.subplots()
ax = sns.heatmap(data, annot = True, fmt="d", linewidths=0, cmap = 'viridis', xticklabels = True)
ax.invert_yaxis()
ax.set_xlabel('States')
ax.set_ylabel('Day n°')
plt.show()

Let’s dive into the code to learn Seaborn’s heatmap functionality in a step-by-step manner.

Importing the required libraries for this example

We start our script by importing the libraries requested for running this example; namely Numpy, Pandas, Matplotlib and Seaborn.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

What’s in the data?

As mentioned in the introduction part, we will use the COVID-19 data that were also used in the article about Scipy.curve_fit() function. Data have been downloaded from the official website of the “Centers for Disease Control and Prevention” as a .csv file.

The file reports multiple information regarding the COVID-19 pandemic in the different US countries, such as the total number of cases, the number of new cases, the number of deaths etc…; all of them have been recorded every day, for multiple US countries.

We will generate a heatmap that displays in each slot the number of total cases recorded for a particular day in a particular US country. To do that, the first thing that should be done is to import the .csv file and to store it in a Pandas DataFrame.

Importing the data with Pandas

The data are stored in a .csv file; the different values are separated by a semi-colon while the thousands symbol is denoted with a comma. In order to import the .csv file within our python script, we exploit the Pandas function .read_csv() which accepts as input the path of the file and converts it into a Pandas DataFrame.

It is important to note that, when calling .read_csv(), we specify the separator, which in our case is “;” by saying “sep = ‘;’” and the symbol used for denoting the thousands, by writing “thousands = ‘,’”. All these things are contained in the following code lines:

#url of the .csv file
url = r"path of the file" # import the .csv file into a pandas DataFrame
df = pd.read_csv(url, sep = ';', thousands = ',')

Creating the arrays that will be used in the heatmap

At this point, we have to edit the created DataFrame in order to extract just the information that will be used for the creation of the heatmap.

The first values that we extract are the ones that describe the name of the countries in which the data have been recorded. To better identify all the categories that make up the DataFrame, we can type “df.columns” to print out the header of the file. Among the different categories present in the header, the one that we are interested in is “state”, in which we can find the name of all the states involved in this chart.

Since the data are recorded on daily basis, each line corresponds to the data collected for a single day in a specific state; as a result, the names of the states are repeated along this column. Since we do not want any repetition in our heatmap, we also have to remove the duplicates from the array.

We proceed further by defining a Numpy array called “states” in which we store all the values present under the column “state” of the DataFrame; in the same code line, we also apply the method .drop_duplicates() to remove any duplicate of that array. Since there are 60 states in the DataFrame, we limit our analysis to the first 40, in order not to create graphical problems in the labels of the heatmap x-axis, due to the limited window space.

#defining the array containing the states present in the study
states = np.array(df['state'].drop_duplicates())[:40] 

The next step is to extract the number of total cases, recorded for each day in each country. To do that, we exploit two nested for loops which allow us creating a list containing the n° of total cases (an integer number for each day) for every country present in the “states” array and appending them into another list called “overall_cases” which needs to be defined before calling the for loop.

#extracting the total cases for each day and each country
overall_cases = []

As you can see in the following code, in the first for loop we iterate over the different states that were previously stored into the “states” array; for each state, we define an empty list called “tot_cases” in which we will append the values referred to the total cases recorded at each day.

for state in states: tot_cases = []

Once we are within the first for loop (meaning that we are dealing with a single state), we initialize another for loop which iterates through all the total cases values stored for that particular state. This second for loop will start from the element 0 and iterate through all the values of the “state” column of our DataFrame. We achieve this by exploiting the functions range and len.

 for i in range(len(df['state'])):

Once we are within this second for loop, we want to append to the list “tot_cases” only the values that are referred to the state we are currently interested in (i.e the one defined in the first for loop, identified by the value of the variable “state”); we do this by using the following if statement:

 if df['state'][i] == state: tot_cases.append(df['tot_cases'][i])

When we are finished with appending the values of total cases for each day of a particular country to the “tot_cases” list, we exit from the inner for loop and store this list into the “overall_cases” one, which will then become a list of lists. Also in this case, we limit our analysis to the first 30 days, otherwise we would not have enough space in our heatmap for all the 286 values present in the DataFrame.

 overall_cases.append(tot_cases[:30])

In the next iteration, the code will start to analyze the second element of the “states” array, i.e. another country, will initialize an empty list called “tot_cases” and enter in the second for loop for appending all the values referred to that country in the different days and eventually, once finished, append the entire list to the list “overall_cases”; this procedure will be iterated for all the countries stored in the “states” array. At the end, we will have extracted all the values needed for generating our heatmap. 

Creating the DataFrame for the heatmap

As already introduced in the first part, we exploit the Seaborn function .heatmap() to generate our heatmap.

This function can take as input a pandas DataFrame that contains the rows, the columns and all the values for each cell that we want to display in our plot. We hence generate a new pandas DataFrame (we call it “data”) that contains the values stored in the list “overall_cases”; in this way, each row of this new DataFrame is referred to a specific state and each column to a specific day.

We then transpose this DataFrame by adding “.T” at the end of the code line, since in this way we can then insert the name of the states as the header of our Dataframe.

data = pd.DataFrame(overall_cases).T

The names of the states were previously stored in the array “states”, we can modify the header of the DataFrame using the following code:

data.columns = states

The DataFrame that will be used for generating the heatmap will have the following shape:

   CO  FL  AZ  SC  CT  NE  KY  WY  IA  ...  LA  ID  NV  GA  IN  AR  MD  NY  OR 0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0 1   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0 2   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0 3   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0 4   0   0   1   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0 

The row indexes represent the n° of the day in which the data are recorded while the columns of the header are the name of the states.

Generating the heatmap

After generating the usual plot window with the typical matplotlib functions, we call the Seaborn function .heatmap() to generate the heatmap.

The mandatory input of this function is the pandas DataFrame that we created in the previous section. There are then multiple optional input parameters that can improve our heatmap:

  • linewidths allows adding a white contour to each cell to better separate them, we just have to specify the width;
  • xticklabels modify the notation along the x-axis, if it’s equal to True, all the values of the array plotted as the x-axis will be displayed.
  • We can also chose the colormap of the heatmap by using cmap and specifying the name of an available heatmap (“viridis” or “magma” are very fancy but also the Seaborn default one is really cool);
  • finally, it is possible to display the numerical value of each cell by using the option annot = True; the numerical value will be displayed at the center of each cell.

The following lines contain the code for plotting the heatmap. One final observation regards the command .invert_yaxis(); since we plot the heatmap directly from a pandas DataFrame, the row index will be the “day n°”; hence it will start from 0 and increase as we go down along the rows. By adding .invert_yaxis() we reverse the y-axis, having day 0 at the bottom part of the heatmap.

#Plotting
fig = plt.figure()
ax = fig.subplots()
ax = sns.heatmap(data, annot = True, fmt="d", linewidths=0, cmap = 'viridis', xticklabels = True)
ax.invert_yaxis()
ax.set_xlabel('States')
ax.set_ylabel('Day n°')
plt.show() 

Figure 1 displays the heatmap obtained by this code snippet.

Figure 1: Heatmap representing the number of COVID-19 total cases for the first 30 days of measurement (y-axis) in the different USA countries (x-axis).

As you can see in Figure 1, there are a lot of zeroes, this is because we decided to plot the data related to the first 30 days of measurement, in which the n° of recorded cases were very low. If we decided to plot the results from all the days of measurement (from day 0 to 286), we would obtain the result displayed in Figure 2 (in this latter case, we placed annot equal to False since the numbers would have been too large for the cell size):

Figure 2: Heatmap representing the number of COVID-19 total cases for the first 286 days of measurement (y-axis) in the different USA countries (x-axis); this time annot = False, since the cells are too small for accommodating the n° of total cases (which becomes very large towards the upper part of the heatmap).

The post Creating Beautiful Heatmaps with Seaborn first appeared on Finxter.

Posted on Leave a comment

Python delattr()

Python’s built-in delattr() function takes an object and an attribute name as arguments and removes the attribute from the object. The call delattr(object, 'attribute') is semantically identical to del object.attribute.

This article shows you how to use Python’s built-in delattr() function.

Usage

Learn by example! Here’s an example on how to use the delattr() built-in function.

Create a Car object with one attribute speed.

# Define class with one attribute
class Car: def __init__(self): self.speed = 100 # Create object
porsche = Car()

Print the attribute speed:

# What's the value for attribute speed?
print(porsche.speed)
# 100

Use delattr(porsche, speed) to remove the attribute speed from the object porsche.

# Remove the attribute speed from porsche
delattr(porsche, 'speed')

After removing the attribute, it cannot be accessed anymore:

# Does this still work?
print(porsche.speed)
# No: '''
Traceback (most recent call last): File "C:\Users\xcent\Desktop\Finxter\Blog\HowToConvertBooleanToStringPython\code.py", line 18, in <module> print(porsche.speed)
AttributeError: 'Car' object has no attribute 'speed' '''

Syntax delattr()

The delattr() object has the following syntax:

Syntax: 
delattr(object, attribute) # Removes attribute from object
Arguments object The object from which the attribute should be removed
string The attribute to be removed
Return Value None Returns Nothing. If the attribute doesn’t exist, the method does nothing.

Interactive Shell Exercise: Understanding delattr()

Consider the following interactive code:

Exercise: Does the code work? If yes, run it! If not, fix the bug!


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


Python del vs delattr()

The alternative to Python’s built-in delattr() is to use the del keyword that is also built-in.

The delattr(object, 'attribute') is semantically identical to the del object.attribute call. Note that in the first case, the attribute is given as a string, while in the second case, the attribute is given as a normal attribute name.

# Define class with one attribute
class Car: def __init__(self): self.speed = 100 # Create object
porsche = Car() # What's the value for attribute speed?
print(porsche.speed) # Remove the attribute speed from porsche
del porsche.speed # Does this still work?
print(porsche.speed)

The output is the same:

100
Traceback (most recent call last): File "C:\Users\xcent\Desktop\Finxter\Blog\HowToConvertBooleanToStringPython\code.py", line 17, in <module> print(porsche.speed)
AttributeError: 'Car' object has no attribute 'speed'

Related Functions

  • The getattr() function returns the value of an attribute.
  • The hasattr() function checks if an attribute exists.
  • The setattr() function sets the value of an attribute.

Summary

Python’s built-in delattr() function takes an object and an attribute name as arguments and removes the attribute from the object.


I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python delattr() first appeared on Finxter.

Posted on Leave a comment

6 Best Python NLP Libraries

If you are a data scientist or aspire to be one investing your time in learning natural language processing (NLP) will be an investment in your future. 2020 saw a surge in the field of natural language processing. In this blog post you will discover 5 popular NLP libraries, and it’s applications.

Preprocessing Libraries

Preprocessing a crucial step in any machine learning pipeline. If you are building a language model you would have to create a word vector which involves removing stop words, and converting words to its root form.

#1 Spacy

Spacy is a popular Python library for sentence tokenization, lemmatization, and stemming. It is an industry grade library which can be used for text preprocessing and training deep learning based text classifiers.

Getting started with Spacy: Named Entity Recognition is an important task in natural language processing. NER helps in extracting important entities like location, organization names, etc.

import spacy # python -m spacy download en_core_web_sm
nlp = spacy.load('en_core_web_sm') sentences = ['Stockholm is a beautiful city', 'Mumbai is a vibrant city' ] for sentence in sentences: doc = nlp(sentence) for entity in doc.ents: print(entity.text, entity.label_) print(spacy.explain(entity.label_))

The above code processes the two sentences and extracts the location in both sentences.

Let us now see the output

As seen from the output the code was able to extract Stockholm and Mumbai and associated them with the GPE label which indicates countries, cities, or states.

#2 NLTK

NLTK is another popular Python library for text preprocessing. It was started as an academic project and soon became very popular amongst researchers and academicians.

Let us see how we can do Part of Speech Tagging using NLTK. Part of speech tagging is used to extract the important part of speech like nouns, pronouns, adverbs, adjectives, etc.

import nltk
import os sentence = "Python is a beautiful programming language."
tokens = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokens)
entities = nltk.chunk.ne_chunk(tagged)
print(entities)

The parts of speech that were extract from the above sentence are

 (S   (GPE Python/NNP)   is/VBZ   a/DT   beautiful/JJ   programming/NN   language/NN   ./.) 

Applications

A popular application of NLP is to categorize a document into a given set of labels. There are a number of Python libraries which can help you to train deep learning based models for topic modeling, text summarization, sentiment analysis etc. Let us have a look at some of these popular libraries

Most deep learning based NLP models rely on pretrained language models using a process called transfer learning. A huge corpus of document is trained and then this model can be fine-tuned for a specific domain. Some popular libraries which help in using pretrained models and building industry grade NLP applications are

#3 FARM

Farm is a popular open source package developed by a Berlin based company. It is used to make the life of developers easier by providing some nice functionalities like experiment tracking, multitask-learning and parallelized processing of documents.

#4 Flair

Flair is a popular PyTorch based framework which helps developers to build state of the NLP applications like named entity recognition, part-of-speech tagging, sense disambiguation and classification.

#5 Transformers

Transformers is a popular Python library to easily access pretrained models and has support for both PyTorch and TensorFlow. If you want to build an entire NLP pipeline by using pretrained models for Natural language understanding and generation tasks transformers will make your life easier.

#6 Gensim

Gensim is another popular Python library widely used for topic modelling and provides an easy-to-use interface for popular algorithms like word2vec to find synonymous words.

The post 6 Best Python NLP Libraries first appeared on Finxter.

Posted on Leave a comment

Python compile()

If you’re like me, you love those TLDR; overviews to grasp the big picture quickly. Here is mine about Python’s compile() function:

Python’s built-in compile() method returns an executable code object as an “Abstract Syntax Tree” represented as an ast object. By passing this code object into the exec() or eval() functions, you can run it dynamically in your Python code. This way, you can programmatically create source code and execute it at runtime. To use the function, pass the string code to be executed, the filename, and the execution mode. For example compile('print("hi")', '<string>', 'exec') creates a code object consisting of one line print("hi").

This article shows you how to use Python’s built-in compile() method.

Python compile() built-in function -- Illustrated Explanation

Usage compile()

Learn by example! Let’s say, you have a code file with the following content:

# some_code.py file
customers = {"Alice": 40000, "Bob": 33000}
print(f"Bob: {customers['Bob']}")

Here’s an example on how to use the compile() built-in function.

# Read the code from the file or define it explicitly:
code = 'customers = {"Alice": 40000, "Bob": 33000}\nprint(f"Bob: {customers[\'Bob\']}")' # Create the ast code object
code_obj = compile(code, '<string>', 'exec') # Execute the ast code object
exec(code_obj) # Result:
# Bob: 33000

First, you create the code as a string. Second, you pass the string into the compile() function to create an ast object. You can then pass this object into the exec() function and run it dynamically.

Video compile()

Why Using compile() Instead of exec() With a String?

Before you dive into the syntax, you may not be motivated to use the compile() function in the first place. Why? Because you can also use the exec() function on a string instead of a code object.

Python’s exec() function executes the Python code you pass as a string or executable object argument. This is called dynamic execution because, in contrast to normal static Python code, you can generate code and execute it at runtime. This way, you can run programmatically-created Python code.

Here’s the same code without compile():

code = 'customers = {"Alice": 40000, "Bob": 33000}\nprint(f"Bob: {customers[\'Bob\']}")'
exec(code)
# Bob: 33000

The output is the same. However, there are two advantages of using a code object:

  • You make it explicit. Without compiling the string explicitly, the exec() method would do the same work implicitly. Thus, if you need to run the same code multiple times, you can save significant compilation overhead by just compiling it once in advance.
  • You can use the powerful ast object otherwise. This gives you access to some helper methods and additional information about the code such as the names defined in the code:
>>> code_obj.co_names
('customers', 'print')

You obtain this information from a compiled string which is very convenient!

Syntax compile()

You can use the compile() method with a number of different arguments.

Syntax: 
compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) 
Arguments source A string to be compiled into a code object.
filename The file from which the code given in source was read. If this was a string, use '<string>'.
mode The execution mode—must be one of the following:
'exec' — If source is a sequence of statements
'eval' — If source is a single expression
'single' —If source is a single interactive statement
Optional: flags=0 Controls which compiler options should be activated and which future features should be allowed.
Optional: dont_inherit=False Do you want to avoid inheritance of the compiler options and future features?
Optional: optimize=-1 Optimization level of the compiler:
-1 selects the optimization level of the interpreter as given by -O options.
0 selects no optimization and __debug__ to True.
1 specifies that asserts are removed and sets __debug__ to False.
2 additionally removes docstrings.
Return Value ast Returns an AST object that represents the code as an abstract syntax tree.

Interactive Shell Exercise: Understanding compile()

Consider the following interactive code:

Exercise: Print the number associated to Alice in the dictionary!


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


How to Read and Compile Code from a File

Say, you have the following code in a code file:

# filename.py file
customers = {"Alice": 40000, "Bob": 33000}
print("Bob: " + str(customers['Bob']))

How to read the code from a file, compile it, and execute it at runtime?

Here’s the code:

# 1. Read code from file
f = open('filename.py', 'r')
code = f.read()
f.close() # 2. Compile code string
code_obj = compile(code, 'filename.py', 'exec') # 3. Run the code object (ast)
exec(code_obj)

Alternatively, you can also use the following one-liner to read the code from a file:

code = open('filename.py').read()

Frankly, I don’t see a huge problem with not closing the file if you have a small script and you don’t access the file anywhere else. Python will close it automatically after the code terminates.

Summary

The Python compile() method returns an executable code object as an “Abstract Syntax Tree” that is represented as an ast object.

There are many applications of an ast such as the following: You can pass this code object into the exec() or eval() functions and run it dynamically in your Python code.

This way, you can programmatically create source code and execute it at runtime.

To use the function, you must pass the string code to be executed, the filename, and the execution mode.

For example compile('print("hi")', '<string>', 'exec') would be a valid call that creates a code object consisting of one line print("hi").


I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python compile() first appeared on Finxter.

Posted on Leave a comment

Python eval()

This tutorial shows you how to use Python’s built-in eval() function.

Why Using It? The main application of eval() is to take user input at runtime and run it as a Python expression. This way, you can create a calculator or allow users to perform custom computations on a computing cluster. However, this use also poses the biggest security risk: the user can run byzantine (=harmful) code on your server environment!

How does it work? TLDR;

Python eval(s) parses the string argument s into a Python expression, runs it, and returns the result of the expression. This poses a security risk because a user can use it to run code on your computer. For example, if you allow eval(input()), a user could type os.system('rm -R *') to delete all files in your home directory.

Usage Examples

Learn by example! Here are some examples of how to use the eval() built-in function:

>>> eval('2+2')
4
>>> eval('[1, 2, 3]*3')
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> eval('[x for x in range(10)]')
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> eval('"Alice".replace("e", "")') 'Alic'

You can run any Python code that has a return value within the eval() code. You can even create your own function and run it within eval():

>>> def f(): return 42 >>> eval('f()')
42

This gives you great flexibility in how you use the function to run any string expression you may encounter in Python and it allows you to create Python code programmatically and evaluate it at runtime.

Python eval() - Visual Explanation

Syntax eval()

You can use the eval() method with three different argument lists.

Syntax: 
eval(string)
eval(string, globals)
eval(string, globals, locals) 
Arguments string A string to be evaluated.
globals Optional, default None. A dictionary in which you can define variables that should be globally accessible by the executed object (local namespace).
locals Optional, default None. A dictionary in which you can define variables that should be locally accessible by the executed object (global namespace).
Return Value object Returns the result of parsing the string argument and running it as a Python expression.

Python eval() Return Value

The return value of eval() is a Python object that is the result of parsing the string argument and running it as a Python expression. The code can have side effects which means that it may change the state of your program or even your computer!


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners

[Danger Zone] Python eval(input()) User Input

You can use the eval() function to run code that is typed in dynamically by the user:

def dangerous_function(): # Do nasty stuff like removing files # or creating trojan horses print('You were hacked!') return 42 eval(input())

This is how the user may interact with your code at runtime:

dangerous_function()
You were hacked! 42

You see that the dangerous_function() was executed which could contain all kinds of dangerous code. If you run this on your server, the user may attempt to remove all files on your server! For example, the user may use the command os.system('rm -rf *') to remove all files and folders.

Interactive Jupyter Notebook eval()

Exercise: Run the following interactive code and try to run the dangerous function in the interactive Jupyter notebook!


Python exec() vs eval()

Python’s exec() function takes a Python program, as a string or executable object, and runs it. The eval() function evaluates an expression and returns the result of this expression. There are two main differences:

  • exec() can execute all Python source code, whereas eval() can only evaluate expressions.
  • exec() always returns None, whereas eval() returns the result of the evaluated expression.
  • exec() can import modules, whereas eval() cannot.
exec('x=2;y=2;z=x*y;print(z)')
# 4 print(eval('2*2'))
# 4

Python eval() import

Can you import a Python library within the eval() function? No, you can’t! The import statement is a statement, not an expression. But eval() can only execute expressions. A simple workaround is to create a function with side effects that imports the module within the function body:

def f(): import random return random.randint(0, 9) print(eval('f()'))
# 4

Per default, the eval() function has access to all names in the dir() namespace, so you can also import the library globally and use it within the eval() function:

import random
print(eval('random.randint(0, 9)'))

How to Restrict the Use of Built-in Functions Within eval()

If you don’t want to allow users to access built-in functions, you can restrict this by providing the globals argument as follows:

eval(expression, {'__builtins__': None})

For example:

>>> eval('sum([1, 2, 3])')
6
>>> eval('sum([1, 2, 3])', {'__builtins__': None})
Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> eval('sum([1, 2, 3])', {'__builtins__': None}) File "<string>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable

After restricting the built-in functions in the second call, Python raises an error NoneType object is not subscriptable. This reduces the security risks of your application.

Summary

Python eval(s) parses the string argument s into a Python expression, runs it, and returns the result of the expression.

>>> eval('2+2')
4

This poses a security risk because a user can use it to run code on your computer. For example, if you allow eval(input()), a user could type import os; os.system('rm -R *') to delete all files in your home directory.


I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python eval() first appeared on Finxter.

Posted on Leave a comment

Unreal Engine As A 3D Modeling & Sculpting Application

The most recent releases of Unreal Engine now include new beta Unreal Modeling Tools Editor Mode enabling you to create, sculpt and even texture entirely in Unreal Engine. If you want to check out the new features, you need to enable the plugin. Don’t worry, there are step by step instructions available below

In Unreal Engine, select Edit->Plugins.

In the Plugins dialog, filter by Model and locate and select Modeling Tools Editor Mode and click the Enabled checkbox.

Modeling Tools Editor Mode in Unreal Engine

This will first prompt you if you want to continue due to it being an experimental feature. Allow this, then it will prompt you to restart Unreal Engine, click Restart Now.

New plugin restart Unreal Engine

Once your project has restarted, you can access the new modeling tools in the Modes menu by selecting Modeling.

Modeling Mode in Unreal Engine

Once enabled a new toolbar will be available with options for creating new geometry from primitives or other creation modes, tools for modifying and deforming as well as sculpting geometry and much more.

Go hands-on with the Unreal Modeling Tools Editor Mode plugin in the video below.

[youtube https://www.youtube.com/watch?v=9SgD17vqU3A?feature=oembed&w=1500&h=844]
Posted on Leave a comment

A Guide to Python’s pow() Function

Exponents are superscript numbers that describe how many times you want to multiply a number by itself. Calculating a value raised to the power of another value is a fundamental operation in applied mathematics such as finance, machine learning, statistics, and data science. This tutorial shows you how to do it in Python!

Definition

For pow(x, y), the pow() function returns the value of x raised to the power y. It performs the same function as the power operator ** , i.e. x**y, but differs in that it comes with an optional argument called mod.

Examples without mod

>>> pow(5, 2)
25
>>> pow(-3, 3)
-27
>>> pow(2, -2)
0.25

Parameters and Syntax

pow(base, exp, mod=None)

The pow() function includes two compulsory arguments, base and exp, and one optional argument, mod, whose default value is None. All arguments must be of numeric data type.

Parameter Description
exp A number that represents the base of the function, whose power is to be calculated.
base A number that represents the exponent of the function, to which the base will be raised.
mod A number with which the modulo will be computed.

Return value: The output of base raised to the power exp and will be a numeric data type, int, float or complex, depending on what you input.

Using the pow() function without the mod argument

When using the pow(x, y) function without the optional mod argument, it will perform the same operation as the power operator x**y, raising x to the power y.

Comparison of the two methods

>>> pow(6, 4)
1296
>>> 6 ** 4
1296

The pow() function accepts all numeric data types, i.e. int, float and even complex numbers. In general the return value will depend on what data types you input. The example above shows that both arguments are type int, therefore, an int type is returned. However, if you were to instead use a float number as one or both of the arguments, the function will automatically return a float type.

Examples using float types

>>> pow(3.0, 4)
81.0
>>> pow(4.5, 2.3)
31.7971929089206

As with float type inputs leading to float outputs, the same reasoning applies to complex numbers. If you enter a complex number as one or both of the arguments, a complex number will be returned.

Example using complex numbers

>>> pow(4+2j, 3)
(16+88j)

The return type will also depend on whether your arguments are non-negative or negative, as is shown in the below table.

base exp Return type
Non-negative Non-negative int
Non-negative Negative foat
Negative Non-negative int
Negative Negative float

Examples of return values with different input types

>>> pow(7, 2)
49
>>> pow(4, -5)
0.0009765625
>>> pow(-6, 3)
-216
>>> pow(-9, -4)
0.00015241579027587258

Using the pow() function with a mod argument

What sets the pow() function apart from the ** operator is its third optional argument, mod, which gives you the ability to do a modulo operation within the function. 

The process of operations when using the mod argument is as follows:

If we have pow(x, y, z), the function first performs the task of raising x to the power y and then that result is used to perform the modulo task with respect to z. It would be the equivalent of (x**y) % z .

Examples using mod

>>> pow(14, 7, 5)
4
>>> pow(-8, 3, 5)
3
>>> pow(2, 4, -3)
-2

The general rule for using the mod argument is that all values must be of integer type, the exp argument must be non-negative and the mod argument must be non-zero. However, Python 3.8 now comes with the functionality of computing modular inverses. In this case, the exp argument may be negative, on the condition that base is relatively prime to mod, i.e, the only common integer divisor of base and mod is 1.

So, when using the pow() function with negative exp, the function will perform as follows:

pow(inv_base, -exp, mod)

In other words, the function will compute the modular inverse of base and mod first and then that result will be used in the pow() function as base to be computed as normal with the  exp argument being converted to its non-negative counterpart.

Example of modular inverse

>>> pow(87, -1, 25)
23

In this example, the straight modular inverse is calculated because inv_base will be raised to the power 1.

Example of modular inverse when exp is not -1

>>> pow(34, -5, 19)
10
# The modular inverse of 34 mod 19 is 14, therefore, we end up with the function pow(14, 5, 19)
>>> pow(14, 5, 19)
10

Calculating the nth root of a number using pow()

Unfortunately, Python does not have a built-in function to calculate the nth root of a number. The math module only has a function to calculate square roots, math.sqrt(), therefore, we have to get creative in order to calculate nth roots.

We know that nx is equivalent to x1n. Thus, using this knowledge we can calculate the nth root in Python by using either pow(x, (1/n)) or x**(1/n)

Examples of calculating nth roots

>>> pow(625, (1/4))
4.0
>>> 729**(1/3)
8.999999999999998

Note that performing an nth root calculation will always return a float when not using complex numbers. Since Python’s float type works on approximations, it will often return the approximation rather than the exact number, even when an exact answer is possible. This is demonstrated in the second example above.

When calculating the nth root of a negative number, the return value will be a complex number whether an integer number is possible or not. 

Examples of calculating nth roots of negative bases

>>> pow(-16, (1/2))
(2.4492935982947064e-16+4j)
>>> pow(-27, (1/3))
(1.5000000000000004+2.598076211353316j)

We would expect the second example above, the cubed root of -27, to result in -3, but instead we get a complex number. This is because Python returns the principal root rather than the real root. For an explanation of these different types of roots, you can look up the Fundamental theorem of algebra.

math.pow() Function

In the math module of Python, there is a similar function called math.pow(). To use this we first need to import the math function, thus, the built-in pow() function will be very slightly faster. The main differences between the two functions is that math.pow() does not allow for the optional mod argument and it will always return a float. So if you want to ensure that you get a float result, math.pow() is a better option.

Example of using math.pow()

>>> import math
>>> math.pow(9, 5)
59049.0

When to use the pow() function vs when to use the ** operator

When deciding between using the pow() function or the ** operator, the most important factor to consider would be the efficiency of your code. We can use the timeit.timeit() function from the timeit module to find out how fast Python executes our code.

Examples of using timeit with simple numbers

>>> import timeit
>>> timeit.timeit('pow(5, 2)')
0.25059129999863217
>>> timeit.timeit('5**2')
0.008814800001346157

When performing a simple power computation, the ** operator appears to be much faster.

Examples using modulo

>>> timeit.timeit('pow(52, 2, 4)')
0.7482693000001746
>>> timeit.timeit('52**2 % 4')
0.012026999998852261

The same is true even when we include a modulo operation. 

However, when we want to perform power operations with very large numbers, the pow() function is much quicker, showing that the power of the pow() function lies in executing longer computations.

Examples using large numbers

>>> timeit.timeit('pow(5234, 2341, 124)')
0.9020593000004737
>>> timeit.timeit('5234**2341 % 124')
152.56075580000106

Here the pow() function is extremely fast compared to the ** operator. Therefore, we can generalize these findings by saying that when you want to perform short, simple calculations, the ** operator is the better option, however, if your operations involve very large numbers, the pow() function is much more efficient.

The post A Guide to Python’s pow() Function first appeared on Finxter.

Posted on Leave a comment

How to Get The Current Reference Count of an Object in Python?

The reference count is the number of times an object is referenced by a variable. If an object isn’t referenced anymore, it can be safely removed from the memory—what’s the use of an object nobody cares for anyway? This article shows you how to count the number of references to a Python object.

What’s the Current Reference Count of Python Object

Question: How to get the current reference count of a Python object?

Get Reference Count Object Python

Answer: You can get the current reference count with the sys module’s getrefcount() method. For example, to get the number of times variable x is referenced, run sys.getrefcount(x).

Here’s the general strategy for an arbitrary object:

import sys
print(sys.getrefcount(object))

The following code snippet shows an example of an object x that has a reference count of 13. After creating a new reference y to the object x, the reference count increases by one to 14.

import sys x = 42 print(sys.getrefcount(x))
# 13 y = x print(sys.getrefcount(x))
# 14

You may not have expected that the initial reference count is 13. So, why is that? Let’s see.

Why is the Reference Count Higher Than Expected?

Reason 1: Implicit Helper Variables

Python is a high-level programming language. It provides you many convenience functions and memory management. You don’t have to care about the concrete memory locations of your objects and Python automatically performs garbage collection—that is—removing unused objects from memory. To do all of this work, Python relies on its own structures. Among those, are helper variables that may refer to an object you’ve created. All of this happens under the hood, so even if you don’t refer an object in your code, Python may still have created many references to that object. That’s why the reference count can be higher than expected.

Reason 2: Temporary Reference in getrefcount()

Note that by calling the function getrefcount(x), you create an additional temporary reference to the object x. This alone increases the reference counter by one.

The post How to Get The Current Reference Count of an Object in Python? first appeared on Finxter.

Posted on Leave a comment

Captive User Interfaces — Why You Should Avoid Them

This tutorial shows you the meaning of captive user interfaces and why they’re discouraged under the Unix philosophy. I’ve written this as a first chapter draft for my upcoming book “From One to Zero” to appear in 2020 with San Francisco-based publisher NoStarch.

What’s a Captive User Interface (CUI)?

A captive user interface is a way of designing a program that requires the user to interact with the program in a session before they’ll be able to proceed with their main execution flow. If you invoke a program in your terminal (Windows, MacOS, or Linux), you must communicate with the program before you can go back to the terminal. Examples are mini programs such as SSH, top, cat, vim—as well as programming language features such as Python’s input() function.

Python Example Captive User Interface

Say you create a simple life expectancy calculator in Python. The user must type in their age and it returns the expected number of years left based on a straightforward heuristic. This is a fun project found here.

"If you’re under 85, your life expectancy is 72 minus 80% of your age.
Otherwise it’s 22 minus 20% of your age."

Your initial, bad, Python code is shown here:

def your_life_expectancy(): age = int(input('how old are you? ')) if age<85: exp_years = 72 - 0.8 * age else: exp_years = 22 - 0.2 * age print(f'People your age have on average {exp_years} years left - use them wisely!') your_life_expectancy()

Listing 8-8: Life-expectancy calculator – a simple heuristic – implemented as a captive user interface.

Here are some runs of the code:

>>> how old are you? 10
People your age have on average 64.0 years left - use them wisely!
>>> how old are you? 20
People your age have on average 56.0 years left - use them wisely!
>>> how old are you? 77
People your age have on average 10.399999999999999 years left - use them wisely!

Interactive Jupyter Notebook to Calculate Your Life Expectancy Using a Captive User Interface

In case you want to try it yourself, I’ve created an interactive Jupyter notebook you can run in your browser to calculate your own life expectancy. But, please, don’t take it too serious! Here’s the notebook:

The code makes use of Python’s input() function that blocks the program execution and waits for user input. Without user input, the code doesn’t do anything. This seriously limits the usability of the code.

What if I wanted to calculate the life expectancy for every age from 1 to 100 based on the heuristic and plot it? I’d have to manually type 100 different ages and store the results in a separate file. Then, you’d have to copy&paste the results into a new script to plot it.

The function really does two things: process the user input and calculate the life expectancy. This already violates rule number 3: Make Every Program Do One Thing Well.

But it also violates our rule: don’t use captive user interfaces if possible.

Non-Captive User Interface Python Example

Here’s how the function could’ve been implemented more cleanly:

def your_life_expectancy(age): if age<85: return 72 - 0.8 * age return 22 - 0.2 * age age = int(input('how old are you? '))
exp_years = your_life_expectancy(age)
print(f'People your age have on average {exp_years} years left - use them wisely!')

Listing: Life-expectancy calculator – a simple heuristic – without captive user interface.

The code is functionally identical to the code with captive user interface. However, it has a big advantage: now, you can use the function in different and unexpected—by the initial developer—ways:

import matplotlib.pyplot as plt def your_life_expectancy(age): '''Returns the expected remaining number of years.''' if age<85: return 72 - 0.8 * age return 22 - 0.2 * age # Plot for first 100 years
plt.plot(range(100), [your_life_expectancy(i) for i in range(100)]) # Style plot
plt.xlabel('Age')
plt.ylabel('No. Years Left')
plt.grid() # Show and save plot
plt.savefig('age_plot.jpg')
plt.savefig('age_plot.pdf')
plt.show()

Listing: Code to plot the life expectancy for years 0-99.

The resulting plot is shown in the following figure:

Figure: How the heuristic works for input years 0-99.

Let’s not talk too much about the flaws of this heuristic—it’s crude by design—but focus on how the rule of avoiding captive user interface has helped us produce this plot. Without the rule, we’d have to write a new function, add redundancies and unnecessary complexity. By considering the rule, we’ve simplified the code and opened up all kinds of future programs to use and built-upon the heuristic. Instead of optimizing for one specific use case, we’ve written the code in a general way that can be used by hundreds of different applications.

The post Captive User Interfaces — Why You Should Avoid Them first appeared on Finxter.

Posted on Leave a comment

Python complex() — A Useless Python Feature?

The Python complex() method returns a complex number object. You can either pass a string argument to convert the string to a complex number, or you provide the real and imaginary parts to create a new complex number from those.

This article shows you how to use Python’s built-in complex() constructor. You’ll not only learn how to use it—but also why it is useless and what you should do instead in newer Python versions.

Usage

Learn by example! Here are some examples of how to use the complex() built-in function:

>>> complex(1, -2)
(1-2j)
>>> complex(2, -1)
(2-1j)
>>> complex(2, 2)
(2+2j)
>>> complex(1)
(1+0j)
>>> complex(2)
(2+0j)
>>> complex('42-21j')
(42-21j)

Syntax Complex()

You can use the complex() method with three different argument lists.

Syntax: 
complex(real) # Imaginary Part is 0j
complex(real, img) # Both real and imaginary part are given
complex(string)  # String has format 'x+yj' for real part x and imaginary part y. 
Arguments real The real part of the complex number
img The imaginary part of the complex number
string A string defining the real and imaginary part in the form 'x+yj' or 'x+yJ' where x and y are integers for the real and imaginary parts.
Return Value complex Returns a complex number.

Video Complex()

Interactive Shell Exercise: Understanding Complex()

Consider the following interactive code:

Exercise: Guess the output before running the code.


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


How to Create Complex Number Without complex()

Interestingly, you don’t need the complex() constructor to create a complex number! Instead, newer version of Python have built-in complex number support—just use the the syntax x+yj for real part x and imaginary part y to obtain a complex number.

a = 1+1j
b = 4+42j
c = 0+0j print('Complex Numbers:')
print(a, b, c) print('Types:')
print(type(a), type(b), type(c))

The output is:

Complex Numbers:
(1+1j) (4+42j) 0j
Types:
<class 'complex'> <class 'complex'> <class 'complex'>

Summary

The Python complex() method returns a complex number object. To create a complex number:

  • Pass a string argument to convert the string to a complex number, or
  • Provide the real and imaginary parts to create a new complex number from those.

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:


Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python complex() — A Useless Python Feature? first appeared on Finxter.