Commit 27f60a48 authored by Bertrand Néron's avatar Bertrand Néron
Browse files

add code for OOP exercises

parent 5d43aa3e
.. sectnum::
:start: 13
.. _Object_Oriented_Programming:
***************************
Object Oriented Programming
***************************
Exercises
=========
Exercise
--------
Exercise
--------
Exercise
--------
Exercise
--------
\ No newline at end of file
class Sequence(object):
def __init__(self, identifier, comment, seq):
self.id = identifier
self.comment = comment
self.seq = self._clean(seq)
def _clean(self, seq):
"""
:param seq:
:return:
"""
return seq.replace('\n')
def enzyme_filter(self, enzymes):
"""
:param enzymes:
:return:
"""
enzymes_which_binds = []
for enz in enzymes:
if enz.binds(self.seq):
enzymes_which_binds.append(enz)
return
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
def binds(self, seq):
"""
:param seq:
:return:
"""
return self.binding in seq.seq
\ No newline at end of file
class Matrix(object):
def __init__(self, row, col, val=None):
self._row = row
self._col = col
self._matrix = []
for i in range(row):
c = [val] * col
self._matrix.append(c)
def size(self):
return self._row, self._col
def get_cell(self, row, col):
self._check_index(row, col)
return self._matrix[i][j]
def matrix_set(self, row, col, val):
self._check_index(row, col)
self._matrix[row][col] = val
def __str__(self):
s = ''
for i in range(self._row):
s += self._matrix[i]
s += '\n'
return s
def _check_index(self, row, col):
if not (0 < row <= self._row) or not (0 < col <= self._col):
raise IndexError("matrix index out of range")
import math
class Point(object):
"""Class to handle point in a 2 dimensions space"""
def __init__(self, x, y):
"""
:param x: the value on the X-axis
:type x: float
:param y: the value on the Y-axis
:type y: float
"""
self.x = x
self.y = y
def show(self):
"""
:return: the coordinate of this point
:rtype: a tuple of 2 elements (float, float)
"""
return (self.x, self.y)
def move(self, x, y):
"""
:param x: the value to move on the X-axis
:type x: float
:param y: the value to move on the Y-axis
:type y: float
"""
self.x += x
self.y += y
def dist(self, pt):
"""
:param pt: the point to compute the distance with
:type pt: :class:`Point` object
:return: the distance between this point ant pt
:rtype: int
"""
dx = pt.x - self.x
dy = pt.y - self.y
return math.sqrt(dx ** 2 + dy ** 2)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment