Tag Archives | python

Doxygen 1.8.19 duyuruldu

C++, C, Java, Objective-C, Python, IDL, PHP, C#, Fortran, VHDL, Tcl ve bir dereceye kadar D dilleri için online/offline dokümantasyon hazırlamayı sağlayan bir dokümantasyon sistemi olan Doxygen‘in 1.8.19 sürümü duyuruldu. Deneysel çok iş parçacıklı girdi işleme desteği eklenen yeni sürüm, yüksek çözünürlüklü ekranlar için ölçeklenebilir arama çubuğu ile geliyor. Sqlite3 çıktısını daha iyi kontrol etmek için yapılandırma seçenekleri eklenen sürümde, Cmake’in ctest kullanarak testleri paralel olarak çalıştırması etkinleştirilmiş bulunuyor. Projelere ait dokümantasyon hazırlarken zaman bakımından büyük bir kazanç sağlayan yazılım, Mac OS X ve Linux altında geliştirilmiş, ancak oldukça taşınabilir bir platform olarak ayarlanmıştır. Doxygen ayrıca hepsi otomatik olarak üretilen bağımlılık grafiklerini, kalıtım şemalarını ve işbirliği şemalarını kullanarak çeşitli elemanlar arasındaki ilişkileri görselleştirebilir. Doxygen’i normal belgeler oluşturmak için de kullanabilirsiniz. Doxygen 1.8.19 hakkında ayrıntılı bilgi edinmek için değişiklikler sayfasını inceleyebilirsiniz.

Continue Reading →

Doxygen 1.8.19 edinmek için aşağıdaki linkten yararlanabilirsiniz.

0

Python correlation matrix tutorial

In this blog, we will go through an important descriptive statistic of multi-variable data called the correlation matrix. We will learn how to create, plot, and manipulate correlation matrices in Python. We will be looking at the following topics:
1 What is the correlation matrix?,
1.1 What is the correlation coefficient?
2 Finding the correlation matrix of the given data
3 Plotting the correlation matrix
4 Interpreting the correlation matrix
5 Adding title and labels to the plot
6 Sorting the correlation matrix
7 Selecting negative correlation pairs
8 Selecting strong correlation pairs (magnitude greater than 0.5)
9 Converting a covariance matrix into the correlation matrix
10 Exporting the correlation matrix to an image
11 Conclusion

Continue Reading →

What is the correlation matrix?

A correlation matrix is a tabular data representing the ‘correlations’ between pairs of variables in a given data.

We will construct this correlation matrix by the end of this blog.

Each row and column represents a variable, and each value in this matrix is the correlation coefficient between the variables represented by the corresponding row and column.

The Correlation matrix is an important data analysis metric that is computed to summarize data to understand the relationship between various variables and make decisions accordingly.

It is also an important pre-processing step in Machine Learning pipelines to compute and analyze the correlation matrix where dimensionality reduction is desired on a high-dimension data.

We mentioned how each cell in the correlation matrix is a ‘correlation coefficient‘ between the two variables corresponding to the row and column of the cell.

Let us understand what a correlation coefficient is before we move ahead.

What is the correlation coefficient?

A correlation coefficient is a number that denotes the strength of the relationship between two variables.

There are several types of correlation coefficients, but the most common of them all is the Pearson’s coefficient denoted by the Greek letter ρ (rho).

It is defined as the covariance between two variables divided by the product of the standard deviations of the two variables.

Where the covariance between X and Y COV(X, Y) is further defined as the ‘expected value of the product of the deviations of X and Y from their respective means’.
The formula for covariance would make it clearer.

So the formula for Pearson’s correlation would then become:

The value of ρ lies between -1 and +1.
Values nearing +1 indicate the presence of a strong positive relation between X and Y, whereas those nearing -1 indicate a strong negative relation between X and Y.
Values near to zero mean there is an absence of any relationship between X and Y.

Finding the correlation matrix of the given data

Let us generate random data for two variables and then construct the correlation matrix for them.

import numpy as np

np.random.seed(10)

# generating 10 random values for each of the two variables
X = np.random.randn(10)

Y = np.random.randn(10)

# computing the corrlation matrix
C = np.corrcoef(X,Y)

print(C)

Output:

Since we compute the correlation matrix of 2 variables, its dimensions are 2 x 2.
The value 0.02 indicates there doesn’t exist a relationship between the two variables. This was expected since their values were generated randomly.

In this example, we used NumPy’s `corrcoef` method to generate the correlation matrix.
However, this method has a limitation in that it can compute the correlation matrix between 2 variables only.

Hence, going ahead, we will use pandas DataFrames to store the data and to compute the correlation matrix on them.

Plotting the correlation matrix

For this explanation, we will use a data set that has more than just two features.

We will use the Breast Cancer data, a popular binary classification data used in introductory ML lessons.
We will load this data set from the scikit-learn’s dataset module.
It is returned in the form of NumPy arrays, but we will convert them into Pandas DataFrame.

from sklearn.datasets import load_breast_cancer

import pandas as pd

breast_cancer = load_breast_cancer()

data = breast_cancer.data

features = breast_cancer.feature_names

df = pd.DataFrame(data, columns = features)

print(df.shape)

print(features)

There are 30 features in the data, all of which are listed in the output above.

Our goal is now to determine the relationship between each pair of these columns. We will do so by plotting the correlation matrix.

To keep things simple, we’ll only use the first six columns and plot their correlation matrix.
To plot the matrix, we will use a popular visualization library called seaborn, which is built on top of matplotlib.

import seaborn as sns

import matplotlib.pyplot as plt

# taking all rows but only 6 columns
df_small = df.iloc[:,:6]

correlation_mat = df_small.corr()

sns.heatmap(correlation_mat, annot = True)

plt.show()

Output:

The plot shows a 6 x 6 matrix and color-fills each cell based on the correlation coefficient of the pair representing it.

Pandas DataFrame’s corr() method is used to compute the matrix. By default, it computes the Pearson’s correlation coefficient.
We could also use other methods such as Spearman’s coefficient or Kendall Tau correlation coefficient by passing an appropriate value to the parameter 'method'.

We’ve used seaborn’s heatmap() method to plot the matrix. The parameter ‘annot=True‘ displays the values of the correlation coefficient in each cell.

Let us now understand how to interpret the plotted correlation coefficient matrix.

Interpreting the correlation matrix

Let’s first reproduce the matrix generated in the earlier section and then discuss it.

You must keep the following points in mind with regards to the correlation matrices such as the one shown above:

  1. Each cell in the grid represents the value of the correlation coefficient between two variables.
  2. The value at position (a, b) represents the correlation coefficient between features at row a and column b. This will be equal to the value at position (b, a)
  3. It is a square matrix – each row represents a variable, and all the columns represent the same variables as rows, hence the number of rows = number of columns.
  4. It is a symmetric matrix – this makes sense because the correlation between a,b will be the same as that between b, a.
  5. All diagonal elements are 1. Since diagonal elements represent the correlation of each variable with itself, it will always be equal to 1.
  6. The axes ticks denote the feature each of them represents.
  7. A large positive value (near to 1.0) indicates a strong positive correlation, i.e., if the value of one of the variables increases, the value of the other variable increases as well.
  8. A large negative value (near to -1.0) indicates a strong negative correlation, i.e., the value of one variable decreases with the other’s increasing and vice-versa.
  9. A value near to 0 (both positive or negative) indicates the absence of any correlation between the two variables, and hence those variables are independent of each other.
  10. Each cell in the above matrix is also represented by shades of a color. Here darker shades of the color indicate smaller values while brighter shades correspond to larger values (near to 1).
    This scale is given with the help of a color-bar on the right side of the plot.

Adding title and labels to the plot

We can tweak the generated correlation matrix, just like any other Matplotlib plot. Let us see how we can add a title to the matrix and labels to the axes.

correlation_mat = df_small.corr()

sns.heatmap(correlation_mat, annot = True)

plt.title("Correlation matrix of Breast Cancer data")

plt.xlabel("cell nucleus features")

plt.ylabel("cell nucleus features")

plt.show()

Output:

If we want, we could also change the position of the title to bottom by specifying the y position.

correlation_mat = df_small.corr()

sns.heatmap(correlation_mat, annot = True)

plt.title("Correlation matrix of Breast Cancer data", y=-0.75)

plt.xlabel("cell nucleus features")

plt.ylabel("cell nucleus features")

plt.show()

Output:

Sorting the correlation matrix

If the given data has a large number of features, the correlation matrix can become very big and hence difficult to interpret.

Sometimes we might want to sort the values in the matrix and see the strength of correlation between various feature pairs in an increasing or decreasing order.
Let us see how we can achieve this.

First, we will convert the given matrix into a one-dimensional Series of values.

correlation_mat = df_small.corr()

corr_pairs = correlation_mat.unstack()

print(corr_pairs)

Output:

The unstack method on the Pandas DataFrame returns a Series with MultiIndex.That is, each value in the Series is represented by more than one indices, which in this case are the row and column indices that happen to be the feature names.

Let us now sort these values using the sort_values() method of the Pandas Series.

sorted_pairs = corr_pairs.sort_values(kind="quicksort")

print(sorted_pairs)

Output:

We can see each value is repeated twice in the sorted output. This is because our correlation matrix was a symmetric matrix, and each pair of features occurred twice in it.

Nonetheless, we now have the sorted correlation coefficient values of all pairs of features and can make decisions accordingly.

Selecting negative correlation pairs

We may want to select feature pairs having a particular range of values of the correlation coefficient.
Let’s see how we can choose pairs with a negative correlation from the sorted pairs we generated in the previous section.

negative_pairs = sorted_pairs[sorted_pairs < 0]

print(negative_pairs)

Output:

Selecting strong correlation pairs (magnitude greater than 0.5)

Let us use the same approach to choose strongly related features. That is, we will try to filter out those feature pairs whose correlation coefficient values are greater than 0.5 or less than -0.5.

strong_pairs = sorted_pairs[abs(sorted_pairs) > 0.5]

print(strong_pairs)

Output:

Converting a covariance matrix into the correlation matrix

We have seen the relationship between the covariance and correlation between a pair of variables in the introductory sections of this blog.

Let us understand how we can compute the covariance matrix of a given data in Python and then convert it into a correlation matrix. We’ll compare it with the correlation matrix we had generated using a direct method call.

First of all, Pandas doesn’t provide a method to compute covariance between all pairs of variables, so we’ll use NumPy’s cov() method.
cov = np.cov(df_small.T)

print(cov)

Output:

We’re passing the transpose of the matrix because the method expects a matrix in which each of the features is represented by a row rather than a column.

So we have gotten our numerator right.
Now we need to compute a 6×6 matrix in which the value at i, j is the product of standard deviations of features at positions i and j.

We’ll then divide the covariance matrix by this standard deviations matrix to compute the correlation matrix.

Let us first construct the standard deviations matrix.

#compute standard deviations of each of the 6 features
stds = np.std(df_small, axis = 0) #shape = (6,)

stds_matrix = np.array([[stds[i]*stds[j] for j in range(6)] for i in range(6)])

print("standard deviations matrix of shape:",stds_matrix.shape)

Output:

Now that we have the covariance matrix of shape (6,6) for the 6 features, and the pairwise product of features matrix of shape (6,6), we can divide the two and see if we get the desired resultant correlation matrix.

new_corr = cov/std_matrix

We have stored the new correlation matrix (derived from a covariance matrix) in the variable new_corr.

Let us check if we got it right by plotting the correlation matrix and juxtaposing it with the earlier one generated directly using the Pandas method corr().

plt.figure(figsize=(18,4))

plt.subplot(1,2,1)

sns.heatmap(correlation_mat, annot = True)

plt.title("Earlier correlation matrix (from Pandas)")

plt.xlabel("cell nucleus features")

plt.ylabel("cell nucleus features")

plt.subplot(1,2,2)

sns.heatmap(correlation_mat, annot = True)

plt.title("Newer correlation matrix (from Covariance mat)")

plt.xlabel("cell nucleus features")

plt.ylabel("cell nucleus features")

plt.show()

Output:

We can compare the two matrices and notice that they are identical.

Exporting the correlation matrix to an image

Plotting the correlation matrix in a Python script is not enough. We might want to save it for later use.
We can save the generated plot as an image file on disk using the plt.savefig() method.

correlation_mat = df_small.corr()

sns.heatmap(correlation_mat, annot = True)

plt.title("Correlation matrix of Breast Cancer data")

plt.xlabel("cell nucleus features")

plt.ylabel("cell nucleus features")

plt.savefig("breast_cancer_correlation.png")

After you run this code, you can see an image file with the name ‘breast_cancer_correlation.png’ in the same working directory.

Conclusion

In this tutorial, we learned what a correlation matrix is and how to generate them in Python. We began by focusing on the concept of a correlation matrix and the correlation coefficients.

Then we generated the correlation matrix as a NumPy array and then as a Pandas DataFrame. Next, we learned how to plot the correlation matrix and manipulate the plot labels, title, etc. We also discussed various properties used for interpreting the output correlation matrix.

We also saw how we could perform certain operations on the correlation matrix, such as sorting the matrix, finding negatively correlated pairs, finding strongly correlated pairs, etc.

Then we discussed how we could use a covariance matrix of the data and generate the correlation matrix from it by dividing it with the product of standard deviations of individual features.
Finally, we saw how we could save the generated plot as an image file.

0

SparkyLinux 5.12 duyuruldu

Debian GNU/Linux tabanlı Polonya kökenli dağıtım SparkyLinux‘un 5.12 sürümü duyuruldu. 5 Temmuz 2020 itibariyle Debian kararlı depolarıyla senkronize edilen sistem, 4.19.118 Linux çekirdeği üzerine yapılandırılmış bulunuyor. Hafif LXQT masaüstü ortamına sahip tam özellikli bir sistem sunduğu ifade edilen sürüm ile birlikte, Xfce masaüstü ortamına sahip versiyonu ya da MinimalGUI, MinimalCLI ve ARMHF versiyonları da kullanıma sunuluyor. Debian 10 “Buster” temelinde gelen ve hafif LXDE masaüstü ortamına sahip tam özellikli bir sistem sunulduğu ifade edilirken, minimal ve ARMHF versiyonlarının da kullanıcıların isteklerine yanıt veren hoş çözümler olduğu bildiriliyor. Sistem; Firefox 68.10.0esr, Thunderbird 68.9.0, VLC 3.0.11, LibreOffice 6.1.5 gibi çeşitli güncel paketlerle geliyor. Bu aradada MinimalGUI’de Otter tarayıcı, yerini Epiphany tarayıcısına bırakmış bulunuyor. Zaten Sparky 5.x kullanıyor olanların, tekrar yükleme yapmalarına gerek olmadığı, tam sistem yükseltmesi yaparak yeni sürüme terfi edebilecekleri belirtiliyor. SparkyLinux 5.12 hakkında ayrıntılı bilgi edinmek için sürüm duyurusunu inceleyebilirsiniz.

Continue Reading →

SparkyLinux 5.12 edinmek için aşağıdaki linklerden yararlanabilirsiniz.

0

Django 3.1 beta 1 duyuruldu

Python ile yazılmış özgür, yüksek seviyeli, BSD lisansı ile lisanslanmış yüksek seviyeli ve açık kaynak kodlu bir bir web uygulama çatısı olan Django‘nun 3.1 beta 1 sürümü, Django ekibi tarafından duyuruldu. Django ekibinin Django 3.1 beta 1’i duyurmaktan mutluluk duyduğu ifade ediliyor. 3.1 sürüm döngüsünün ikinci aşamasını temsil eden sürüm, Django 3.1’de gelen değişiklikleri denemek için bir fırsat olarak ifade ediliyor. Artık Django 3.1 için 3.1 sürüm notlarının da okunabileceği belirtilirken, Ağustos ayının ortasında bir sürüm adayı çıkarılmasının planlandığı ifade ediliyor. Güvenli biçimde bir web uygulamaları geliştirme platformu olan Django; her projeye temel kayıt ekleme, silme, düzenleme ve güncelleme fonksiyonlarını bünyesinde barındıran dinamik bir yönetim paneli sağlıyor. Django 3.1 beta 1 hakkında ayrıntılı bilgi edinmek için sürüm duyurusunu inceleyebilirsiniz.

Continue Reading →

Django 3.1 beta 1 edinmek için aşağıdaki linklerden yararlanabilirsiniz.

0

SparkyLinux 2020.06 çıktı

Debian GNU/Linux tabanlı Polonya kökenli dağıtım SparkyLinux‘un 2020.06 sürümü çıktı. Henüz resmi duyurusu yapılmamış olan sürüme ait ISO kalıpları indirilmek üzere yansılarda yerini almış bulunuyor. Henüz resmi duyurusu yapılmamış olduğu için hakkında hemencecik bilgi edinme olanağı olmayan sürümün 5.6.14 Linux çekirdeği üzerine yapılandırılmış olarak geldiğini ve LXQt masaüstü ortamını kullanıma sunduğunu söyleyelim. Sistem; LibreOffice 6.4.4.2, Mozilla Firefox 77.0, Thunderbird 68.8.1 gibi güncel paketlerle kullanıma sunuluyor. UEFI makinelere Sparky kurulum desteği de içeren yeni sürüm, Calamares ve Sparky yükleyicilerle geliyor. SparkyLinux 2020.06 hakkında ayrıntılı bilgi edinmek için en azından şimdilik package-list dosyasını inceleyebilirsiniz. Sparky 2020.06 az önce duyuruldu. Sürüm duyurusunu burada bulabilirsiniz.

Continue Reading →

SparkyLinux 2020.06 edinmek için aşağıdaki linklerden yararlanabilirsiniz.

0

SparkyLinux 2020.05 Special Editions duyuruldu

Debian GNU/Linux tabanlı Polonya kökenli dağıtım SparkyLinux‘un 2020.05 Special Editions sürümü duyuruldu. Öncelikle hafif LXQT masaüstü ortamına sahip tam özellikli bir sistem sunduğu ifade edilen sürümün, Xfce ve MATE masaüstü ortamlarıyla dizayn edilen versiyonları dışında MinimalGUI ve MinimalCLI versiyonları da kullanıma sunulmuş bulunuyor. Bunların dışında GameOver, Multimedia ve Rescue  versiyonları da bulunuyor. Debian “Bullseye” testing sürümüne dayalı olarak gelen yeni sürüm, 11 Mayıs 2020 tarihi itibariyle Debian testing depolarıyla senkronize edilmiş bulunuyor. 5.6.7 Linux çekirdeği üzerine yapılandırılan sistem, Calamares 3.2.23 yükleyici içeriyor. Güvenli Önyükleme özelliğine sahip UEFI makinelerine Sparky kurulumuna ek destek eklendiği söylenirken, tüm iso kalıplarına yeni paketler eklendiği ifade ediliyor. SparkyLinux kullanıcılarının sistemi yeniden yüklemesine gerek olmadığı, sistemin apt yoluyla güncellenebileceği belirtiliyor. SparkyLinux 2020.05 Special Editions hakkında ayrıntılı bilgi edinmek için sürüm duyurusunu inceleyebilirsiniz.

Continue Reading →

SparkyLinux 2020.05 Special Editions edinmek için aşağıdaki linklerden yararlanabilirsiniz.

0

Exiting/Terminating Python scripts (Simple Examples)

Today, we’ll be diving into the topic of exiting/terminating Python scripts! Before we get started, you should have a basic understanding of what Python is and some basic knowledge about its use. You can use the IDE of your choice, but I’ll use  Microsoft’s Linux Subsystem for Windows (WSL) package this time. For more information on that and how you enable it on Windows 10 go here. The way Python executes a code block makes it execute each line in order, checking dependencies to import, reading definitions and classes to store in memory, and executing pieces of code in order allowing for loops and calls back to the defined definitions and classes. When the Python interpreter reaches the end of the file (EOF), it notices that it can’t read any more data from the source, whether that be the user’s input through an IDE or reading from a file. To demonstrate let’s try to get user input and interrupt the interpreter in the middle of execution!

Continue Reading →

Why does Python automatically exit a script when it’s done?

First, from your bash terminal in your PowerShell open a new file called “input.py”

nano input.py

Then paste the following into the shell by right-clicking on the PowerShell window

name=input("Don't type anything!\n")

print("Hi,",name,"!")

Now, press CTRL+X to save and exit the nano window and in your shell type:

python3 input.py
Don't type anything!

And press CTRL+D to terminate the program while it’s waiting for user input

Traceback (most recent call last):

File "input.py", line 1, in

name=input("Don't type anything!")

EOFError

The EOFError exception tells us that the Python interpreter hit the end of file (EOF) condition before it finished executing the code, as the user entered no input data.

When Python reaches the EOF condition at the same time that it has executed all the code it exits without throwing any exceptions which is one way Python may exit “gracefully.”

Detect script exit

If we want to tell when a Python program exits without throwing an exception, we can use the built-in Python atexit module.

Theatexit handles anything we want the program to do when it exits and is typically used to do program clean up before the program process terminates

To experiment with atexit, let’s modify our input.py example to print a message at the program exit. Open the input.py file again and replace the text with this

import atexit

atexit.register(print,"Program exited successfully!")

name=input("What's your name?\n")

print("Hi,",name,"!")

Type your name and when you hit enter you should get:

What's your name?
Example
Hi, Example !
Program exited successfully!

Notice how the exit text appears at the end of the output no matter where we place the atexit call, and how if we replace the atexit call with a simple print(), we get the exit text where the print() all was made, rather than where the code exits.

Program exited successfully!
What's your name?
Example
Hi, Example !

Graceful exit

There are several ways to exit a Python Program that doesn’t involve throwing an exception, the first was going to try is quit()

You can use the bash command echo $? to get the exit code of the Python interpreter.

python3
Python 3.8.2 (default, Apr 1 2020, 15:52:55)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
$ echo $?
0

We can also define the type of code the interpreter should exit with by handing quit() an integer argument less than 256

python3
Python 3.8.2 (default, Apr 1 2020, 15:52:55)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit(101)
$ echo $?
101

exit() has the same functionality as it is an alias for quit()

python3
Python 3.8.2 (default, Apr 1 2020, 15:52:55)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit(101)
$ echo $?
101

Neither quit() nor exit() are considered good practice, as they both require the site module which is meant to be used for interactive interpreters and not in programs. For our programs, we should use something like sys.exit

python3
Python 3.8.2 (default, Apr 1 2020, 15:52:55)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.exit(101)
$ echo $?
101

Notice that we need to explicitly import a module to call exit(), this might seem like its not an improvement but it guarantees that the necessary module is loaded because it’s not a good assumption site will be loaded at runtime. If we don’t want to import extra modules, we can do what exit()quit() and sys.exit() are doing behind the scenes and raise SystemExit

python3
Python 3.8.2 (default, Apr 1 2020, 15:52:55)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> raise SystemExit(101)
$ echo $?
101

Exit with error messages

What if we get bad input from a user? Let’s look back at our input.py script, and add the ability to handle bad input from the user (CTRL+D to pass an EOF character)

nano input.py
try:

name=input("What's your name?\n")

print("Hi, "+name+"!")

except EOFError:

print("EOFError: You didn't enter anything!")

$ python3 input.py
What's your name?
EOFError: You didn't enter anything!

The try statement tells Python to try the code inside the statement and to pass any exception to the except statement before exiting.

Exiting without error

What if the user hands your program an error but you don’t want your code to print an error, or to do some sort of error handling to minimize user impact?

We can add a finally statement that lets us execute code after we do our error handling in catch

nano input.py
try:

name=input("What's your name?\n")

if(name==''):

print("Name cannot be blank!")

except EOFError:

#print("EOFError: You didn't enter anything!")
name="Blankname"

finally:

print("Hi, "+name+"!")

$ python3 input.py
What's your name?
Hi, Blankname!

Notice the user would never know an EOFError occurred, this can be used to pass default values in the event of poor input or arguments.

Exit and release your resources

Generally, Python releases all the resources you’ve called in your program automatically when it exits, but for certain processes, it’s good practice to encase some limited resources in a with block.

Often you’ll see this in open() calls, where failing to properly release the file could cause problems with reading or writing to the file later.

nano openfile.py
with open("testfile.txt","w") as file:

file.write("let's write some text!\n")

$ python3 openfile.py
$ cat testfile.txt
let's write some text!

The with block automatically releases all resources requisitioned within it. If we wanted to more explicitly ensure the file was closed, we can use the atexit.register() command to call close()

$ nano openfile.py
import atexit

file=open("testfile.txt","w")

file.write("let's write some text!\n")

atexit.register(file.close)

If resources are called without using a with block, make sure to explicitly release them in an atexit command.

Exit after a time

If we are worried our program might never terminate normally, then we can use Python’s multiprocessing module to ensure our program will terminate.

$ nano waiting.py
import time

import sys

from multiprocessing import Process

integer=sys.argv[1]

init=map(int, integer.strip('[]'))

num=list(init)[0]

def exclaim(int):

time.sleep(int)

print("You were very patient!")

if __name__ == '__main__':

program = Process(target=exclaim, args=(num,))

program.start()

program.join(timeout=5)

program.terminate()

$ python3 waiting.py 7
$ python3 waiting.py 0
You were very patient!

Notice how the process failed to complete when the function was told to wait for 7 seconds but completed and printed what it was supposed to when it was told to wait 0 seconds!

Exiting using a return statement

If we have a section of code we want to use to terminate the whole program, instead of letting the break statement continue code outside the loop, we can use the return sys.exit() to exit the code completely.

$ nano break.py
import time

import sys

def stop(isTrue):

for a in range(0,1):

if isTrue:

break

else:

print("You didn't want to break!")

return sys.exit()

mybool = False

stop(mybool)

print("You used break!")

Exit in the middle of a function

If we don’t want to use a return statement, we can still call the sys.exit() to close our program and provide a return in another branch. Let’s use our code from break.py again.

$ nano break.py
import time

import sys

def stop(isTrue):

for a in range(0,1):

if isTrue:

word="bird"

break

else:

print("You didn't want to break!")

sys.exit()

mybool = False

print(stop(mybool))

Exit when conditions are met

If we have a loop in our Python code and we want to make sure the code can exit if it encounters a problem, we can use a flag that it can check to terminate the program.

$ nano break.py
import time

import sys

myflag=False

def stop(val):

global myflag

while 1==1:

val=val+1

print(val)

if val%5==0:

myflag=True

if val%7==0:

myflag=True

if myflag:

sys.exit()

stop(1)

$ python3 break.py
2
3
4
5

Exit on keypress

If we want to hold our program open in the console till we press a key, we can use an unbound input() to close it.

$ nano holdopen.py
input("Press enter to continue")
$ python3 holdopen.py
Press enter to continue
$

We can also pass CTRL+C to the console to give Python a KeyboardInterrupt character. We can even handle the KeyboardInterrupt exception like we’ve handled exceptions before.

$ nano wait.py
import time

try:

i=0

while 1==1:

i=i+1

print(i)

time.sleep(1)

except KeyboardInterrupt:

print("\nWhoops I took too long")

raise SystemExit

$ python3 wait.py
1
2
3
^C

Whoops I took too long

Exit a multithreaded program

Exiting a multithreaded program is slightly more involved, as a simple sys.exit() is called from the thread that will only exit the current thread. The “dirty” way to do it is to use os._exit()

$ nano threads.py
import threading

import os

import sys

import time

integer=sys.argv[1]

init=map(int, integer.strip('[]'))

num=list(init)[0]

def exclaim(int):

time.sleep(int)

os._exit(1)

print("You were very patient!")

if __name__ == '__main__':

program = threading.Thread(target=exclaim, args=(num,))

program.start()

program.join()

print("This should print before the main thread terminates!")

$ python3 threads.py 6
$

As you can see, the program didn’t print the rest of the program before it exited, this is why os._exit() is typically reserved for a last resort, and calling either Thread.join() from the main thread is the preferred method for ending a multithreaded program.

$ nano threads.py

import threading

import os

import sys

import time

import atexit

integer=sys.argv[1]

init=map(int, integer.strip('[]'))

num=list(init)[0]

atexit.register(print,"Threads exited successfully!")

def exclaim(int):

time.sleep(int)

print("You were very patient!")

if __name__ == '__main__':

program = threading.Thread(target=exclaim, args=(num,))

program.start()

program.join()

$ python3 threads.py 6
You were very patient!
Threads exited successfully!

End without sys exit

Sys.exit() is only one of several ways we can exit our python programs, what sys.exit() does is raise SystemExit, so we can just as easily use any built-in Python exception or create one of our own!

$ nano myexception.py
class MyException(Exception):

pass

try:

raise MyException()

except MyException:

print("The exception works!")

$ python3 myexception.py
The exception works!

We can also use the os._exit() to tell the host system to kill the python process, although this doesn’t do atexit cleanup.

$ python3
Python 3.8.2 (default, Apr 1 2020, 15:52:55)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os._exit(1)

Exit upon exception

If we want to exit on any exception without any handling, we can use our try-except block to execute os._exit().

Note: this will also catch any sys.exit()quit()exit(), or raise SystemExit calls, as they all generate a SystemExit exception.

$ python3
Python 3.8.2 (default, Apr 1 2020, 15:52:55)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> try:
... quit()
... except:
... os._exit(1)
...
$ echo $?
1

Exit and restart

Finally, we’ll explore what we do to exit Python and restart the program, which is useful in many cases.

$ nano restart.py
import atexit

import os

atexit.register(os.system,"python3 restart.py")

try:

n=0

while 1==1:

n=n+1

if n%5==0:

raise SystemExit
except:

print("Exception raised!")

$ python3 restart.py
Exception raised!
Exception raised!
...
Exception raised!
^Z
[3]+ Stopped python3 restart.py

I hope you find the tutorial useful. Keep coming back.

Thank you.

0