1,760
edits
Changes
no edit summary
<font color= Lab Preparation 'red'>'''** DO NOT USE - TO BE UPDATED FOR CENTOS 8.0 **'''</font>= LAB OBJECTIVES =
= INVESTIGATION 1: INSTALLING LINUXVM=
=== Part PART 1 - Choosing Installing Your Linux Distribution ===
<!-- Try to use tables and simplify the layers of WIKI to make top links less verbose to students -->
:Centos 7 - release 1810 will be used for this course. The purpose for this is to allow you to use a consistent and stable '''RedHat''' based OS, and to lower the amount of new linux distros that you need to learn.
<!--==== Environment Setup ====
:'''Perform the following steps:''' :#Regardless of the Linux distribution that is used for this course, these lab will be using and referring to the current version of '''CentOS 7''' (Graphical Desktop) for our main Linux machine. When creating your VM, refer to the '''Details / Minimum Requirements''' section above for assistance. :#Install CentOS 7 VM selecting the default partitions. When you have finished the installation of CentOS 7, you may proceed to the next step.<blockquote style="margin-left:35px;">{{Admon/important|style="padding-left:25px"|Unit Feedback Scripts|Each '''Part''' (within an 'Investigation) is referred to as a '''Unit'''. Each Unit will require that the student download and run a '''Unit Feedback Script''', which provides the OPS435 student "real-time feedback" of their completed work.<br>This feedback is not considered to be perfect or fool-proof; however, it may provide feedback (hints) in case a student gets stuck or experiences an error when performing administration tasks or when creating their Python scripts. These unit feedback scripts can also be used to confirm that the student's Python script is on the right track, and provide a consistent record of their Python scripting progress throughout their labs.}}</blockquote>
cd ~/ops435/lab1/
pwd # <-- i.e. confirm that you are in the right correct directoryls CheckLab1.py || wget matrixhttps://raw.senecacollegegithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab1.py
python3 ./CheckLab1.py -f -v lab0a
</source></li>#<li style="margin-left:25px;">Before moving on to the next step make sure you identify any and all errors in "lab1a.py"from the check script. <br>When the check script tells you everything is "ok" for "lab0a", you may proceed to the next step.<br><br></li></ol>-->
=== Part PART 2 - Setting Up up Your Linux Python Environment For OPS435 Labs===
In order to learn how to use python on your Linux machine, it is important to setup your Linux environment and learn how to interact with a Python shell. The following should be done on a terminal as the superuser (i.e. root).
:'''Perform the following steps:'''
:#The first step is to update your entire system. Fedora uses a new package manager: it is based on yum, yet it contains newer code and has more maintainer features as also contains improved features. It should work very similarly to the yum command, but check the man pages if you get confused:<sourcelang="bash">dnf yum update</source>#Lets start installing applications we need, first Python version 3 and version 2:<source>dnf install python3 python2 # Install python3.5 and python2.7</source>#Next, you will install a couple of useful applications called '''tmux''' and '''screen'''. They are referred to as '''terminal multiplexers'''. If you plan to spend a lot of time in the terminal, this powerful tool will help you get it done. Lets install it and plan to use it later:<source>dnf install screen tmux && ln -s /usr/bin/true /etc/sysconfig/bash-prompt-screen
</source>
:# You will now set your hostname to the Linux Distribution we are usingInstall extra packages for enterprise linux:<sourcelang="bash">hostnamectl set-hostname fedora24 # Set hostname to distributionyum install epel-namerelease
</source>
:#Installing vim(Vi IMproved) will give us syntax highlighting Next install applications that we required, first Python version 3 and allow for advanced customization for terminal editingversion 2:<sourcelang="bash">dnf yum install vimpython34 python34-common vimdevel # Install python3.4 and python3.4 development librariesyum install python python2-enhanced devel # Install vim python2.7 and python2.7 development libraries
</source>
:#Python pip is We now need to create a package manager specifically for Pythonlink to python3. While it is usually not recommended to install software outside of dnf or yum, sometimes the only way to get a specific or latest version will be through pip4:<sourcelang="bash">dnf install pythoncd /binln -pip s python3-pip # Install pip</source><br>In the dictionary, "git" is defined as an unpleasant or contemptible person. In the IT inductory, on the other hand, '''Git''' refers to a version control system that allows you to track any changes made to files and programs. The benefit to using git is primarily found when it's used with multiple people, sharing and working on code together. While that is not how we will be using it in this course, you may find some benefits in using it for managing multiple versions of the same program or for backing up your code onto the internet. Check out bitbucket for a free private code repository.<br><br>#Issue the following command to install git:<source>dnf install git # Install git command line tool4 python3
</source>
:#IPython Next, you will be one install a couple of useful applications called '''tmux''' and '''screen'''. They are referred to as '''terminal multiplexers'''. If you plan to spend a lot of time in the tools we terminal, this powerful tool will use the mosthelp you get it done. Lets install it. You will learn more about and plan to use it in the next sectionlater:<sourcelang="bash">dnf yum install pythonscreen tmux && ln -s /usr/bin/true /etc/sysconfig/bash-ipython python3prompt-ipython # Install enhanced interactive pythonscreen
</source>
:#Issue Set your hostname to the following commands Linux Distribution we are using in your Ipython shell to check your work for this sectioncase you did not set it correctly during the install process:<sourcelang="bash">cd ~/ops435/lab1/pwd hostnamectl set-hostname centos7 #confirm that you are in the right directoryls CheckLab1.py || wget matrix.senecacollege.ca/~acoatley-willis/CheckLab1.pypython3 ./CheckLab1.py -f Set your hostname to distribution-v lab0bname
</source>
::You will be required to use a text editor in order to create and modify your Python scripts. There are many text editors that provide various features to become more comfortable and productive during your Python coding sessions.
::Below is a listing of several common text editors and their features.
<blockquote style="margin-left:35px;">{{Admon/tip|style="padding-left:25px"|Selecting an Appropriate Text Editor|
'''Geany''' (recommended text editor for labs)<br>A simple graphical text editor for developers. It is available in the CentOS package repositories.<br>
'''Vim'''<br>vim is a powerful text editor for system administration and programming tasks. All of the shortcuts and commands you've learned over the years will help you edit programs efficiently.<br>
'''Sublime'''<br>Another powerful and popular text editor designed for programming. Is not free or open source.}}</blockquote>
<table border="1" cellspacingol><li value=6"0" cellpaddingstyle="5" width="40%" ><tr><td width="150pxmargin-left:25px;">'''Text EditorPython pip'''</td><td>is a package manager specifically for Python. While it is usually not recommended to install software outside of '''Characteristicsdnf'''</td></tr> <tr><td valign="top">or '''Vimyum'''</td><td valign="top">As a system administrator you have probably spent a ton of time inside vim. Well vim is just as powerful and useful when you get to programming, all sometimes the shortcuts and commands you've learned over the years will help you edit programs efficiently. On top of what you already know, it might be time only way to customize vim get a little more for programming. Vim can actually specific or latest version will be modified to become a full programming environment with all the features you yould expect.</td>through pip:</tr><tr><td valignsource lang="topbash">'''Atom'''</td><td valign="top">"A hackable text editor for the 21st Century"yum install python-pip # Install python2. This text editor is a powerful tools that comes with everything your need right out of the box. Atom allows for deep customization from everything from complete functionality changes to just changing the theme. Definitely worth checking out, especially for python development7 pipyum install python34-pip # Install python3.4 pip</tdsource></trli> <tr><td valignli style="topmargin-left:25px;">'''Sublime'''Upgrade python34-pip with the following command:</td><td valignsource lang="topbash">Sublime is a popular text editor with tons of customizations and themespip3.4 install --upgrade pip</tdsource></trli><tr><td valignli style="topmargin-left:25px;">Issue the following command to install '''Other Editorsgit''':</td><td valignsource lang="topbash">xyum install git # Install git command line tool</tdsource></trli></tableol>
<blockquote style== Part 3 "margin- Lab left:35px;">{{Admon/tip|What is a Git?| '''Git''' refers to a '''version control system''' that allows you to track any changes made to files and programs. Our primary use for git in this course will be for backup onto to the internet. Check lab0c ==out '''bitbucket''' for a free private code repository.}}</blockquote>
<preol><li value="8" style="margin-left:25px;">IPython will be one of the tools we will use the most. Lets install it. You will learn more about it in the next section:<source lang="bash">yum install python-ipython # Install ipython for python2.7pip3.4 install ipython # Install ipython for python3.4</source></li><li style="margin-left:25px;">Exit from the superuser shell to your regular user shell and issue the following commands to check your work for this section:<source lang="bash">mkdir -p ~/ops435/lab1/
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory
ls CheckLab1.py || wget matrixhttps://raw.senecacollegegithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab1.pypython3 ./CheckLab1.py-f -v lab0apython3 ./CheckLab1.py -f -v lab0clab0b</presource></li> <li style="margin-left:25px;">Before moving on to the next step , make sure you identify and correct any and all errors in "lab1a.pylab0b"output. When the check script tells you everything is "ok", you may procede proceed to the next step.</li></ol><br><br>
= Investigation INVESTIGATION 2 - IPython : USING THE "IPYTHON" CLI =
:'''IPython ''' is an interactive environment that allows us to run python code line by line as we write it. This will function almost exactly also act like a bash shell prompt, allowing users to enter a command and recieve the output backlimited number of Bash shell commands. However the You will notice this by seeing an [IN] prompt where to enter commands and an [OUT] prompt that we will be running are lines of python codedisplay output from issued commands. Using this method we We will start creating scripts out of the code we build within this '''IPython''' environment.<blockquote style="margin-left:35px;">{{Admon/important|style="padding-left:25px"|ipython and python|[https://plot.ly/python/ipython-vs-python/ What is the difference between '''python''' and '''ipython'''?] Please note that in later labs, you can use either '''python''' or '''ipythone''' as the IPython environmentinteractive python shell.}}</blockquote>
== Part = PART 1 - Using IPython Common Ipython Commands and Features ===
==== Using Magic Functions ====
:Lets start with trying to run begin by running some python code in a the ipython interactive shell. This is a advanced python shell, similar to the bash shell that you have been using throughout the linux Linux courses. Throughout these labs the term command can also refer to these "magical functions".
:#Make sure you're using your regular user login and not using the root user for the following section.:#To access the '''ipython''' shell, issue the following command:<presource>
ipython3
</presource>Now we are now inside the IPython environment. We can run some basic bash commands within this shell by issuing IPython magic functions. <br>:#Lets try a few commands out now:<source> ls pwd cd ~/ ls</source>'''Remember:''' You are not using Python here, instead, these are aliases for Bash shell commands that IPython gives you access to. What you are actually using is bash, but not all bash commands are available in the IPython environment.<br><br>:#Lets find out which ones are available, type the following command into the IPython shell:<source> alias</source>We should now have a list of all the bash commands available in IPython.:#Lets setup a directory structures for completing and organizing labs. These should be the locations to store your lab scripts.<source> mkdir ~/ops435/lab2 mkdir ~/ops435/lab3 mkdir ~/ops435/lab4 mkdir ~/ops435/lab5 mkdir ~/ops435/lab6 mkdir ~/ops435/lab7 mkdir ~/ops435/lab8</source>:#If you are interested in finding more information about other magic functions in IPython, then within the IPython shell, and enter the following (press the letter '''q''' to exit this function):<source> magic</source>
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory
ls CheckLab1.py || wget matrixhttps://raw.senecacollegegithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab1.pypython3 ./CheckLab1.py -f -v lab1alab0c</presource></ol><br><br>
:In this investigation we will start writing our very first python scripts. These will be very basic and help us practice syntax and foundation skills, such as: outputting text to the screen, storing data inside objects, and using math operators.
<blockquote style= Investigation 3 "margin- Writing left:35px;">{{Admon/important|style="padding-left:25px"|Object and Variable|The terms '''object''' and '''variable''' are used to refer to a data item in computer programming. The term '''variable''' are mostly used in 3rd generation programming/scripting languages (e.g. bash) while the term '''object''' is preferred in object oriented programming/scripting languages like Python. In the context of Python Script =programming/scripting, When you see the term '''variable''' in the lab instructions, it can be replaced by the term '''object'''.}}</blockquote>
== Part = PART 1 - Printing Common Introductory Python Functions ===
:#Create a new python file in your ~/ops435/lab1 directory. Call it lab1a.py. The first Python code we will write is going to call the print function. A function is code that has been defined in another location. Functions can take arguments, use these arguments in some way, and then usually return a result. The first function we will use is the "print()" functions, it's sole purpose is to output information to the screen.<prebr><br>:#Add the following line into your source code file:<source>
print()
</presource>And run it from the command-line: <source>python3 ./lab1a.py</source> You will notice that nothing happened when is printed even though we ran this called the "print()" function. This is because we didn't pass any arguments to it, lets try again.<br><br> :# Modify your call to print() to inlcude an argument ('hello world'):<br><presource>
print('hello world')
</presource> This time we should now see that the python function "print()" has outputted to the screen the words 'hello world'. In python a word or a bunch of characters like 'hello world' is called a 'string'. So what we did In the above isexample, passed a '''string''' was passed as a '''argument''' to the print '''function'''. These words are important for understanding and talking about different aspects of code.<br><br> == Part 2 - Hello World == Next, we will make our first script with :# Note that there are similarities between the above Python print() function. Open and the Bash echo command, but Python is more picky than bash (which is a new text file called "lab1agood thing).py"Try to run print without the brackets or without the quotes to see what happens. <preblockquote style="margin-left:35px;">%vim ~{{Admon/ops435/lab1/lab1atip|Reading errors|One of the things that makes a good programmer is debugging skills. The first and most important debugging technique is reading and understanding error messages. Try to understand what the errors are saying even if you think you already know what the problem is and already have some idea about how to fix it.py}}</preblockquote> <ol><li value="5" style="margin-left:25px;">Write the following code into our python file. Note the she-bang line at the top of the file to run this script in the python3 environment. You will need to add this she-bang line for all python scripts you create for this course.<presource lang="python">
#!/usr/bin/env python3
print('Hello world')
</presource><li style="margin-left:25px;">Another way of running a pythion program is executing it directly, e.g.: <source>./test.py</source>Note that the file will need execute permissions even though you ran it just fine earlier. Why is that? <li style="margin-left:25px;">Download the check script and check your work. Enter the following commands from the bash shell.<source lang="bash"> cd ~/ops435/lab1/ pwd #confirm that you are in the right directory ls lab1a.py #confirm that you have the lab1a.py script in your directory ls CheckLab1.py || wget https://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab1.py python3 ./CheckLab1.py -f -v lab1a</source><li style="margin-left:25px;">Before moving on to the next step make sure you identify any and all errors in "lab1a.py". When the check script tells you everything is "ok", you may proceed to the next step.</ol>
:In Python, an object is used to store data for use later in the program. This data can be a string, integer, decimal number, characters, etc. We will only be covering '''string''' and '''integer''' variables in this lab. You will learn and use other python object types in future labs.
print(name)
</presource>:#Think about why this does something different:<source lang="python">print('name')</source>:#Now lets try something new, we are going to print out the string and concatenate/combine it with another string. The plus sign can be used to join 2 strings together. However, make sure that your variable object is always outside the quotes, or it will not resolve to a value. <presource lang="python">
print('I have a friend named ' + name)
</presource> === Part 3 - Evaluation === Create a :#To gain practice, complete your python scriptwith the following content and details: lab1b.py::::* The script should have a '''Shebang line''' like you did for your lab1a.py python script::::* The script should use a single variable object called "name"::::* The value of the "name" variable object should be "Isaac"::::* The script, when executed, should print out "How old are you Isaac?" Example ::::*Sample run: <presource>% cd ~/ops435/lab1/%run ./lab1b.py How old are you Isaac?</presource> Try the check checking script as you are working through a script to sometimes get hints.<br><br> <ol><li value="7" style== Part 3 "margin- Lab Check lab1b === This course is designed with a unit testing suite, which can be used to look at the scripts you write left:25px;">Download and give real-time feedback. This feedback is not perfect, however it may offer some hints if you get stuck with a error. It can also be used to make sure you are on run the write track, and show progress. Download the check checking script. Enter the following commands from the '''bash shell.''':<presource lang="bash">
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory
ls CheckLab1.py || wget matrixhttps://raw.senecacollegegithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab1.py
python3 ./CheckLab1.py -f -v lab1b
</presource></li><li style="margin-left:25px;"> Before proceeding, make certain that you identify any and all errors in "lab1b.py". When the check script tells you everything is "ok", you may proceed to the next step.</li></ol>
:#Create a python script (called lab1c.py) and first - start with a few simple things to try: :#Lets create some new variables objects to play with.<presource lang="python">
num1 = 5
num2 = 10
</presource> In IPython we :#You can inspect these variables by just typing the name of print the variable. But values in a those integer objects:<source lang="python script this will not provide any output. This feature is useful however for debugging. <pre">print(num1)print(num2)</presource> :#Now we will make a new variable integer object and try some math. :<presource lang="python">
sum = num1 + num2
</presource> This will add the values contained in the variables integer objects together, providing a sum. However you will note that there is no output. First lets Let's inspect the new value. :<pre>sum</pre> Does this value look right? If we wanted to print this out to the screen we could use the following source lang="python code. <pre">
print(sum)
</presource>Does this value look right? Are you sure?<br> :#Now lets try printing this sum out with a string.:<presource lang="python">
print('The sum is: ' + sum)
</presource> What happened? Did you receive an error? This will may have been the first time you've seen this error, but it won't be the last. What we tried to do is combine a string with a number, and this won't work. <br><br>In order to use display this number as a string we will use the "str()" function on it. The "str()" function will return a string of your number and provide it as a argument to "print()". This function will not change the value of your variableobject, your variable object is still a an intergerobject.<br><br> :# Issue the following:<presource lang="python">
print('The sum is: ' + str(sum))
</presource>What did you notice this time?<br /> === Part 4 - Evaluation === Create a :#To gain practice, complete your python scriptwith the following features: lab1c.py:::* The script should have a Shebang line.:::* The script should have a variable an object called '''name''':::* The script should have a variable an object called '''age''':::* The value of the '''name''' variable object should be '''Isaac''':::* The variable object '''age''' should contain a integer:::* The value of the '''age''' variable object should be '''72''':::* The script, when executed, should print out "Isaac is 72 years old!" :::Example run: <presource>%cd ~/ops435/lab1/%run ./lab1c.py
Isaac is 72 years old!
</presource><br> Try the check script as you are working through a script to sometimes get hints.<br> =<ol><li value=10" style= Part 4 "margin- Lab Check lab1c === This course is designed with a unit testing suite, which can be used to look at the scripts you write left:25px;">Download and give real-time feedback. This feedback is not perfect, however it may offer some hints if you get stuck with a error. It can also be used to make sure you are on run the write track, and show progress. Download the check checking script. Enter the following commands from the bash shell.:<presource lang="bash">
cd ~/ops435/lab1/
pwd #confirm that you are in the right directory
ls CheckLab1.py || wget matrixhttps://raw.senecacollegegithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab1.py
python3 ./CheckLab1.py -f -v lab1c
</presource></li><li style="margin-left:25px;">Before moving on to the next step make sure you identify any and all errors in "lab1c.py". When the check script tells you everything is "ok", you may proceed to the next step.</li></ol>
:In the previous section, you performed a couple of simple mathematical operations. In this section, you will learn some additional mathematical operations.
= LAB 1 SIGN-OFF (SHOW INSTRUCTOR) =
[[Image:lab1_signoff.png|thumb|right|450px|Students should be prepared with '''all required commands (system information) displayed in a terminal (or multiple terminals) prior to calling the instructor for signoff'''.]]
::<span style=== Part 5 color:green;font- Lab Check lab1d ===size:1.5em;">✓</span> Submit your output and Python scrips via Blackboard instead.