Notes for CSC 162, 20 April 2010 ff (not in the text) Project 6 due Friday 30 Apr., noon PLEASE FILL OUT ON-LINE COURSE EVALUATION ---------------------------------------- Python as a scripting ("glue") language "force kill" example import sys, os, re, time if len(sys.argv) != 2: sys.stderr.write('usage: ' + sys.argv[0] + ' pattern\n') sys.exit(1) PS = os.popen("/bin/ps -w -w -x -o'pid,command'") # -w -w : unlimited width output # -x : don't require a controlling terminal # -o'pid,command' : output fields line = PS.readline() # discard header line line = PS.readline().rstrip() # prime pump while line != "": proc = int(re.search('\S+', line).group()) # RE matches any string of non-whitespace characters # group() returns the whole match if re.search(sys.argv[1], line) and proc != os.getpid(): print line + '? ', answer = sys.stdin.readline() while not re.search('^[yn]', answer, re.I): # .I flag : ignore case print '? ', # trailing comma inhibits newline answer = sys.stdin.readline() if re.search('^y', answer, re.I): os.kill(proc, 9) time.sleep(1) try: # expect exception if process os.kill(proc, 0) # no longer exists sys.stderr.write("unsuccessful; sorry\n"); sys.exit(1) except: pass # do nothing sys.stdout.write('') # inhibit prepended blank on next print line = PS.readline().rstrip() ======================================== Web scripting CGI server-side client-side ---------------------------------------- CGI scripting in Python http://localhost/cgi-bin/status.py (file /Library/WebServer/CGI-Executables/status.py) http://localhost/~scott/add.html (file /Users/scott/Sites/add.html; calls /Library/WebServer/CGI-Executables/add.py) ---------------------------------------- server-side scripting in PHP http://localhost/~scott/add2.html (file /Users/scott/Sites/add2.html) http://localhost/~scott/add.php (file /Users/scott/Sites/add.php) ---------------------------------------- client-side scripting in JavaScript http://localhost/~scott/js_adder.html (file /Users/scott/Sites/js_adder.html) ======================================== Intro to Java concept of compilation efficiency early error detection JIT compilation javac, java static typing declarations built-ins can't be target of a reference, or element of a container classes set of fields, methods statically known parameter passing quite similar to Python built-ins (ints, doubles, chars, Booleans) passed by value objects (including arrays) passed by reference everything embedded in a class public, private static free-format, with curly braces and semicolons hello, world: class hello { public static void main(String[] args) { System.out.println("hello, world"); } } more complicated example: insertion sort import java.util.Vector; import java.lang.Integer; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class InsertionSort { // has to have same name as file static void insSort(int[] V) { for (int i = 1; i < V.length; i++) { // note weird syntax of /for/ loop int curVal = V[i]; int pos = i; while (pos > 0 && V[pos-1] > curVal) { // parens required around condition V[pos] = V[pos-1]; pos--; // decrement operator } V[pos] = curVal; } } public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); Vector nums = new Vector(); while(true) { String line = ""; // initialization required for "definite assignment" try { line = in.readLine(); } catch (IOException e) { // handler required, because readLine can raise } if (line.equals("")) break; int num = Integer.parseInt(line); nums.add(num); // append } int[] A = new int[nums.size()]; // There exists a toArray() method on Vectors that would // fill in A in one fell swoop, but only with capital-I // Integers, not with ints. for (int i = 0; i < nums.size(); i++) { A[i] = nums.get(i); } insSort(A); for (int i = 0; i < nums.size(); i++) { System.out.println(Integer.toString(A[i])); } } }