Intermediate classes: Difference between revisions
Jump to navigation
Jump to search
(Created page with "__NOTOC__ {| width=500 style="font-size: 10px; float:right; margin-left: 10px; margin-top: -56px;" |Previous: Beginning classes |Next: Beginning Unittest |} == Required course material for the lesson == Powerpoint: [https://teaching.healthtech.dtu.dk/material/22118/22118_07-Classes.ppt Classes]<br> Resource: Example code - Classes<br> == Subjects covered == Classes<br> Object Oriented Programming == Exercises to be handed in == The exercises will be about...") |
mNo edit summary |
||
| Line 2: | Line 2: | ||
{| width=500 style="font-size: 10px; float:right; margin-left: 10px; margin-top: -56px;" | {| width=500 style="font-size: 10px; float:right; margin-left: 10px; margin-top: -56px;" | ||
|Previous: [[Beginning classes]] | |Previous: [[Beginning classes]] | ||
|Next: [[Beginning | |Next: [[Beginning unit test]] | ||
|} | |} | ||
== Required course material for the lesson == | == Required course material for the lesson == | ||
| Line 13: | Line 13: | ||
== Exercises to be handed in == | == Exercises to be handed in == | ||
The exercises will | The first two exercises will continue from last time. | ||
# Add iteration and length evaluation (__len__ magic method) to the '''Fasta''' class using magic methods. Do this so you can write code like <div style="font-family:'Courier New'">if len(MyFastaInstance) > 0:<br> for header, sequence in MyFastaInstance:<br> # Do something with header and sequence<br> print(header, sequence)</div>The function '''len''' returns the number of sequences.<br>br> | |||
# Add the methods '''deletethis()''', '''insertthis(header, sequence)''', '''verifythis(alphabet)''' and '''discardthis(alphabet)''' to the '''Fasta''' class. The methods should only work when ''iterating over an instance at the current item'', i.e. they work when you are iterating over the fasta sequences on the ''current'' sequence and header, like this:<div style="font-family:'Courier New'">for header, sequence in MyFastaInstance:<br> if not MyFastaInstance.verifythis("DNA"):<br> MyFastaInstance.deletethis()<br> continue<br> # Do something with header and sequence</div>As some may remember, it is normally impossible to successfully iterate straightforward through a list and delete and/or add elements to the list during the iteration. You have to make this possible, maybe by changing the way your iteration works in the previous exercise.<br>br> | |||
# Add iteration and length evaluation (__len__ magic method) to the '''Fasta''' class using magic methods. Do this so you can write code like <div style="font-family:'Courier New'">if len(MyFastaInstance) > 0:<br> for header, sequence in MyFastaInstance:<br> # Do something with header and sequence<br> print(header, sequence)</div>The function '''len''' returns the number of sequences. | |||
# Add the methods '''deletethis()''', '''insertthis(header, sequence)''', '''verifythis(alphabet)''' and '''discardthis(alphabet)''' to the '''Fasta''' class. The methods should only work when ''iterating over an instance at the current item'', i.e. they work when you are iterating over the fasta sequences on the ''current'' sequence and header, like this:<div style="font-family:'Courier New'">for header, sequence in MyFastaInstance:<br> if not MyFastaInstance.verifythis("DNA"):<br> MyFastaInstance.deletethis()<br> continue<br> # Do something with header and sequence</div>As some may remember, it is normally impossible to successfully iterate straightforward through a list and delete and/or add elements to the list during the iteration. You have to make this possible, maybe by changing the way your iteration works in the previous exercise. | |||
== Exercises for extra practice == | == Exercises for extra practice == | ||
Revision as of 13:33, 6 March 2026
| Previous: Beginning classes | Next: Beginning unit test |
Required course material for the lesson
Powerpoint: Classes
Resource: Example code - Classes
Subjects covered
Classes
Object Oriented Programming
Exercises to be handed in
The first two exercises will continue from last time.
- Add iteration and length evaluation (__len__ magic method) to the Fasta class using magic methods. Do this so you can write code like if len(MyFastaInstance) > 0:The function len returns the number of sequences.
for header, sequence in MyFastaInstance:
# Do something with header and sequence
print(header, sequence)
br> - Add the methods deletethis(), insertthis(header, sequence), verifythis(alphabet) and discardthis(alphabet) to the Fasta class. The methods should only work when iterating over an instance at the current item, i.e. they work when you are iterating over the fasta sequences on the current sequence and header, like this:for header, sequence in MyFastaInstance:As some may remember, it is normally impossible to successfully iterate straightforward through a list and delete and/or add elements to the list during the iteration. You have to make this possible, maybe by changing the way your iteration works in the previous exercise.
if not MyFastaInstance.verifythis("DNA"):
MyFastaInstance.deletethis()
continue
# Do something with header and sequence
br>