Software Design Lecture Notes Spring 2006 Today: 1) quiz 2) demo scheduling 3) stupid python tricks! Demos ----- 1) explain the goal of the project in terms of min/max deliverable 2) Pick one cool thing to talk about in a little bit of detail a) design decision that worked (or failed in an interesting way) b) cool trick you found or invented c) cool module you are using 3) decide what you will demonstrate, practice it, freeze the code, and don't try anything new live! Don't be like Bill (www.cnn.com/TECH/computing/9804/20/gates.comdex/) 4) you don't have to use slides; consider a handout 5) it is not necessary that all team members present, but if you hand-off, just continue as if uninterrupted. 6) aim for 10-12 minutes and practice for timing. you can say a lot in 10 minutes if you are effective, but if you are disorganized, it will be over before you know it. 7) accentuate the positive! Talk about what you DID do, not what you didn't get done. Talk about what WORKS, not what's broken. 8) have fun, but make a good presentation The Golden Rule of Presentation: tell a story Live Free or Die ---------------- A value is considered False if it is a so-called False value: 0, 0.0, '', (), [], {}, None, False All other values are considered True values, including True. Here is a clever way you could implement logical and: def and(b1, b2): if b1: return b2 return None If the first value is True, then we return the second value. If the second value is True, then the result of the and is True. Similarly, we could implement or: def or(b1, b2): if b1: return True return b2 Notice: 1) in and, if the first value is False, we don't have to evaluate the second 2) in or, if the first value is True, we don't have to evaluate the second That's why the built-in operators implement SHORT-CIRCUIT EVALUATION! res = x and y If x is a False value, res = x, and y is never evaluated If x is a True value, res = y. res = x or y If x is a True value, res = x and y is never evaluated If x is a False value, res = y. This implementation is efficient and lends itself to a design pattern (of sorts) called "Live Free or Die" res = try_to_do_something(args) or die(a_horrible_death) If try_to_do_something succeeds, it returns a True value and everyone is happy. If it fails, it returns a False value and then we have to execute the second half of the or. In CardExample.py: def __cmp__(self, other): return self.suit - other.suit or cmp_rank(self.rank, other.rank) Subtraction is a common way to implement __cmp__ If self.suit and self.rank are different, the difference is a True value, and we return it without executing cmp_rank. If they are the same, the difference is 0, which is a False value, and we have to check the ranks. Oh, the humanity! Needless to say, this pattern is not immediately obvious to the most casual observer, and should be saved for special occasions. slices ------ In Python 2.3 or better, what do you think this does? t = range(10) t[::2] t[::-1] list comprehensions ------------------- What's this? t = range(10) [x**2 for x in t if x%2 == 0] break and continue ------------------ break exits the current loop immediately (go to end) continue goes to the next element in the sequence immediately (go to beginning) def closest(self, others): t = [] for animal in others: if animal is self: continue distance = self.distance_from(animal) t.append((distance, animal)) (distance, animal) = min(t) return animal For fun: rewrite this function as a one-liner.