Python DevelopmentFOSSEEDepartment of Aerospace EngineeringIIT Bombay1 May, 2010Day 2, Session 4FOSSEE (IIT Bombay) Python Development 1 / 36Tests: Getting startedOutline1 Tests: Getting started2 Coding Style3 DebuggingErrors and ExceptionsStrategyExerciseFOSSEE (IIT Bombay) Python Development 2 / 36Tests: Getting startedgcd revisited!Opengcd.pydef gcd(a, b):if a % b == 0:return breturn gcd(b, a%b)print gcd(15, 65) gcd(16, 76)python gcd.pyFOSSEE (IIT Bombay) Python Development 3 / 36Tests: Getting startedFind lcm using our gcd moduleOpenlcm.pyablcm =gcd(a,b)from gcd import gcddef lcm(a, b):return (a b) / gcd(a, b)*print lcm(14, 56)python lcm.py5456FOSSEE (IIT Bombay) Python Development 4 / 36Tests: Getting startedWriting stand-alone moduleEditgcd.py file to:def gcd(a, b):if a % b == 0:return breturn gcd(b, a%b)if __name__ == "__main__":print gcd(15, 65) gcd(16, 76)python gcd.py lcm.pyFOSSEE (IIT Bombay) Python Development 5 / 36Tests: Getting startedAutomating testsdef gcd(a, b):if a % b == 0:return breturn gcd(b, a % b)if __name__ == ’__main__’:assert gcd(15, 65) == 5 gcd(16, 76) == 4FOSSEE (IIT Bombay) Python Development 6 / 36Tests: Getting startedAutomating testsif __name__ == ’__main__’:for line in open(’numbers.txt’):numbers = line.split()x = int(numbers[0])y = int(numbers[1])result = int(numbers[2])if gcd(x, y) != result:print "Failed gcd testfor", x, yFOSSEE (IIT Bombay) Python Development 7 / ...
Voir