diff --git a/source/Object_Oriented_Programming.rst b/source/Object_Oriented_Programming.rst index 1e76016b67c8300c91b54a9392411ef9f418e0ef..a35c32c2ac97621ff9935d8dbd9874b60370b389 100644 --- a/source/Object_Oriented_Programming.rst +++ b/source/Object_Oriented_Programming.rst @@ -8,19 +8,145 @@ Object Oriented Programming *************************** + Exercises ========= +Exercise +-------- + +Modelize a sequence with few attributes and methods + +.. literalinclude:: _static/code/sequence.py + :linenos: + :language: python + +:download:`sequence.py <_static/code/sequence.py>` . + Exercise -------- +Instanciate 2 sequences using your Sequence class, and draw schema representing the namespaces + +.. image:: _static/figs/sequence_namespace.png + :alt: sequence namespace + :align: left + :height: 400px + + +.. container:: clearer + + .. image :: _static/figs/spacer.png + Exercise -------- +Can you explain this result (draw namespaces to explain) ? +how to modify the class variable *class_attr* + +.. literalinclude:: _static/code/class_attribute.py + :linenos: + :language: python + +:download:`class_attribute.py <_static/code/class_attribute.py>` . + Exercise -------- +Write the definition of a Point class. Objects from this class should have a + + * a method **show** to display the coordinates of the point + * a method **move** to change these coordinates. + * a method **dist** that computes the distance between 2 points. + +.. note:: + the distance between 2 points A(x0, y0) and B(x1, y1) can be compute + + + .. math:: + d(AB) = \sqrt{(x1-x0))^2 + (y1-y0)^2} + + (http://www.mathwarehouse.com/algebra/distance_formula/index.php) + + +The following python code provides an example of the expected behaviour of objects belonging to this class: :: + + >>> p1 = Point(2, 3) + >>> p2 = Point(3, 3) + >>> p1.show() + (2, 3) + >>> p2.show() + (3, 3) + >>> p1.move(10, -10) + >>> p1.show() + (12, -7) + >>> p2.show() + (3, 3) + >>> p1.dist(p2) + 1.0 + + +.. literalinclude:: _static/code/point.py + :linenos: + :language: python + +:download:`point.py <_static/code/point.py>` . Exercise --------- \ No newline at end of file +-------- + +Use OOP to modelize restriction enzyme, and sequences. + +the sequence must implement the following methods + + * enzyme_filter which take as a list of enzymes as argument and return a **new** list containing the enzymes which have + binding site in sequence + +the restriction enzyme must implements the following methods + + * binds which take a sequence as argument and return True if the sequence contains a binding site, False otherwise. + +solve the exercise :ref:`enzyme_exercise` using this new implementation. + + +.. literalinclude:: _static/code/enzyme.py + :linenos: + :language: python + +:download:`enzyme.py <_static/code/enzyme.py>` . + +Exercise +-------- + +refactor your code of :ref:`matrix_exercise` in OOP style programming. implements only + + * **size**: return the number of rows, and number of columns + * **get_cell**: that take the number of rows, the number of columns as parameters, + and returns the content of cell corresponding to row number col number + * **set_cell**: that take the number of rows, the number of columns as parameters, and a value + and set the value val in cell specified by row number x column number + * **to_str**: return a string representation of the matrix + * **mult**: that take a scalar and return a new matrix which is the scalar product of matrix x val + +you can change the name of the methods to be more pythonic + +.. literalinclude:: _static/code/matrix_obj.py + :linenos: + :language: python + +:download:`matrix_obj.py <_static/code/matrix_obj.py>` . + +Exercise +-------- + +Use the code to read multiple sequences fasta file in procedural style and refactor it in OOP style. +use the file :download:`abcd.fasta <_static/data/abcd.fasta>` to test your code. + +What is the benefit to use oop style instead of procedural style? + +.. literalinclude:: _static/code/fasta_object.py + :linenos: + :language: python + +:download:`fasta_object.py <_static/code/fasta_object.py>` . \ No newline at end of file diff --git a/source/_static/code/class_attribute.py b/source/_static/code/class_attribute.py new file mode 100644 index 0000000000000000000000000000000000000000..02b38fc4202c1112ea5cd6b7b738ae58efc1ef42 --- /dev/null +++ b/source/_static/code/class_attribute.py @@ -0,0 +1,30 @@ +class MyClass(object): + + class_attr = 'foo' + + def __init__(self, val): + self.inst_attr = val + + + + +a = MyClass(1) +b = MyClass(2) + +print a.inst_attr +1 +print b.inst_attr +2 + +print a.class_attr == b.class_attr +True +print a.class_attr is b.class_attr +True + +b.class_attr = 4 + +print a.class_attr +4 +del a.class_attr + +MyClass.class_attr = 4 \ No newline at end of file diff --git a/source/_static/code/enzyme.py b/source/_static/code/enzyme.py index ea839f4da832e9f71fdb16c67909bb1896c29c38..c104537dc89b3a033c259825a2cbf0a4b70611a1 100644 --- a/source/_static/code/enzyme.py +++ b/source/_static/code/enzyme.py @@ -31,12 +31,15 @@ class Sequence(object): class RestrictionEnzyme(object): def __init__(self, name, binding, cut, end, comment=''): - self.name = name - self.binding = binding - self.cut = cut - self.end = end - self.comment = comment - + self._name = name + self._binding = binding + self._cut = cut + self._end = end + self._comment = comment + + @property + def name(self): + return self._name def binds(self, seq): """ diff --git a/source/_static/code/sequence.py b/source/_static/code/sequence.py new file mode 100644 index 0000000000000000000000000000000000000000..06017f983c241a03af4d9907435a920814ff1fb4 --- /dev/null +++ b/source/_static/code/sequence.py @@ -0,0 +1,31 @@ +class Sequence(object): + + def __init__(self, identifier, comment, seq): + self.id = identifier + self.comment = comment + self.seq = self._clean(seq) + + + def _clean(self, seq): + """ + remove newline from the string representing the sequence + :param seq: the string to clean + :return: the string without '\n' + :rtype: string + """ + return seq.replace('\n') + + + def gc_percent(self): + """ + :return: the gc ratio + :rtype: float + """ + seq = self.seq.upper() + return float(seq.count('G') + seq.count('C')) / len(seq) + + + + +dna1 = Sequence('gi214', 'the first sequence', 'tcgcgcaacgtcgcctacatctcaagattca') +dna2 = Sequence('gi3421', 'the second sequence', 'gagcatgagcggaattctgcatagcgcaagaatgcggc') \ No newline at end of file diff --git a/source/_static/figs/sequence_namespace.png b/source/_static/figs/sequence_namespace.png new file mode 100644 index 0000000000000000000000000000000000000000..c49265792c1fa3de974ae409e804973214e7502d Binary files /dev/null and b/source/_static/figs/sequence_namespace.png differ diff --git a/source/index.rst b/source/index.rst index 76f715f18f537bfdcac830d69ae0cf167b766be1..c16bd97902f646b5a07d3268923fef7871c2470b 100644 --- a/source/index.rst +++ b/source/index.rst @@ -17,7 +17,7 @@ Contents: Dive_into_Functions Modules_and_Packages Input_Output - + Object_Oriented_Programming Indices and tables ==================