Computer Sciences Department logo

CS 368-2 (2012 Spring) — Day 8 Homework Hints

These hints are intended for students who are relatively new to programming. If you are not a beginner, try to complete the homework without reading this page first. The more challenges that you face, perhaps initially fail at, then ultimately overcome, the more you learn!

The hints below actually form an ordered work plan. That is, I wrote them assuming that you would do each step in order. It is probably helpful to complete one step before moving on.

  1. First, copy the my_system() function from today’s slides. Then, write a “main” part of your script below it that calls the function with a very simple command. Try “ls -l” or something like that. Write all of the code to run the command, check its exit status for errors, and print its output. Do not move on until you get this much to work correctly! And even then, I suggest experimenting with a few other simple shell commands, so that you get the hang of running another program from within your script.
  2. Next, instead of running arbitrary shell commands, run the simulator program, just once (no loop yet!). You should not need to change the code that actually runs the command, checks for errors, and prints its output — all of that code stays the same (hopefully). Again, get this step to work before moving on!
  3. Next, enclose your code that runs the simulator in a simple loop. If you have forgotten how to write loops that execute a certain number of times, look up the xrange() built-in function in Python. At first, just hard-code the script to loop exactly 2 times through the running of the simulator. The code from above basically does not change, except to be enclosed by the loop.
  4. Next, leaving alone all of the code you have written so far, add some code to process the parts of the command line into one or more global variables. In the slides, there were two different modules highlighted for parsing command-line options. Do you need either of them? Re-read the assignment and think about whether you need to handle options or just a fixed set of arguments. Write print statements to verify that your script is parsing the command line correctly.
  5. Next, change your simulator-running code to actually use the information from the command line. This should be a fairly easy step.
  6. OK, take a break, and think about what you have accomplished and what remains to be done. Your script now can run the simulator many times with command-line arguments taken from those given to your own script. However, it is currently just printing all of the simulator’s output to the screen, which gets to be too much output and does not meet the requirements of Part 2 anyway. So, next you must tackle the output itself. The output from each run of the script consists of a single string; yes, there are multiple lines of output in that string, so it contains embedded newline characters. Try printing repr(your_output_variable_here) to see a more literal view of what is in your string. So you have a string… and you need to extract parts of it… what have you learned that is good for extracting parts of a string based on surrounding patterns?
  7. Here is a suggestion for extracting data from the output of the simulator: Consider writing a little function that takes the output string and some extra information about the extraction pattern, then returns the extracted data. If you can get that function to work on one data element, then you should be able to use it 2–3 more times to extract the remaining data elements. Now you have 3–4 total data elements per run of the simulation. Print out those values instead of the full simulation output. Now you are almost there!
  8. In addition to printing out the extracted data, also save it in a data structure for later use. What data structure is appropriate here? Do you need to look up each cluster of 3–4 values by some other key later? Or is it sufficient just to loop through the items? Think now about how your code will look when writing the data to an output file; doing so may help you think about the best way to store the data in the interim.
  9. OK, last step (for Part 2)! You have data, and so now it just needs to be written out to a file. Each line is formatted the same way. This should be easy…