Thursday, January 30, 2014

week 4

I feel extremely overwhelmed by A1, glad I have great partner by my side. it take me a really long time knowing what the model actually is. I started with a dictionary and was building the methods. I haven't yet come to the recursion part. But, this is gonna take a while.

As for classes this week, I feel like the biggest difficulty of recursion for me, is that, though i can understand them step by step, i could not write the code on my own. I hope though intense exercise, I could overcome this problem. Turtle is great fun. Methods: colour, setheading(left/right), forward/backward, speed, hideturtle, clone(). Notably, every recursion should have a base case as how to exit, or would result in an endless cycle.

I don't have a partner for lab this week, so i get to do everything on my own, but it's not actually a bad experience. I get to organize my thoughts and plan everything step independently. I learn how to manipulate a subclass by inheriting specific attribute from parent class.
class Car(Motorized):
    def Car(self):
        Motorized.__init__(self,*,*,*)

In addition, this is the first time i ever trying writing unittest. just for future reference:
import unitest

class TestSpecficClass(unittest.Testcase):
    def setUp(self):
        #create a specific object of the class
        self.classname = Class(**)
     
   def tearDown(self):
     
  def test_something(self):
       self.aseertEqual(call, expected value, error message)
       self.assertRaises(CertainError, call the function, give the separate parameter)
       self.assertTrue(usually take one parameter)

if __name__=="__main__":
   unittest.main(exit=False)



 
NOTE:
1.exception hierarchy
       ValueError
 |      Exception
 |      BaseException
 |      object
2. property function

3. print function

for i in a:
    print(i,end=" ")
dsf dsf
for i in a:
    print(i,end="")
dsfdsf

Question:
now does super works?
 the call super(self.__class__,self) is going to return the superclass of certain class

Wednesday, January 22, 2014

Object-oriented programming

We learnt about hierarchy system in computer science this week. Guess it's everywhere.

Subclass is a great way to start a new class based on some features in existing class. It inherits everything from the parent class. In the meanwhile, new methods can be added and inherited method can be edited.
Every class starts with a __init__ method. And every method starts with the parameter "self", note that we need to put self's type in Quotation Mark. We can also set the a parameter's default value. e.g. data=None. But if we also want to include its type. We put it this way: data: list = None.

In today's class, one of the major issue we discussed is the class __repr__ method, which returned a string that can be used as an instance of the class. We can also the format method to generate the same result: return "Stack({})".format(self._data). Don't forget to put quotation mark because format can only be implemented on str.

Now, let's move on to Exception. This kind of confuse me for quite some time. The example given in class can be also viewed as a hierarchy system where Exception > SpecialException > ExtremeException. Once I put down in a circle graph, I understand better. Because ExtremeException is a subset of SpecialException, together with the fact that the "try, except, except. except" implement in the given sequence. When ExtremeException is raised, it can be caught in SpecialException. Also note that in Exception, the names follow CapitalizedWords rule. 

In a addition, from the notes, we can combine two classes into a master class by class AB(A,B) where A has priority.

BTW one of the problems i had in today's lab is that, keep in mind. every function should have return statement. Though pop method did return something, only when return it can it be shown.

Cheers!