Lab 1
Semester 2, 2019: Lab 1
S2 2018
This lab has two parts. Part A is intended to help you familiarise yourself with the computing environment found on the CSIT lab computers and the labs in the Hanna Neumann building which you will be using throughout the course. If something unexpected happens, or if you get stuck, you can ask your tutors, or another student. Part B introduces your first programming exercises.
Both parts should be completed during semester week 2. It is not expected that you will be able to finish all the programming exercises during the scheduled 2 hour lab session. The normal workload for a 6unit course is a bit over 10 hours per week. That means students in this course are expected to spend at least 56 hours per week practicing programming in addition to the scheduled lecture and lab times.
You should have 247 access to some of the CSIT labs N112, N113 and N114 just use your student card to swipe in so that you can work there whenever there is no scheduled activity usually after 7pm on weekdays, and on weekends. Sometimes lab rooms will not be full even when there is a scheduled class, in which case you can often take a spare place. However, you should always check with the teacher in the ongoing class that it is ok, and you should of course not disturb students in that class. The teacher always has the right to ask you to leave a computer lab room when there is a scheduled class that you are not enrolled in, regardless of whether it is full or not.
Anaconda python is also installed on the ANU InfoCommons computers. These are the Windows and Mac computers found in, for example, libraries and lecture theatres across the campus. This means you can work on programming exercises also on those machines.
Part A
For these exercises, you will need to be sitting in front of a computer in the CSIT labs.
The main objective of this part is to ensure that you have a working computer account, are able to use some of the utilities of the CSIT student computing environment, and can run basic python programs on this system. Part B consists of a set of programming exercises. From next week on, we will focus solely on python programming.
1
Background
The computers in the CSIT labs are running the GNU Linux operating system. If you have never used a Linux system before, dont panic! The basic concepts you will be working with files, directories, applications, etc are similar to those in other operating systems such as Microsoft Windows and Mac OS X. In fact, if youve used OS X, you have used a system that is very similar to Linux behind the scenes. There is a bit of a learning curve, but it shouldnt be long until you feel comfortable.
You do need to be aware that the CSIT computing environment is not the same as that found on other computers at the ANU. You should be able to access your ANUwide home drive file storage look for an application called ANU Home Drive:
Figure 1: ANU Home Drive icon
Files that you store here will be accessible from other computers on campus, such as the InfoCommons Windows and Mac computers. Other ways you can move your files around is by copying them onto an external memory drive e.g. USB stick, through an online service such as dropbox, or emailing them to yourself.
Exercise 0: Logging in and out, locking the screen
You log into the CSIT computers using your student identification number prefixed by a u and the same password as elsewhere in the university. IF, however, you have never logged into the CSIT systems before then you need to first log on to STREAMS. This will trigger creation of an account on the CSIT
2
system. This was mentioned in the first lecture so hopefully everyone has done this. If you have not, then you will need to do this during the lab using a web browser. The creation of the account is not instantaneous. It can take up to a day to take effect.
If you have not created your account in advance of the lab, you can log in on the CSIT lab computer using a guest session. This will let you use programs on the computer such as the web browser, python interpreter, and so on. However, if you are logged in as guest, you will not be able to save any work except using external memory, or an online service: all files that you save will be automatically deleted when you end the guest session.
Before you log in, you can choose if you want the standard Ubuntu desktop or the XFCE desktop. They look a bit different, but both have the same functionality. The Ubuntu desktop is the default.
It is important that when you finish with the computer you log off. This is done by clicking on the icon in the top right hand corner that looks like an onoff button. In XFCE, click on your username which is shown in the top right hand corner. This will bring up a menu with an option to log off. Try logging off and on again now.
If you step away from your computer for a short period then you can lock the screen. This is another menu option you should try. Recognise that if you lock the screen and go away for a long period of time another person is likely to come and reboot the machine this will log you off and could result in you losing any work that you have not saved. So before locking the screen, save all your work. If in doubt about what this means ask a fellow student or your tutor.
Never leave the computer without either logging out or locking the screen! If you leave the computer unlocked, other people can read and change or delete everything that is on your account. They can also do malicious things to other people under your identity. You are responsible for anything done on, or through, a computer logged in with your user name.
Exercise 1: Exploring the desktop
Much like Windows and OS X, Linux provides a graphical user interface, which follows a consistent set of guidelines. You will find windows, menus, control panels, consistent visual user feedback, direct manipulation and interaction between programs, and other aspects of modern graphical user interfaces.
When you first log in, you will see a mostly empty desktop. Depending on whether you chose Ubuntu or XFCE as your desktop at login, you will have one of two different views: The way that you find and start application programs is slightly different between the two.
In Ubuntu, a few programs are accessible by buttons on the panel on the left side of the desktop. For other applications, click the button at the bottom of the panel or press the menu key on the keyboard, type in a few letters from the beginning of a word in the name of the application youre looking for and icons for matching applications should appear. Then click on the one you want:
In XFCE, programs are found under the Applications menu in the top panel. It has several submenus: For example, the Firefox web browser is found under the Internet submenu. The tools you will need for writing and running python programs are mostly found under the Development submenu:
Locate and start the following
3
Figure 2: screenshot Ubuntu menu
a web browser for example, Firefox the Spyder IDE
As you become more experienced, you may want to customize your desktop to better suite your personal style.
Exercise 2: Files and folders
Select Files icon from the sidebar Ubuntu or the Files application from the Accessories menu XFCE.
This will open a window displaying the contents of your home directory on the CSIT system. You can also create files and folders using this graphical interface. Files that you store here will remain when you next log in on any CSIT lab machine unless you are using a guest session. Again, as mentioned before this is not the same as your ANU home drive, so you will have to use the ANU Home Drive application, some other online service, or a USB memory, if you want to copy files between the two.
First, in your home directory, create a new directory folder named comp1730. In the comp1730 folder, create another named lab1.
Download and save this small python program: printbrickwall1.py. Assuming you are using the Firefox browser, you can do this by rightclicking on the link and selecting Save link as. . . . The way to
4
Figure 3: screenshot XFCE menu 5
do it in other browsers may be slightly different, but all should have the capability. Save this file in the lab1 directory that you created.
Part B
Choosing an IDE
An Integrated Development Environment, or IDE, for python combines a text editor for writing programs with a python shell interpreter, and some helpful functions to integrate the two.
Different IDEs are available in the CSIT lab and the InfoCommons enviroments. You will need to learn to use one of them. You can also use a standalone text editor, and the python3 or ipython interpreter through the commandline terminal. If you have no strong preference, we recommend that you use the Anaconda Spyder IDE, since its relatively easy to use and available on all platforms.
The python shell or interpreter is the program that executes python programs and interactive commands. It uses the ipython shell, which looks a bit different from the standard python3 shell, but under the hood they are mostly the same. When you start the ipython shell you should see a message like this
Python 3.6.8 Anaconda custom 64bit default, Dec 30 2018, 01:22:34
Type copyright, credits or license for more information.
IPython 7.5.0 An enhanced Interactive Python.
In 1:
The Python version number may be different in different environments. For example, on the InfoCommons computers it may be Python 3.6.1. The important thing is that it is python 3.
The prompt In 1: indicates that the interpreter is waiting for you to type in a command or expression. The number in brackets is a running counter of inputs and outputs. If you start a standard python3
shell, the prompt will instead look like this:
Examples on these lab pages will be written using the ipython shell prompt. There are a few other differences between the two shells: ipython gives you some shortcuts to make typing in commands interactively easier, and prefixes the values that results from evaluating an expression with Out. When you use certain types of graphics such as function plots, the ipython shell will show them inline in the shell window, while the python3 shell will show them in a separate window.
Spyder is the IDE that comes with Anaconda. It is available in both the CSIT lab environment and on the InfoCommons computers. You can find Spyder in the start menu of CSIT lab computers, and in the Windows start menu on the InfoCommons computers.
6
Figure 4: Spyder IDE
7
The Spyder IDE provides you with an editor to the left in the image below, a python interpreter below on the right as well as a window for help messages, variable inspector, and other things:
To run a program that youre editing or writing in Spyder, click the run icon
a file this may bring up a dialog with some options: just click Run to accept the defaults. As mentioned in the lectures, you can set the default working directory for the python shell in the upper right corner
outlined in red above.
IDLE is the default IDE for python, and comes with most python distributions. It is more simplistic some would say it is just bad, but some students may find it sufficient. You can find Idle3 Anaconda on the CSIT computers. On the InfoCommons Windows or Mac computers, you will have to use the search function on the start menu and search for IDLE or python IDLE. There is more than one version available, so check after you have started it that you have the right one it should show a message like the
one above. In particular, make sure it is running python 3.
IDLE has a separate window for the python shell, and one window for each file you edit. To run a program
in IDLE, you can select Run Module from the menu in the editor window, or press F5.
The PyCharm IDE is available in the CSIT lab environment. You can find it in the Development submenu on the GNOME desktop, or by searching in the Ubuntu start menu. Like Spyder, it provides a splitwindow layout, with an editor, a python shell, and other tools. Note that PyCharm does not open a python shell on startup, but only when you run a program. Before you can run a program in PyCharm, you will need to set the python interpreter. Click on the Configure Python Interpreter link:
and select or type in if its not available as a choice usrlocalanaconda3binpython3. The IDE that is used in the lectures is Spyder or PyCharm.
Exercise 3: Debugging
Open the file printbrickwall1.py that you saved in Exercise 2 in your IDE and attempt to run it. The program has several syntax errors, so it will not run. Depending on which IDE you are using, you may even see some errors highlighted before you try to run the program.
Identify and correct the syntax errors so that the program runs. The aim of this simple program is to print a brick wall pattern on the screen, like this:
8
. The first time you run
Figure 5: PyCharm, with configure link
9
If you cannot find or fix the errors, ask your tutor or another student for help. In case you get stuck, a cheat sheet is provided here.
However, even after the syntax errors have been corrected, the program may not print what it should i.e., exactly the output that is shown above. Modify the program so that you get the correct output. Do not write a whole new program! You should understand how this program works, and fix it so that it does the right thing.
Exercise 4: Programming the robot simulator
Download a copy of the robot.py module and save it in your lab1 directory.
Before you can do any robot programming, you have to make sure that you can import this module.
Remember that python will look for the module in the current working directory cwd.
When you run a python program, the cwd is the directory that the program is in. Thus, an easy way to make import work is to create a new program, call it firsttest.py, and save that too in the lab1 directory.
Note: All your python programs must be saved with a name that ends in .py. Your program should contain the single statement
import robot
Run the program. After this, you should be able to run robot simulator commands in the shell. Remember that you must start by initialising the simulation:
In 1: robot.init
After this, you can test driving the robot around:
In 2: robot.driveright
In 3: robot.liftup
In 4: robot.grippertoopen
In 5: robot.liftdown
If you want to see the real robot in action, here is a short video demonstrating it. If you play the video with subtitles enabled, you can see which command it is executing at each step.
10
The python help system
Python has a builtin help function, which gives you access to documentation in the python shell. Try it out on the robot module:
In 6: helprobot
…
Programming problems
1. The default simulator setup what you get when you start the simulator with just robot.init has three boxes on the shelf. It also has a limit of one on the height that the robot can reach. Height zero is on the table; height one is one step above.
Write procedures functions to make each pair of the boxes swap places, i.e., one to swap the left and middle box, one to swap the middle and right box, and one to swap the left and right boxes
ending with the middle box in the middle. Recall that a function definition is done like this:
def swapleftandmiddle:
…function suite…
The function suite is a sequence of statements. The extent of the suite is defined by indendation whitespace before the statement: all statements in the suite must have the same amount of
indentation. The standard is 4 spaces or 1 tab.
Remember to identify the assumptions of your function. Do you assume that the robot is standing in front of the left box or the middle one at the beginning? What is the assumed state of the lift and gripper? Document your assumptions in code comments.
You will likely find that defining functions in the shell is quite frustrating. Put your function definitions in firsttest.py which you created earlier. Then, after you have writen each function definition, run firsttest.py, the same way you ran printbrickwall1.py. You should now be able to call the new function from the shell.
2. Can you identify some manoeuvres that are common to all three problems? Split those off into separate functions. Your code should become more compact and easier to read.
3. The simulator allows the robot to lift a stack of boxes, not just a single box. However, if we try to do this with the real robot, chances are the stack will fall over. Try to write your functions so that the robot accomplishes each swap without ever lifting more than one box at a time.
Exercise 5 finding and using modules
Python comes with over 200 modules in the standard library, and many more optional modules can be installed. If you type
In 1: help
11
at the prompt, you enter pythons builtin help system. The prompt will change to
help
Type quit and press Enter to leave the help system and return to the normal python shell.
You can get information about a module by typing in the modules name, for example:
help math
This will print the complete listing of all functions, variables, classes, etc, that the module defines. This may be a bit more that what youre ready for at this point, so lets look at some other ways to find information.
First, most python shells support tab completion. This means that if you type in the beginning of name and press the Tab key, the shell will display a list of defined names that match that beginning. IDLE will show the list as a popup menu. This works with modules too, but only if they have been imported. For example, if you type
In 1: import math
In 2: math.
and then press the Tab key, you should get a list of all names defined in the math module. From the python shell, you can also use the help function to get information about specific names, such as,
In 3: helpmath.expm1
Note that here you did not enter into help mode.
Another way to find information is on the web. Open a browser and go to python docs. By default, the site will show you the documentation for the latest version of python3. If you are looking for documentation for another version, select it from the side bar, or using the menu in the top left corner. Follow the link Library Reference: this will take you to the main index for the python standard library. Find the module youre interested in on the page, and open its documentation page.
You can also find a lot of useful help just by typing a query into a search engine such as any one of the 40 active search engines listed on this wikipedia page. Make sure you include the name of the programming language python. You may also find it that you get more useful answers if you include the version number
3.X and specific details about your query.
Problem
Find a module that has a function that gives you the current date. Use it to print a message, such as:
In 1: printTodays date is:, X
12
replacing X with a call to the function that you found.
The print function prints text to the terminal window. You can give it several arguments, which will all be printed in sequence, on a single line. For example,
In 2: printThe sum of , 2, and, 3, is, 2 3
Note that bits of text are always enclosed in quotation marks.
13