Update S6: Object Oriented Programming authored by Gregorio's avatar Gregorio
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# Session 6: Object Oriented Programming # Session 6: Object Oriented Programming
* **Time**: 2h * **Duration**: 2h
* **Goals**: * **Goals**:
* Understand how Classes (new concept to acquire) work * Understand how Classes (new concept to acquire) work
* Create our first Class * Create our first Class
...@@ -89,7 +89,7 @@ Imagine now that we define a new sequence, but we make a mistake: ...@@ -89,7 +89,7 @@ Imagine now that we define a new sequence, but we make a mistake:
```python3 ```python3
from Seq0 import * from Seq0 import *
# -- This sequence is invalid, as it as characteres # -- This sequence is not valid, as it has characters
# -- different than the 4 bases: A,T,C,G # -- different than the 4 bases: A,T,C,G
seq1 = "ATTXMMNXCCCGGGG" seq1 = "ATTXMMNXCCCGGGG"
...@@ -196,9 +196,9 @@ Testing.... ...@@ -196,9 +196,9 @@ Testing....
### Adding data: attribute strbases ### Adding data: attribute strbases
For representing a **sequence** we will use a **string** that will be stored in every object. We will call this string **strbases**. As mentioned earlier, data stored in the objects are called **attributes**, as they describe the objetcts. For representing a **sequence** we will use a **string** that will be stored in every object. We will call this string **strbases**. As mentioned earlier, data stored in the objects are called **attributes**, as they describe the objects.
Let's modify the \__init_\_ method to include a new parameter: the string that represents the sequence of the objetc. That parameter will be stored in the object attribute: **self.strbases**. Let's modify the \__init_\_ method to include a new parameter: the string that represents the sequence of the object. That parameter will be stored in the object attribute: **self.strbases**.
```python ```python
class Seq: class Seq:
...@@ -326,7 +326,7 @@ New classes can be **derived** from others, reusing their methods (inherited fro ...@@ -326,7 +326,7 @@ New classes can be **derived** from others, reusing their methods (inherited fro
```python3 ```python3
class Gene(Seq): class Gene(Seq):
"""This class is derived from the Seq Class """This class is derived from the Seq Class
All the objects of class Gene will inherite All the objects of class Gene will inherit
the methods from the Seq class the methods from the Seq class
""" """
pass pass
...@@ -366,12 +366,12 @@ Let's create a new \__init_\_ file method that includes a new optional parameter ...@@ -366,12 +366,12 @@ Let's create a new \__init_\_ file method that includes a new optional parameter
```python3 ```python3
class Gene(Seq): class Gene(Seq):
"""This class is derived from the Seq Class """This class is derived from the Seq Class
All the objects of class Gene will inheritate All the objects of class Gene will inherit
the methods from the Seq class the methods from the Seq class
""" """
def __init__(self, strbases, name=""): def __init__(self, strbases, name=""):
# -- Call first the Seq initilizer and then the # -- Call first the Seq initializer and then the
# -- Gene init method # -- Gene init method
super().__init__(strbases) super().__init__(strbases)
self.name = name self.name = name
...@@ -411,12 +411,12 @@ In the Gene class, we can either create **new methods** or **re-implement** meth ...@@ -411,12 +411,12 @@ In the Gene class, we can either create **new methods** or **re-implement** meth
```python3 ```python3
class Gene(Seq): class Gene(Seq):
"""This class is derived from the Seq Class """This class is derived from the Seq Class
All the objects of class Gene will inheritate All the objects of class Gene will inherit
the methods from the Seq class the methods from the Seq class
""" """
def __init__(self, strbases, name=""): def __init__(self, strbases, name=""):
# -- Call first the Seq initilizer and then the # -- Call first the Seq initializer and then the
# -- Gene init method # -- Gene init method
super().__init__(strbases) super().__init__(strbases)
self.name = name self.name = name
...@@ -471,7 +471,7 @@ import termcolor ...@@ -471,7 +471,7 @@ import termcolor
termcolor.cprint("Hey! this is printed in green!", 'green') termcolor.cprint("Hey! this is printed in green!", 'green')
``` ```
You will see that the word **termcolor** is underlined in **red**. This is because Pycharm does not know anything about the termcolor module. You will see that the word **termcolor** is underlined in **red**. This is because PyCharm does not know anything about the termcolor module.
![](https://gitlab.etsit.urjc.es/rperez/pne-wiki/-/raw/master/s6-OOP/libraries-02.png) ![](https://gitlab.etsit.urjc.es/rperez/pne-wiki/-/raw/master/s6-OOP/libraries-02.png)
...@@ -514,7 +514,7 @@ New sequence created! ...@@ -514,7 +514,7 @@ New sequence created!
The goal of this exercise is to detect **incorrect sequences**. The goal of this exercise is to detect **incorrect sequences**.
* **Filename**: S06/e1.py * **Filename**: S06/e1.py
* **Description**: Modify the \___ini__\_ method of the Seq class so that it detects whether the given string only contains valid bases ('A', 'C', 'G' and 'T'). If a different character is found, the sequence should be initialized with the **"ERROR"** string, and the message **"INCORRECT Sequence detected"** should be printed in the console. * **Description**: Modify the \___init__\_ method of the Seq class so that it detects whether the given string only contains valid bases ('A', 'C', 'G' and 'T'). If a different character is found, the sequence should be initialized with the **"ERROR"** string, and the message **"INCORRECT Sequence detected"** should be printed in the console.
When the previous **main program** is executed, this is what should be printed on the console: When the previous **main program** is executed, this is what should be printed on the console:
...@@ -598,7 +598,7 @@ Let's play with the **termcolor** module. Modify exercise 3 for printing both li ...@@ -598,7 +598,7 @@ Let's play with the **termcolor** module. Modify exercise 3 for printing both li
You should modify the **print_seqs()** function for including an additional parameter: the color. You should modify the **print_seqs()** function for including an additional parameter: the color.
* **Filename**: S06/e4.py * **Filename**: S06/e4.py
* **Description**: The same output than e3.py, but in colors. This is how it should looks like: * **Description**: The same output than for e3.py, but in colors. This is how it should looks like:
![](https://gitlab.etsit.urjc.es/rperez/pne-wiki/-/raw/master/s6-OOP/ex4-01.png) ![](https://gitlab.etsit.urjc.es/rperez/pne-wiki/-/raw/master/s6-OOP/ex4-01.png)
...@@ -615,7 +615,7 @@ The session is finished. Make sure, during this week, that everything in this li ...@@ -615,7 +615,7 @@ The session is finished. Make sure, during this week, that everything in this li
* [ ] e2.py * [ ] e2.py
* [ ] e3.py * [ ] e3.py
* [ ] e4.py * [ ] e4.py
* [ ] All the previous files have been pushed to your remote Gitlab repo * [ ] All the previous files have been pushed to your remote GitLab repository
# Credits # Credits
...@@ -628,4 +628,4 @@ The session is finished. Make sure, during this week, that everything in this li ...@@ -628,4 +628,4 @@ The session is finished. Make sure, during this week, that everything in this li
# Links # Links
* [Universidad Rey Juan Carlos de Madrid](https://www.urjc.es/) * [Universidad Rey Juan Carlos de Madrid](https://www.urjc.es/)
* [Escuela Técnica Superior de Ingeniería de Telecomunicaciones (URJC)](https://www.urjc.es/universidad/facultades/escuela-tecnica-superior-de-ingenieria-de-las-telecomunicaciones/content/etsit-escuela-tecnica-superior-de-ingenieria-de-telecomunicacion) * [Escuela de Ingeniería de Fuenlabrada (URJC)](https://www.urjc.es/eif)
\ No newline at end of file \ No newline at end of file