Intermediate classes: Difference between revisions

From 22118
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 Unittest]]
|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 be about the making of a class. A the exercises goes on the class can do more and more. Remember to add code outside the class, which test the new methods you make as you make them. The class methods need some decent input control and you need to think about what is required. Notice how versatile the class becomes, as you progress through the exercises.
The first two exercises will continue from last time.
# Create a class called '''Fasta'''. The purpose of the class is to do various kinds of manipulation of fasta files and sequences. Start with adding 3 class methods:<br>'''load(filename)''', which gets a file name/path and loads the fasta file into internal instance lists with header and sequence.<br>'''save(filename)''', which writes the internal instance header/sequence lists into a fasta file.<br>'''content()''', which returns two lists; the headers and the sequences.<br>You can likely reuse part of your functions in exercise 3 and 4 from [[Making Functions]].<br>Example use of the Fasta class:<br><div style="font-family:'Courier New'">myfasta = Fasta()<br>myfasta.load("dna7.fsa")<br>print(myfasta.content())<br>myfasta.save("newfile.fsa")</div>
# 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>&nbsp;&nbsp;&nbsp;&nbsp;for header, sequence in MyFastaInstance:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Do something with header and sequence<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(header, sequence)</div>The function '''len''' returns the number of sequences.<br>br>
# Add a method '''delete([start[,end]])''', which deletes entries in the headers and sequences. If called with no arguments like '''delete()''' it deletes all headers/sequences. If called with one argument like '''delete(start)''' it deletes the header and sequence at position ''start''. If called with two arguments, it deletes headers and sequences from position ''start'' up to but not including position ''end''. ''start'' and ''end'' can also be negative numbers, and in such case we count from the end of the lists, just like it works in normal list manipulations.
# 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>&nbsp;&nbsp;&nbsp;&nbsp;if not MyFastaInstance.verifythis("DNA"):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyFastaInstance.deletethis()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue<br>&nbsp;&nbsp;&nbsp;&nbsp;# 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>
# Modify the '''content()''' method, so it works similar to '''delete()'''. If called with no arguments like '''content()''' it returns 2 lists with all headers and sequences. If called with one argument like '''content(start)''' it returns the header and sequence at position ''start''. If called with two arguments, it returns the headers and sequences from position ''start'' up to but not including position ''end'' as two lists. Be careful that you return a copy of the lists and not the lists themselves as then the headers and sequences in the instance can be modified unintended outside the instance.
# Add a method '''insert(header,sequence[,position])''', which adds header and sequence to the instance lists. ''header'' and ''sequence'' can either be simple strings (single header and sequence) or lists of headers and sequences. If ''position'' is not given, then the addition takes place at the end of the existing headers/sequences. If ''position'' is given then insertion takes places at that position.
# Add a method '''verify(alphabet,[start[,end]])''', which verifies sequence entries according to an alphabet. It works in a way similar to '''delete()''' and '''content()''' for the range of what should be verified. The method returns True if all entries in the range are verified (belonging to the alphabet), False otherwise.<br>The alphabet could be a DNA alphabet (ATCG), a protein alphabet or something derived from those. You should put your alphabets into class variables, as they are common for all instances, and not subject to change.
# Add a method '''discard(alphabet,[start[,end]])''', which discards sequence entries and corresponding headers if they do not match the given alphabet. It works in a way similar to '''delete()''' and '''content()''' for the range of what should be verified.
# 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>&nbsp;&nbsp;&nbsp;&nbsp;for header, sequence in MyFastaInstance:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Do something with header and sequence<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;if not MyFastaInstance.verifythis("DNA"):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyFastaInstance.deletethis()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue<br>&nbsp;&nbsp;&nbsp;&nbsp;# 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.

  1. 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:
        for header, sequence in MyFastaInstance:
            # Do something with header and sequence
            print(header, sequence)
    The function len returns the number of sequences.
    br>
  2. 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:
        if not MyFastaInstance.verifythis("DNA"):
            MyFastaInstance.deletethis()
            continue
        # Do something with header and sequence
    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>

Exercises for extra practice