Update S6: Object Oriented Programming authored by Gregorio's avatar Gregorio
...@@ -173,7 +173,7 @@ The **methods** are the **actions** that the objects can perform. The first meth ...@@ -173,7 +173,7 @@ The **methods** are the **actions** that the objects can perform. The first meth
class Seq: class Seq:
"""A class for representing sequences""" """A class for representing sequences"""
def __init__(self): def __init__(self):
print("New sequence created!") # I would not consider a good practice printing here but... print("New sequence created!") # It's not good practice to print here but let's make an exception!
# Main program # Main program
...@@ -192,11 +192,11 @@ Testing.... ...@@ -192,11 +192,11 @@ Testing....
``` ```
* Execute it **step by step**, using the **step over** command * Execute it **step by step**, using the **step over** command
* Execute it **step by step** using the **step into** command every time. Check that the debugger **enters** into the **Class** and execute the\*\* \__init_\_\*\* method * Execute it **step by step** using the **step into** command every time. Check that the debugger **enters** into the **Class** and execute the **\__init_\_** method
### 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 objects. To represent 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 object. 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**.
...@@ -204,7 +204,6 @@ Let's modify the \__init_\_ method to include a new parameter: the string that r ...@@ -204,7 +204,6 @@ Let's modify the \__init_\_ method to include a new parameter: the string that r
class Seq: class Seq:
"""A class for representing sequences""" """A class for representing sequences"""
def __init__(self, strbases): def __init__(self, strbases):
# Initialize the sequence with the value # Initialize the sequence with the value
# passed as argument when creating the object # passed as argument when creating the object
self.strbases = strbases self.strbases = strbases
...@@ -232,16 +231,13 @@ class Seq: ...@@ -232,16 +231,13 @@ class Seq:
"""A class for representing sequences""" """A class for representing sequences"""
def __init__(self, strbases): def __init__(self, strbases):
# Initialize the sequence with the value # Initialize the sequence with the value
# passed as argument when creating the object # passed as argument when creating the object
self.strbases = strbases self.strbases = strbases
print("New sequence created!") print("New sequence created!")
def __str__(self): def __str__(self):
"""Method called when the object is being printed""" """Method called when the object is being printed"""
# -- We just return the string with the sequence # -- We just return the string with the sequence
return self.strbases return self.strbases
...@@ -277,16 +273,13 @@ class Seq: ...@@ -277,16 +273,13 @@ class Seq:
"""A class for representing sequences""" """A class for representing sequences"""
def __init__(self, strbases): def __init__(self, strbases):
# Initialize the sequence with the value # Initialize the sequence with the value
# passed as argument when creating the object # passed as argument when creating the object
self.strbases = strbases self.strbases = strbases
print("New sequence created!") print("New sequence created!")
def __str__(self): def __str__(self):
"""Method called when the object is being printed""" """Method called when the object is being printed"""
# -- We just return the string with the sequence # -- We just return the string with the sequence
return self.strbases return self.strbases
...@@ -321,7 +314,7 @@ Sequence 2: CGTAAC ...@@ -321,7 +314,7 @@ Sequence 2: CGTAAC
### Inheritance ### Inheritance
New classes can be **derived** from others, reusing their methods (inherited from the parent) and adding new ones. This is called **inheritance**. Just to present the concepts, let's create the **Gene class**, that derives (inherits) from Seq. It will not add anything. New classes can be **derived** from others, reusing their methods (inherited from the parent) and adding new ones. This is called **inheritance**. Just to present the concepts, let's create the **Gene class**, that derives (inherits) from Seq. It won't add anything.
```python3 ```python3
class Gene(Seq): class Gene(Seq):
...@@ -415,7 +408,6 @@ class Gene(Seq): ...@@ -415,7 +408,6 @@ class Gene(Seq):
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 initializer and then the # -- Call first the Seq initializer and then the
# -- Gene init method # -- Gene init method
super().__init__(strbases) super().__init__(strbases)
...@@ -461,7 +453,7 @@ This means that there is a **sub-class** that is overriding this method. And tha ...@@ -461,7 +453,7 @@ This means that there is a **sub-class** that is overriding this method. And tha
## Installing libraries: termcolor ## Installing libraries: termcolor
There are many **python libraries** available for you to use. You only have to **install** them. As an example of how to do it, let's install the [termcolor](https://pypi.org/project/termcolor/) library. It will allow us to **print messages in color** in the **console**. There are many **Python libraries** available for you to use. You only have to **install** them. As an example of how to do it, let's install the [termcolor](https://pypi.org/project/termcolor/) library. It will allow us to **print messages in color** in the **console**.
Create a **new python file** in the **S06** folder and call it **test-color-py**. Write this code: Create a **new python file** in the **S06** folder and call it **test-color-py**. Write this code:
...@@ -485,7 +477,7 @@ Now **run** the program. You will see a green message: ...@@ -485,7 +477,7 @@ Now **run** the program. You will see a green message:
![](https://gitlab.etsit.urjc.es/rperez/pne-wiki/-/raw/master/s6-OOP/libraries-03.png) ![](https://gitlab.etsit.urjc.es/rperez/pne-wiki/-/raw/master/s6-OOP/libraries-03.png)
You now have the power of printing in colors...use it wisely :) You now have the power of printing in colors... remember to use it wisely :)
## Exercises ## Exercises
...@@ -495,7 +487,7 @@ Let's practice with classes and objects!. If you have not done it yet, create th ...@@ -495,7 +487,7 @@ Let's practice with classes and objects!. If you have not done it yet, create th
### Exercise 1 ### Exercise 1
The current **Seq class** created as a example in this session does not check if the given string of bases is **valid**. Therefore, if we execute the following code in the the main program: The current **Seq class** created as an example in this session does not check if the given string of bases is **valid**. Therefore, if we execute the following code in the main program:
```python3 ```python3
s1 = Seq("ACCTGC") s1 = Seq("ACCTGC")
...@@ -550,7 +542,7 @@ Sequence 2: (Length: 6) CAGATA ...@@ -550,7 +542,7 @@ Sequence 2: (Length: 6) CAGATA
We need to develop some **functions** to create sequences for **testing** the Seq objects. For instance, the function **generate_seqs(pattern, number)**, that has two parameters, will create a **list** with the provided _number_ of sequences. All sequences are created from the provided **pattern**. This pattern is a string of one or more valid bases. The first sequence of the list will be the pattern, the second will consist in the pattern repeated twice, in the third, the pattern will be repeated three times, and so on. We need to develop some **functions** to create sequences for **testing** the Seq objects. For instance, the function **generate_seqs(pattern, number)**, that has two parameters, will create a **list** with the provided _number_ of sequences. All sequences are created from the provided **pattern**. This pattern is a string of one or more valid bases. The first sequence of the list will be the pattern, the second will consist in the pattern repeated twice, in the third, the pattern will be repeated three times, and so on.
Therefore, if we call the function generate_seqs() with the parameters ("A", 3), a list of 3 sequences is returned. The bases in every sequence will be: "A", "AA" and "AAA" Therefore, if we call the function generate_seqs() with the parameters ("A", 3), a list of 3 sequences is returned. The bases in every sequence will be: "A", "AA" and "AAA".
* **Filename**: S06/e3.py * **Filename**: S06/e3.py
* **Description**: Implement the generate_seqs() function. Test the function with this main program: * **Description**: Implement the generate_seqs() function. Test the function with this main program:
...@@ -607,7 +599,7 @@ You should modify the **print_seqs()** function for including an additional para ...@@ -607,7 +599,7 @@ You should modify the **print_seqs()** function for including an additional para
The session is finished. Make sure, during this week, that everything in this list is checked! The session is finished. Make sure, during this week, that everything in this list is checked!
* [ ] You have all the items of the session 5 checked! * [ ] You have all the items of the session 5 checked!
* [ ] Your working repo contains the **Session-06 Folder** with the following files: * [ ] Your working repository contains the **Session-06 Folder** with the following files:
* [ ] test-01.py * [ ] test-01.py
* [ ] seq-01.py * [ ] seq-01.py
* [ ] test-color.py * [ ] test-color.py
... ...
......