This week the readings will focus on the basics of programming in Python
The readings for Monday, March 21 are:
Optional Resources:
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.
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:
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
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
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.
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.
Here are some hints:
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)
You can remove the newlines at the end of each line of input by using the str.rstrip method.
For uniq.py
, you may wish to use a dict to keep track of the unique lines.
For head.py
, you may wish to terminate the program early by using
sys.exit.
For wc.py
, you may wish to use the len function along with
str.split.
For cut.py
, you may wish to use a set to handle the FIELDS
.
The Idlebin repository on Bitbucket contains the examples done in in class and test scripts.
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.
To handle command line options, you should use the getopt module as demonstrated in the Lecture 12 slides and sort.py.
The example utilities created in class, along with test scripts can be found in the Idlebin repository on Bitbucket.
If you have any questions, comments, or concerns regarding the course, please provide your feedback at the end of your response.
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.