Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 3

128 bytes added, 03:36, 30 May 2017
PART 2 - Providing Functions With Arguments
20
</source>
:::4. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3b
</source>
:::5. Before proceeding, make certain that you identify any and all errors in lab3b.py. When the check script tells you everything is ok before proceeding to the next step.
'''Multiple Arguments and IF Statements'''
Error: function operator can be "add", "subtract", or "multiply"
</source>
:::5. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3c
</source>
:::6. Before proceeding, make certain that you identify any and all errors in lab3c.py. When the check script tells you everything is ok before proceeding to the next step.
== PART 3 - Running System Commands with Subprocess ==
'''Perform the Following Steps:'''
:#Start the ipython3 shell:<source>
import subprocess
dir(subprocess)
</source>
 :#There are many available modules and attributes available as part of subprocess, we are interested in "'''Popen'''". This method subprocess.Popen() can be used to run system commands as a child process to the Python script. This below output will create a new child process, in Python we can control this through the new Python object we just created, "'''p'''". "'''p'''" now has a collection of methods(functions that are apart of a object) available, view them with '''dir()'''.<source>
p = subprocess.Popen(['date'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
dir(p)
</source>
 :#This next step is going to communicate with the process and get the stdout and stderr from the command we previously.<source>
stdout, stderr = p.communicate()
stdout
stdout
</source>
:#While many of these system commands could be instead written in simply Python, the exercise of running system commands is important.
While many of these system commands could be instead written in simply '''Practice Running System Commands From Python, the exercise of running system commands is important.''':'''lab3dPerform the Following Instructions:''':#Create the "'''~/ops435/lab3/lab3d.py'''" script. The purpose of this script is to create a Python function that can return the linux system's root directory free space. :::*The script should have a '''Shebang line''':::*The script should import subprocess:::*The script should use the linux command "df -h | grep '/$' | awk '{print $4}'" :::*The script should contain the function free_space():::*The function free_space() should return a string which is in utf-8 and has newline characters stript  :::*Note: your output may be completely different, the free/available disk space on every computers root directory may be different. :::2. Sample Run 1: <source>
run lab3d.py
9.6G
</source>
 :::3. Sample Import 1:<source>
import lab3d
'9.6G'
</source>
 :::4. Exit the ipython3 shell, download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3d
</source>
:::5. Before proceeding, make certain that you identify any and all errors in lab3d.py. When the check script tells you everything is ok before proceeding to the next step. 
= INVESTIGATION 2 - LISTS =
198
edits