La lecture en ligne est gratuite
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
Télécharger Lire

Python Development (session 10)

De
40 pages
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 plus Voir moins

Vous aimerez aussi

FOSSEE(IIToBbmya)
Python Development
FOSSEE
Department of Aerospace Engineering IIT Bombay
1 May, 2010 Day 2, Session 4
PythonDevelopment1/63
yabmtyP)DnohleveSSFO(IEEBoIT
Debugging Errors and Exceptions Strategy Exercise
opment2/36
1
Tests: Getting started
2
Coding Style
3
TestsratsOdetteG:gnitliutne
Opendcpy.g
printgcd(15, 65) printgcd(16, 76)
python gcd.py
eTtteG:stsde!sititartingsdrevedgcIIE(SEOSF6
a%b)
defgcd(a, b): ifa % b == 0: returnb return,gdcb(
t3/3pmenvelonoeDyPhtab)yBTmo
seTdmgculodets:GettingstarteFdnildmcsuniogrueveDmpolyP)ynoht
fromgcdrotipmgcd deflcm(a, b): return(a * b)
/
5 4 56
Openyp.cml lcm=acgd(ba,b)
printlcm(14, 56)
python lcm.py
gcd(a,
b)
t4en6/3IITBombaFOSSEE(
python gcd.py python lcm.py
__ ____ __ name ==" main ": printgcd(15, 65) printgcd(16, 76)
if
SOES(EIIFthPyDeonomTBy)ba3/5t
defgcd(a, b): ifa % b == 0: returnb returngcd(b, a%b)
Editpyd.gcfile to:
6olevnempattrderWtinisgatTests:Gettingseludomenola-dn
%
b)
def
gcd(a, b): ifa % b == 0: returnb returngcd(b, a
name ==’ main ’: __ ____ __ assert gcd(15, 65) == 5 assert gcd(16, 76) == 4
if
6/3t6eneDevolmp)yyPhtnoIITBombaFOSSEE(Tests:GettitsgnetratuAdtamogtintses
IT(ImbBoFOEESSveDnpoleP)yaohty/763emtn
if
y
__ ____ __ name ==’ main ’: forlineinopen(ermbnut.sxt): numbers = line.split() x = int(numbers[0]) y = int(numbers[1]) result = int(numbers[2]) ifgcd(x, y) != result: print"Failed gcd test for", x,
esT:GtstietstngestsraetAdtumotanitg
ab)yyPht(EIIBTmoFOSSEesag:ststteGsgnitratPyedonthstTeckPaTe
Python’situntset: http://docs.python.org/library/ unittest.html
nose: http: //code.google.com/p/python-nose/
6oleveDno3/8tnemp
OFSSEEveDnpoletnem63/9IlTy(tISmbBoe)PayhoytdingCo
2
Coding Style
3
Debugging Errors and Exceptions Strategy Exercise
Outline
1
Tests: Getting started
tencydnoCsnsibalitiaySgnidoCdaeRelytFOEESSTII(bmoBP)yaythonDevelopment013/6
Readability Counts! Code is read more often than its written. Consistency! Know when to be inconsistent. Play telephone with that line of code.
Fab)yBTmo(EIISOESpmenveloonDePyth36
Style Rule #1 Naming is 80% of programming
t11/
amount = 12.68 denom = 0.05 _ (amount/ ) num coins = round denom _ _ rounded amount = num coins * denom
diCoAelytSgnnoitseuqodstofgoyle
Un pour Un
Permettre à tous d'accéder à la lecture
Pour chaque accès à la bibliothèque, YouScribe donne un accès à une personne dans le besoin