• No results found

'esting functions

In document python for biologists (Page 120-124)

hen riting !o%e of any type, its important to perio%i!ay !he!- that yo&r !o%e %oes hat yo& inten% it to %o. 'f yo& oo- ba!- over the so&tions to eer!ises from the first fe !hapters, yo& !an see that e generay test o&r !o%e at ea!h step by printing some o&tp&t to the s!reen an% !he!-ing that it oo-s ?K. For eampe, in !hapter 2 hen e ere first !a!&ating 4$ !ontent, e &se% a very short test se<&en!e to verify that o&r !o%e or-e% before r&nning it on the rea inp&t. $he reason e &se% a test se<&en!e as that, be!a&se it as so short, e !o&% easiy or- o&t the anser by eye an% !ompare it to the anser given by o&r !o%e. $his i%ea 5 r&nning !o%e on a test inp&t an% !omparing the res&t to an anser t+at e 0no to be correct1 5 is s&!h a &sef& one that #ython has a b&itin too for epressing it: assert. 4n assertion !onsists of the or% assert, fooe% by a !a to o&r f&n!tion, then to e<&as signs, then the res&t that e epe!t2.

1 $hin- of it as simiar to r&nning a positive !ontro in a etab eperiment.

112 Chapter : riting o&r on f&n!tions

For eampe, e -no that if e r&n o&r get_at_content f&n!tion on the D;4 se<&en!e >4$RC> e sho&% get an anser of 0.. $his assertion i test hether thats the !ase:

assert get/at/content(*A)%&*# BB 0.

;oti!e the to e<&as signs 5 e earn the reason behin% that in the net !hapter. $he ay that assertion statements or- is very simpe if an assertion t&rns o&t to be fase i.e. if #ython ee!&tes o&r f&n!tion on the inp&t >4$RC> an% the anser isnt 0.= then the program i stop an% e i get an !ssertionError.

4ssertions are &sef& in a n&mber of ays. $hey provi%e a means for &s to !he!-  hether o&r f&n!tions are or-ing as inten%e% an% therefore hep &s tra!- %on

errors in o&r programs. 'f e get some &nepe!te% o&tp&t from a program that &ses a parti!&ar f&n!tion, an% the assertion tests for that f&n!tion a pass, then e !an be !onfi%ent that the error %oesnt ie in the f&n!tion b&t in the !o%e that !as it. $hey aso et &s mo%ify a f&n!tion an% !he!- that e havent intro%&!e% any errors. 'f e have a f&n!tion that passes a series of assertion tests, an% e ma-e some

!hanges to it, e !an rer&n the assertion tests an%, ass&ming they a pass, be !onfi%ent that e havent bro-en the f&n!tion1.

4ssertions are aso &sef& as a form of %o!&mentation. (y in!&%ing a !oe!tion of assertion tests aongsi%e a f&n!tion, e !an sho ea!ty hat o&tp&t is epe!te% from a given inp&t.

Finay, e !an &se assertions to test the behavio&r of o&r f&n!tion for &n&s&a inp&ts. For eampe, hat is the epe!te% behavio&r of get_at_content hen given a D;4 se<&en!e that in!&%es &n-non bases &s&ay represente% as N=E 4 sensibe ay to han%e &n-non bases o&% be to e!&%e them from the 4$

!ontent !a!&ation 5 in other or%s, the 4$ !ontent for a given se<&en!e sho&%nt

113 Chapter : riting o&r on f&n!tions

be affe!te% by a%%ing a b&n!h of &n-non bases. e !an rite an assertion that epresses this:

assert get/at/content(*A)%&<<<<<<<<<<*# BB 0.

$his assertions fais for the !&rrent version of get_at_content. 7oever, e !an easiy mo%ify the f&n!tion to remove a N !hara!ters before !arrying o&t the

!a!&ation:

def get/at/content(dna, sig/figsB2#! dna B dna.replace(<, #

length B len(dna#

a/count B dna.upper(#.count(A# t/count B dna.upper(#.count()#

at/content B (a/count C t/count#  length return round(at/content, sig/figs#

an% no the assertion passes.

'ts !ommon to gro&p a !oe!tion of assertions for a parti!&ar f&n!tion together to test for the !orre!t behavio&r on %ifferent types of inp&t. 7eres an eampe for get_at_content hi!h shos a range of %ifferent types of behavio&r:

assert get/at/content(*A*# BB 1 assert get/at/content(*%*# BB 0 assert get/at/content(*A)%&*# BB 0. assert get/at/content(*A%%*# BB 0.33 assert get/at/content(*A%%*, 1# BB 0.3 assert get/at/content(*A%%*, # BB 0.33333

 (ecap

'n this !hapter, eve seen ho pa!-aging &p !o%e into f&n!tions heps &s to

11B Chapter : riting o&r on f&n!tions

to %efine an% !a o&r on f&n!tions aong ith vario&s ne ays to s&ppy

arg&ments to f&n!tions. e aso oo-e% at a !o&pe of things that are possibe in #ython, b&t rarey a%visabe 5 riting f&n!tions itho&t arg&ments or ret&rn  va&es. Finay, e epore% the &se of assertions to test o&r f&n!tions, an%

%is!&sse% ho e !an &se them to !at!h errors before they be!ome a probem. $he remaining !hapters in this boo- i ma-e &se of f&n!tions in both the

eampes an% the eer!ise so&tions, so ma-e s&re yo& are !omfortabe ith the ne i%eas from this !hapter before moving on.

11 Chapter : riting o&r on f&n!tions

 !ercises

In document python for biologists (Page 120-124)