11. Python scripting in Linux

11.1. Python programming environment: Jupyter notebook (Exercise)

Install Jupyter notebook on your desktop:

pip3 install notebook

Add the PATH environment variable in the bottom of .bashrc file in your home directory:

.bashrc

export PATH=$PATH:.local/bin

Source the .bashrc by running command:

source .bashrc

Assign the password to jupyter notes by running command:

jupyter notebook password

Start jupyter notes by command below:

jupyter notebook &

Access jupyter notebook through the browser on your virtual desktop by navigating to URL below:

jupyter

http://localhost:8888

Start a new python3 project in the jupyter notebook.


11.2. numpy arrays (Exercise)

In your Jupyter notebook, copy and paste the python code from the cell below:

#numpy arrays
import numpy as np
data = np.array([1.0, 2.0, 5.6, 9.8, 3.0])

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Try executing the following functions:

data.max()
data.min()
data.sum()

11.3. Graphics with matplotlib (Exercise)

In your Jupyter notebook, copy and paste the python code from the cell below:


#plot with matplotlib
import matplotlib.pyplot as plt

plt.plot(data)

Press <shift>-<Enter> keys to execute the script in the jupyter cell.


11.4. Matrix multiplication (Exercise)

In your Jupyter notebook, copy and paste the python code from the cell below:

#Matrix multiplication:
import numpy as np

A = np.array(([1, 2, 3], [4, 5, 6]))
B = np.array(([7, 8, 1, 5], [9, 10, 0, 9], [11, 12, 5, 5]))

print(np.dot(A, B)) 

Press <shift>-<Enter> keys to execute the script in the jupyter cell.


11.5. Matrix transposition (Exercise)

In your Jupyter notebook, copy and paste the python code from the cell below:

#Matrix transposition
import numpy as np
a = np.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]])
print(a.T) 

Press <shift>-<Enter> keys to execute the script in the jupyter cell.


11.6. Interaction with operating system (Exercise)

In your Jupyter notebook, copy and paste the python code from the cell below:

#Current working directory
    import os
    os.getcwd()

Try the following in the new cell:

#Change the directory
    os.chdir("/etc")
    os.getcwd()

Try listing the files and directories in /etc:

# see the list of files and directories
    os.listdir()

11.7. Calling Linux commands from Python (Exercise)

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Example of class call

# Create directory DIR1 and check the disk usage in the directory
    import subprocess
    s=subprocess.call("mkdir DIR1; du -sh DIR1", shell=True)

For full control of the standard input and the standart output to the command, we use class Popen:

    import subprocess
    s=subprocess.Popen("du -sh /etc", shell=True, 
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,)
    stdout_value,stderr_value = s.communicate()

    print("std output=", stdout_value) 
    print("std error=", stderr_value) 

The stdout_value content now can be analyzed within python. For example:

# Read /etc/passwd file and print out the 5th field
    import subprocess

    cmd="cat /etc/passwd"
    s=subprocess.Popen(cmd, shell=True,universal_newlines=True,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,)
    stdout_value,stderr_value = s.communicate()

    for i in stdout_value.strip().split('\n'):
        [uname, pwd, uid, gid, name, hd, shell ] =  i.split(':')
        print(name)

11.8. Scripts runnable from command line (Exercise)

The above scripts can be placed in a file, for example call.py:

#!/usr/pin/python3

# see the list of files and directories
    import os
    os.listdir()

Make the file executable:

chmod a+x call.py

Run the script:

./call.py

11.9. References

numpy tutorial