If you do not have a strong preference for languages to use in this assignment, we suggest using Python 3 for the Oracle assignment. Python comes pre-installed on all department machines and on Macs. If you would like to install Python in your own computer, grab version 3.X from the Python website.
If you've never used Python before, we recommend reviewing Prof. Philip Klein's Python lab from Coding the Matrix. Python's official online documentation also provides an introduction.
For Oracle, we ask for two executable programs: topsort
and oracle
. To make a file executable, we must provide an interpreter directive (colloqually known as the "shebang" or "hashbang" on Unix machines) and set executable permissions. If you don't have the files topsort
and oracle
, create them and add the following lines to the top of each:
#!/usr/bin/python3
print("Hello World!")
The #!
line serves as the interpreter directive, and the Python interpreter will run the file's code when executed. To set executable permissions, run chmod +x topsort
and chmod +x oracle
. To learn the details of Unix permissions, we recommend reviewing the relevant parts of this Wikipedia article. To test your programs, run ./topsort
and ./oracle
. If everything worked, you'll receive a "Hello World!" from each.
Python's sys
module provides access to command line arguments. To import the module, add import sys
to your program. The expression sys.argv
produces a list of strings corresponding to command line arguments. Note that the program's name comprises the first element of this list, so sys.argv[1]
returns the first user-specified command line argument.
Python's libraries and built-in functions offer several ways of accessing standard input and output. We suggest accessing standard input and output through the sys
module, also used for accessing command line arguments. sys.stdin.readlines()
produces a list of strings corresponding to each line of standard input. Similarly, sys.stdout.write("Hello World!")
writes "Hello World!" to standard output.
Python's subprocess
module provides an interface for running other processes from within a program. For example, in oracle
, you'll need to run a topsort program. To import the module, add import subprocess
to your program. To start a process and bind its handle to a variable, use a statement of the following form:
import subprocess
process = subprocess.Popen(["/usr/bin/tsort"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Additional arguments to the process follow in the list of the first argument. To write to the standard input of process
, use the communicate method:
in_str = "1 2\n3 4\n"
in_data = in_str.encode('utf-8')
(out_data, err_data) = process.communicate(in_data)
out_str = out_data.decode('utf-8')
err_str = err_data.decode('utf-8')
out_str
contains the standard output and err_str
contains the standard error. Use process.returncode
to retrieve the return code.
Suppose you have a file named myfile.txt
with the following content:
7 11
7 8
3 10
5 11
3 8
8 9
11 10
11 2
11 9
To run your topsort or Unix's built-in topsort program with this data, use a command of the following form to redirect the contents of the file as standard input: /usr/bin/tsort < myfile.txt
(or ./topsort < myfile.txt
for your own topsort). Similarly, to invoke your oracle
, run ./oracle /usr/bin/tsort 50
or ./oracle ./topsort 50
.