CSC323 2010S, Class 03: Python Lab Overview: * Beautiful Code 27. * About programming exercises. * Programming Exercise 1: Some Basics. * Programming Exercise 2: Binary Search. * Programming Exercise 3: A Fraction Class. Admin: * Beautiful Code reading for Thursday: Chapter 19: Multidimensional Iterators in NumPy. Send me a question. * Other reading for Thursday: Find something interesting on the UML and then send me a question. * Summer MAP discussion, today at 4:15 in JRC 101. * EC: Thursday's CS Extra: Summer opportunities in CS. * EC: Friday's CS Table: Pair Programming. Chapter 27 of Beautiful Code: A RESTful Application * Summarize the application: * Purpose * Components * Architecture * Purpose: A Web-based application through which one entity (aka "the distributor") can access information from another entity (aka "the supplier") * Components: * Big things * Web server software * Web client software * Legacy system * Stuff that binds it together * Something to connect server software to legacy system * HTTP (a communication protocol used in the RESTful architecture/hack) * XML (a mechanism for organizing information for transmission, used in the RESTful architecture) * Rosettanet standard (Rules for the formation of valid XML documents and such) * Implementation language * Java for the client and server software * Strategies * Design patterns * OOAD * Requirements: * The code does what the legacy system did. * The code is easily extensible by less qualified programmers, making it easy to add additional features. Thesis: * IT'S BEAUTIFUL: Provides a good example of OO design principles and XML parsing techniques * What are the OO design principles illustrated? * What are the XML parsing techniques illustrated? * Why are these good examples? * Or are they? * What else can we learn from this? What OO design principles are illustrated? * Interfaces and polymorphism: The adapter interface means that some inexperienced programmers can easily extend the system. * But he doesn't really discuss how the client program knows about the new services or deals with the potentially different responses. * Factory methods * Not in the way most of us are accustomed to using factories. * No particular rationale. * Information hiding, in the IBM-supplied interface to the RPG system * In a well designed object oriented system, you don't need to write a lot of code to change behavior. E.g., we only need to override the POST handling method. Detour: What is XML? * XML is generalized HTML. * A way to hierarchically mark up data, so that it can be transmitted as 'plain text'. * A data type definition (DTD) describes legal tags, legal attributes, and legal nesting of tags. * Any XML document describes a tree-like structures. What are the XML parsing techniques illustrated? * Use XPath: Give the path to a node, and that gives you the node. * You don't have to write your own XML parser. * Once you've extracted values, ignore the XML and put them in a dictionary (in this case, implemented as a Hashmap) * Build all your XML by hand. * [Sometimes quick and dirty wins.] What is ugly about this code? * Building XML by hand. * Iterator xpathvals = xpathmappings.values.iterator(); Iterator xpathvars = xpathmappings.keySet().iterator(); while (xpathvals.hasNext() && xpathvars.hasNext()) { node = XPathAPI.selectSingleNode(root, (String) xpathvals.next()); requestvalues.put((String) xpathvars.next(), node.getChildNodes().item(0).getNodeValue()); } * xpathmappings is a dictionary. keys: easy ways to refer to nodes values: paths to nodes * This code assumes that the values and keys iterate in the same order. Do they? Programming Exercises * We'll do a few rounds of programming execises. * At some point, I'll stop you and we'll go over the code you've written for one round. + It's okay if you're not finished by the time I stop you. + If you finish one round before the discussion, just go on to the next round. * Be prepared to discuss what you had difficulty with in each round. Round 1: Some Basics * Write some simple program segments to make sure that you understand the core aspects of Python: Variables, Strings, Procedures, Conditionals, Lists, Loops, Classes, and Dictionaries * Create a small inheritance heirarchy and figure out what happens when multiple ancestors provide methods with the same name. * Write a simple grep procedure that takes as parameters a pattern string and a list of strings, and returns a list of all strings in the list that contain the pattern. (The pattern should be taken verbatim.) Round 2: Binary Search * Simplified problem: + Inputs: - i, an integer - vals, a sorted array (list) of integers. (Sorted in increasing order.) + Output: - If i is in vals, the index of i - If i is not in vals, -1 Round 3: A Fraction Class * Implement a simple fraction class to represent rational numbers. Make sure to support traditional operations on rationals.