DATA 530 Lab 4 - Libraries, Debugging, and Getting Help¶
This lab practices using libraries, debugging code, and getting help.
Objectives¶
Import and use libraries in R and Python.
Perform debugging on Python and R code.
Use the built-in help in R.
Use online resources such as Stack Overflow to find/request help.
Practice with command-line interfaces
Create and navigate directories
Create, modify, rename and delete files
Understand Git and Git fundamentals
Part 1: Bash Crawl¶
Here is a video walkthrough of the first few steps of the activity.
The written portion is written below:
Setting up for the game¶
Open a Terminal window.
Change the directory to where this file is:
cd /path/to/this/folder
.Type
ls
into the Terminal; you will see two things:README.md
and theentrance
directory.Once you go into the
entrance
directory (cd entrance
), the command line based game will begin!
Tip: When I say “go into the cd <BLANK>
. In this game, a “room” is a “directory”.
Starting the game¶
Now that you have entered the dungeon
entrance
, you should typels
in the terminal to look around and see what’s there.You will see there are two “things” in this room, there is a
scroll
, and another room calledcellar
.Let’s read the scroll using
cat scroll
.
Tip: When I say “read the scroll”, I mean output the contents of the scroll file into the Terminal using cat scroll
.
Great! The scroll told us that we can look and see what’s in the room using
ls
and move in and out of rooms usingcd <directory>
. Let’s go into the cellar:cd cellar
.Once we’re in the
cellar
, let’s have a look around:ls
. You should see anarmoury
, ascroll
, and sometreasure
.Let’s read the scroll:
cat scroll
. It should tell you aboutls -F
. Here is some more information aboutls -F
:
Display a slash (`/’) immediately after each pathname that is a directory, an asterisk (‘*’) after each that is executable, an AT sign (‘@’) after each symbolic link.
That sounds useful, let’s do an
ls -F
in the cellar to see what each of the things are. You’ll notice that the armoury is another room, the scroll is just a file, and the treasure has a “*” at the end, so it’s an executable. Cool! We don’t know what to do with the executable yet, but let’s trycat treasure
to see what happens.Mm. Didn’t work - okay let’s move on and come back to this room later. Let’s go into the
armoury
now:cd armoury
and thenls -F
.We see another scroll (and a potion, a treasure, and another room)! Let’s read the scroll:
cat scroll
.Oooh, we have now learned how to collect
treasure
! Let’s do that in the armoury and follow the instructions to keep our treasure (using environment variables).
Tip: When I say “pick up the ./ <BLANK>
.
It tells us to go back and get the treasure from the cellar! Let’s go and do that now with
cd ..
, pick up the treasure from the cellar, and then come back to the armoury.The potion is also something we can “pick up”, so let’s go ahead and do that. It asks us if you want to drink it: type ‘Y’ for yes, and anything else for no (like ‘n’). Choose an option and see what happens (follow on-screen instructions!)
Alright check if there’s anything else to do this in room, if not, head to the
chamber
next. Start withls -F
and hopefully by now, you know the drill!Have fun! More dungeon rooms will open up as you complete certain tasks, kill certain monsters, and read specific scrolls and tomes. Have fun with it, this is your opportunity to practice your Terminal skills - it sure beats the way I learned Terminal commands (blindly typing in commands in a black box until I got it to do what I wanted!). There is no need to hit every room in the dungeon, but do make a concerted effort and you’ll likely get most of the marks for this lab.
Tip: You can ignore a few of the more “advanced” entries of the Tome, including anything related to tmux
or gzip
. I say “pick up the ./ <BLANK>
.
What you need to submit for Part 1.¶
When you’re done exploring the dungeon there are several things I want you to do:
1. Commit everything to your repository and push to the cloud:¶
git add .
git commit -m "Finished playing with the dungeon"
git push
2. Run tree
, and save the output to a file:¶
Update: This has been made optional because it’s not trivial to install tree
on Windows. This page has some directions if you’re interested in installing it.
You may need to install tree (if you didn’t already do it in the dungeon) with: conda install -c conda-forge tree
tree
tree > mydungeonmap.md
3. Commit the dungeon map to your repository¶
Run the following commands in your Terminal:
git add .
git commit -m "Added my dungeon map"
git push
4. Save the last 100 commands from your Terminal¶
We want to see some serious effort with the dungeon map, so we are asking for the last 100 commands you entered into the Terminal. Please open this file and do a quick check to make sure there is no sensitive information in this file (like passwords or private messages). If there is, you can simply edit file or remove that command.
Note: Just because we’re asking for the last 100 commands doesn’t mean that we expect you to have 100 commands, you may have less and you may have more. That’s fine.
history 100 > dungeonHistory.md
5. Commit the dungeon history to your repository¶
Run the following commands in your Terminal:
git add .
git commit -m "Added my dungeon history"
git push
Part 2: Debugging and Libraries¶
Question #1 - Python Library (3 marks)¶
Find a library that can read JSON data. Write a Python program that uses this library to read the data JSON file and print out the province with the largest population.
### Your answer here
Question #2 - R Library (3 marks)¶
Repeat question #2 for R. Find a library that can read JSON data. Write an R program that uses this library to read the data JSON file and print out the province with the largest population.
### Name of your .R file (in this repository)
Question #3 - Debugging (3 marks)¶
Debugging involves finding errors in code that is not working properly. Three types of errors:
Syntax errors - are identified when the code is run and will cause an error due to incorrect commands
Logic errors - occur when code has correct syntax but does not function as expected
Exceptions - occur when a run-time event causing an error is not handled by the code (try-except)
Indicate how to fix the following code programs and indicate what kind of error(s) occurred.
Code Sample 2¶
# Allows user to enter a number until enters STOP. Multiplies number by 2. Handles incorrect user input.
num = int(input("Enter a number:"))
while num != "STOP":
print(num*2)
num = int(input("Enter a number:"))
### Your answer here
Code Sample 3¶
# Prints out items in a list in between 4 and 8 inclusive
data = [6, 5, 3, 4, 7, 1, 8]
for i in range(0,len(data)+1):
if data[i] >= 4 && data[i] < 8:
print("Data: ")
print(data[i])
### Your answer here
Question #4 - Create a Stack Overflow account (1 mark)¶
Create an account on Stack Overflow.
### Your username here
(Optional) Question 5 (1 mark)¶
See if you can help someone answer a new question in Python or R.
Provide a link to your post for the mark.
### Your answer here