Readings

This week the readings will focus on the basics of programming in Python

The readings for Monday, March 21 are:

  1. Scipy Lecture Notes: 1.2. The Python language

Optional Resources:

  1. A Byte of Python

  2. Automate The Boring Stuff

  3. Think Python

Python 2.7

As mentioned in class, we will be using Python 2.7 rather than Python 3.x. On most modern Unix systems, you will not need to install anything extra to run Python 2.7, but if you do not have it, I recommend the Anaconda distribution.

Activities

As mentioned in class, for this reading assignment and the next homework assignment, we will be re-implementing a few standard Unix utilities in Python. For this assignment, you are to implement the following commands:

  1. uniq.py: This is a Python implementation of the uniq command. It should support the following flags:

    $ ./uniq.py -h
    usage: uniq.py [-c] files ...
    
        -c      prefix lines by the number of occurrence
    
  2. head.py: This is a Python implementation of the head command. It should support the following flags:

    $ ./head.py -h
    usage: head.py [-n NUM] files ...
    
        -n NUM  print the first NUM lines instead of the first 10
    
  3. wc.py: This is a Python implementation of the wc command. It should support the following flags:

    $ ./wc.py -h
    usage: wc.py [-c -l -w] files ...
    
        -c      print the byte/character counts
        -l      print the newline counts
        -w      print the word counts
    

    For your implementation, the user must pass one of the -c, -l, or -w flags. Otherwise, wc.py should display an error message and then terminate with an error code.

  4. cut.py: This is a Python implmenetation of the cut command. It should support the following flags:

    $ ./cut.py -h
    usage: wc.py [-d DELIM -f] files ...
    
        -d DELIM  use DELIM instead of TAB for field delimiter
        -f FIELDS select only these FIELDS
    

    For your implementation, the user must pass at least the -f flag to specify the FIELDS. Otherwise, cut.py should display an error message and then terminate.

    Furthermore, your implementation only needs support the following types of FIELD arguments:

    1
    1,2
    3,1,2
    3,1,2,2,2,2,2
    

    You do not need to support ranges.

Hints

Here are some hints:

  1. Although Python uses duck typing, you do need to pay attention to types. For instance, if you expect an argument to be an int, but the value is a str, you can convert it:

    number = int(string)
    
  2. You can remove the newlines at the end of each line of input by using the str.rstrip method.

  3. For uniq.py, you may wish to use a dict to keep track of the unique lines.

  4. For head.py, you may wish to terminate the program early by using sys.exit.

  5. For wc.py, you may wish to use the len function along with str.split.

  6. For cut.py, you may wish to use a set to handle the FIELDS.

  7. The Idlebin repository on Bitbucket contains the examples done in in class and test scripts.

Standard Input

Note, if no files are specified, then each script should read input from sys.stdin. Likewise, if - is passed as a file argument, then sys.stdin should be read.

Command Line Options

To handle command line options, you should use the getopt module as demonstrated in the Lecture 12 slides and sort.py.

Idlebin

The example utilities created in class, along with test scripts can be found in the Idlebin repository on Bitbucket.

Feedback

If you have any questions, comments, or concerns regarding the course, please provide your feedback at the end of your response.

Submission

To submit your assignment, please commit your work to the reading09 folder in your Assignments Bitbucket repository by 11:59 PM on Monday, March 21.