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.
The focus of this reading is to introduce scripting in Python.
The readings for Monday, February 12 are:
Here are some additional resources:
In this course, we will be using Python 3.6 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 Anaconda
by performing the following modifications to your
# CSH setenv PATH ~pbui/pub/anaconda3/bin:$PATH # BASH export PATH=~pbui/pub/anaconda3/bin:$PATH
Note, there will not be an homework assignment next week. Instead, we will have an in-class exam on Friday, February 16. To prepare for this exam, please take a look at Checklist 01.
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 reading04 # Create reading04 branch and check it out $ cd reading04 # Go into reading04 folder # Download Reading 04 Makefile $ curl -LO https://gitlab.com/nd-cse-20289-sp18/cse-20289-sp18-assignments/raw/master/reading04/Makefile # Execute tests (and download them) $ make
Record the answers to the following Reading 04 Quiz questions in your
echo.py as the basis for a Python script called
accomplishes the same task as the modified version of
exists.sh in Reading
# 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
cat.py 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 04 - 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
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 reading04 # Create reading04 branch and check it out $ cd reading04 # Go into reading04 folder $ $EDITOR answers.json # Edit your answers.json file $ ../.scripts/submit.py # Check reading04 quiz Submitting reading04 assignment ... Submitting reading04 quiz ... Q1 0.20 Q2 0.40 Q3 0.40 Score 1.00 $ git add answers.json # Add answers.json to staging area $ git commit -m "Reading 04: 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 .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 2.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 commit -m "Reading 04: Scripts" # Commit work $ git push -u origin reading04 # Push branch to GitLab