Everyone:

Next week, we will transition from shell scripting and filters to creating more sophisticated scripts using the Python programming language. Using a more advanced scripting language such as Python will allow us to use sequence data structures such as lists and mapping data structures such as dictionaries, and thus perform more sophisticated data manipulation efficiently and effectively.

TL;DR

The focus of this reading is to introduce scripting in Python.

Readings

The readings for this week are:

  1. Scipy Lecture Notes: 1.2. The Python language

  2. A Byte of Python - Data Structures

Optional Resources

Here are some additional resources:

  1. A Byte of Python

  2. Automate The Boring Stuff

Python 3

In this course, we will be using Python 3.x rather than Python 2.x. Although Python 2 is still widely available, it is deprecated and will soon be unsupported. Therefore, most new projects and applications are moving to Python 3, so we will follow suit1.

Because Python 3 is not the default on most systems, you may need to install it yourself. On Windows or macOS, I recommend the Anaconda distribution, which includes Python and a bunch of useful libraries and tools.

On the student machines, you can use the instructor's copy of Python by performing the following modifications to your PATH environmental variable:

export PATH=~pbui/pub/pkgsrc/bin:$PATH

This will give you access to a more current version of Python along with some of the libraries we will be using such as Requests.

Quiz

This week, your reading quiz is split into two sections: the first part is your normal dredd quiz, while the second part involves three Python scripts: exists.py, head.py, and sort.py.

To test these scripts, you will need to download the Makefile and test scripts:

$ git checkout master                 # Make sure we are in master branch
$ git pull --rebase                   # Make sure we are up-to-date with GitLab

$ git checkout -b reading05           # Create reading05 branch and check it out

$ cd reading05                        # Go into reading05 folder

# Download Reading 05 Makefile
$ curl -LOk https://gitlab.com/nd-cse-20289-sp19/cse-20289-sp19-assignments/raw/master/reading05/Makefile

# Execute tests (and download them)
$ make

Questions

Record the answers to the following Reading 05 Quiz questions in your reading05 branch:

exists.py

Use the echo.py script as the basis for a Python script called exists.py, which accomplishes the same task as the modified version of exists.sh in Reading 02:

# Run script and check error code
$./exists.py * && echo Success
exists.py exists!
README.md exists!
Success

$ ./exists.py * ASDF || echo Success
# Run script and check error code
exists.py exists!
README.md exists!
ASDF does not exist!
Success

head.py

Use the cat.py script as the basis for a Python script called head.py, which is a simple implementation of the head command:

# Print Usage
$ ./head.py -h
Usage: head.py files...

    -n  NUM      print the first NUM lines instead of the first 10

# Read from STDIN
$ seq 10 | ./head.py
1
2
3
4
5
6
7
8
9
10

# Read from file specified in command line argument
$ ./head.py README.md
Reading 05 - README
===================

# Limit number of lines
$ seq 10 | ./head.py -n 1
1

# Limit number of lines and read from explicit STDIN (-)
$ seq 10 | ./head.py -n 1 -
1

Hints

sort.py

Use the cat.py script as the basis for a Python script called sort.py, which is a simple implementation of the sort command:

# Print Usage
$ ./sort.py -h
Usage: sort.py files...

    -r  reverse the result of comparisons

# Read from STDIN
$ seq 10 | ./sort.py -r 
9
8
7
6
5
4
3
2
10
1

Hints

Submission

To submit you work, follow the same process outlined in Reading 01:

$ git checkout master                 # Make sure we are in master branch
$ git pull --rebase                   # Make sure we are up-to-date with GitLab

$ git checkout -b reading05           # Create reading05 branch and check it out

$ cd reading05                        # Go into reading05 folder
$ $EDITOR answers.json                # Edit your answers.json file

$ ../.scripts/submit.py               # Check reading05 quiz
Submitting reading05 assignment ...
Submitting reading05 quiz ...
     Q1 0.10
     Q2 0.20
     Q3 0.20
  Score 0.50

$ git add answers.json                # Add answers.json to staging area
$ git commit -m "Reading 05: Quiz"    # Commit work

$ $EDITOR exists.py                   # Edit your exists.py file

$ ./test_exists.sh                    # Test your exists.py
Testing exists.py ...
 exists.py *                              ... Success
 exists.py * ASDF                         ... Success
   Score 0.50

$ $EDITOR head.py                     # Edit your head.py file

$ ./test_head.sh                      # Test your head.py
Testing head.py ...
 Usage                                    ... Success
 head on /etc/hosts                       ... Success
 head on /etc/passwd                      ... Success
 head on stdin (implicit)                 ... Success
 head on stdin (explicit)                 ... Success
 head -n 1                                ... Success
 head -n 2                                ... Success
 head -n 3                                ... Success
 head -n 4                                ... Success
 head -n 5                                ... Success
 head -n 6                                ... Success
 head -n 7                                ... Success
 head -n 8                                ... Success
 head -n 9                                ... Success
 head -n 10                               ... Success
   Score 1.50

$ $EDITOR sort.py                     # Edit your sort.py file

$ ./test_sort.sh                      # Test your sort.py
Testing sort.py ...
 Usage                                    ... Success
 sort    on /etc/group                    ... Success
 sort -r on /etc/group                    ... Success
 sort    on /etc/passwd                   ... Success
 sort -r on /etc/passwd                   ... Success
 sort    on stdin (implicit)              ... Success
 sort -r on stdin (implicit)              ... Success
 sort    on stdin (explicit)              ... Success
 sort -r on stdin (explicit)              ... Success
   Score 1.50

$ git add Makefile                    # Add Makefile to staging area
$ git add exists.py                   # Add exists.py to staging area
$ git add head.py                     # Add head.py to staging area
$ git add sort.py                     # Add sort.py to staging area
$ git commit -m "Reading 05: Scripts" # Commit work

$ git push -u origin reading05        # Push branch to GitLab

Remember to create a merge request and assign the appropriate TA from the Reading 05 TA List.


  1. As a luddite, this breaks my heart, but there is no point in yelling at clouds. Evolve or die.