diff --git a/src/additions/test_generators.py b/src/additions/test_generators.py index 6b2f2736..e928faee 100644 --- a/src/additions/test_generators.py +++ b/src/additions/test_generators.py @@ -1,5 +1,5 @@ -"""Generators. +"""Generators. @see: https://www.learnpython.org/en/Generators Generators are used to create iterators, but with a different approach. Generators are simple diff --git a/src/classes/test_inheritance.py b/src/classes/test_inheritance.py index 5884da2c..d61d9602 100644 --- a/src/classes/test_inheritance.py +++ b/src/classes/test_inheritance.py @@ -6,7 +6,6 @@ of the same code, inheritance allows a derived class to reuse the same code and modify accordingly """ - # pylint: disable=too-few-public-methods class Person: """Example of the base class""" @@ -38,7 +37,7 @@ class Employee(Person): in the global scope.) """ def __init__(self, name, staff_id): - Person.__init__(self, name) + super().__init__(name) # You may also use super() here in order to avoid explicit using of parent class name: # >>> super().__init__(name) self.staff_id = staff_id @@ -47,7 +46,10 @@ def get_full_id(self): """Get full employee id""" return self.get_name() + ', ' + self.staff_id - +class Manager(Employee): + def __init__(self, name, staff_id, department ): + super().__init__(name, staff_id) + self.department = department def test_inheritance(): """Inheritance.""" @@ -55,12 +57,15 @@ def test_inheritance(): # new instance of the class. Method references are resolved as follows: the corresponding class # attribute is searched, descending down the chain of base classes if necessary, and the method # reference is valid if this yields a function object. - person = Person('Bill') - employee = Employee('John', 'A23') + person = Person('Jack') + employee = Employee('John', 'A1') + manager = Manager('Ali', 'A3', 'Finance') - assert person.get_name() == 'Bill' + assert person.get_name() == 'Jack' assert employee.get_name() == 'John' - assert employee.get_full_id() == 'John, A23' + assert manager.get_name() == 'Ali' + employee.name = 'Bill' + assert employee.get_full_id() == 'Bill, A1' # Python has two built-in functions that work with inheritance: # @@ -76,6 +81,7 @@ def test_inheritance(): assert isinstance(person, Person) assert isinstance(employee, Person) + assert not isinstance(employee, Manager) assert issubclass(Employee, Person) assert not issubclass(Person, Employee) diff --git a/src/classes/test_instance_objects.py b/src/classes/test_instance_objects.py index e58dc343..65a83cda 100644 --- a/src/classes/test_instance_objects.py +++ b/src/classes/test_instance_objects.py @@ -26,5 +26,8 @@ class DummyClass: # pylint: disable=attribute-defined-outside-init dummy_instance.temporary_attribute = 1 + dummy_instance.name = "Dummy" assert dummy_instance.temporary_attribute == 1 + assert dummy_instance.name == "Dummy" del dummy_instance.temporary_attribute + \ No newline at end of file diff --git a/src/classes/test_method_objects.py b/src/classes/test_method_objects.py index 1d4e66a9..c7b8063c 100644 --- a/src/classes/test_method_objects.py +++ b/src/classes/test_method_objects.py @@ -55,6 +55,7 @@ def test_method_objects(): # calling a method with a list of n arguments is equivalent to calling the corresponding # function with an argument list that is created by inserting the method’s instance object # before the first argument. - - assert counter.get_counter() == 10 - assert MyCounter.get_counter(counter) == 10 + counter.increment_counter() + new_counter = MyCounter() + assert counter.get_counter() == 11 + assert MyCounter.get_counter(new_counter) == 10 diff --git a/src/classes/test_multiple_inheritance.py b/src/classes/test_multiple_inheritance.py index e3d41529..5b6f9779 100644 --- a/src/classes/test_multiple_inheritance.py +++ b/src/classes/test_multiple_inheritance.py @@ -9,13 +9,19 @@ def test_multiple_inheritance(): """Multiple Inheritance""" - + class Parent: + temp="Dummy variable" # pylint: disable=too-few-public-methods - class Clock: + + class Clock(Parent): """Clock class""" time = '11:23 PM' + def get_parent_data(self): + self.temp = "Parent from clock class" + return self.temp + def get_time(self): """Get current time @@ -24,11 +30,15 @@ def get_time(self): return self.time # pylint: disable=too-few-public-methods - class Calendar: + class Calendar(Parent): """Calendar class""" date = '12/08/2018' + def get_parent_data(self): + self.temp = "Parent from calender class" + return self.temp + def get_date(self): """Get current date @@ -63,6 +73,23 @@ class where there is an overlap in the hierarchy. Thus, if an attribute is not f """ calendar_clock = CalendarClock() - assert calendar_clock.get_date() == '12/08/2018' assert calendar_clock.get_time() == '11:23 PM' + assert calendar_clock.get_parent_data() == 'Parent from clock class' + + class Animal: + def print_nature(self): + return 'I am an animal' + + class LandHabitant(): + def print_charateristics(self): + return 'I am a LandHabitant creation' + + + class Tiger(Animal, LandHabitant): + pass + + tiger = Tiger() + + assert tiger.print_nature() == 'I am an animal' + assert tiger.print_charateristics() == 'I am a LandHabitant creation' diff --git a/src/classes/test_practice_class_definition_and_objects.py b/src/classes/test_practice_class_definition_and_objects.py new file mode 100644 index 00000000..174e51e9 --- /dev/null +++ b/src/classes/test_practice_class_definition_and_objects.py @@ -0,0 +1,23 @@ +""" +Practing the Class Definition and objects +""" + + +def test_class_and_instance_variable(): + """ Testing class definition """ + class Car: + """Car class example""" + # pylint: disbale= too-few-public-methods + name = 'Sports Car' + + def __init__(self, owner_name, model): + self.owner_name = owner_name + self.model = model + + assert Car.name == 'Sports Car' + assert Car.__doc__ == 'Car class example' + + car1 = Car('Ali',2019) + + assert car1.name, car1.model == ('Ali',2019) + \ No newline at end of file diff --git a/src/classes/test_practice_classes.py b/src/classes/test_practice_classes.py new file mode 100644 index 00000000..df128957 --- /dev/null +++ b/src/classes/test_practice_classes.py @@ -0,0 +1,74 @@ +""" +Practing the classes tasks +""" + + +def test_class_and_instance_variable(): + """ Testing class and instance variable """ + class Car: + """ Car class example""" + # pylint: disbale= too-few-public-methods + name = 'Sports Car' + + def __init__(self, owner_name): + self.owner_name = owner_name + + car1 = Car('Ali') + car2 = Car('Hamza') + + assert car1.name == "Sports Car" + assert car2.name == "Sports Car" + + assert car1.owner_name == 'Ali' + assert car2.owner_name == "Hamza" + + Car.name = 'MiniVan' + + assert car1.name == 'MiniVan' + car3 = Car('Raza') + assert car3.name == 'MiniVan' + + + class CarWithSharedFeatures: + """ Car class example with wrong shared variable usage""" + features = [] + + def __init__(self, owner_name): + self.name = owner_name # Instance variable unique to each instance. + + def add_feature(self, feature): + """Add feature to the car + This function illustrate mistaken use of mutable class variable features. + """ + self.features.append(feature) + + car1 = CarWithSharedFeatures('Ali') + car2 = CarWithSharedFeatures('Hamza') + + car1.add_feature("Back Camera") + car2.add_feature("Leather seats") + + # All cars are sharing same mutable object + assert car1.features == ["Back Camera", "Leather seats"] + + class CarWithSeperateFeatures: + """ Car class example with correct shared variable usage""" + def __init__(self, owner_name): + self.name = owner_name # Instance variable unique to each instance. + self.features = [] + + def add_feature(self, feature): + """Add feature to the car + This function illustrate mistaken use of mutable class variable features. + """ + self.features.append(feature) + + car1 = CarWithSeperateFeatures('Ali') + car2 = CarWithSeperateFeatures('Hamza') + + car1.add_feature("Back Camera") + car2.add_feature("Leather_seats") + + # Each car has separate features list + assert car1.features == ["Back Camera"] + \ No newline at end of file diff --git a/src/operators/test_arithmetic.py b/src/operators/test_arithmetic.py index fa66ad66..6ef45e4f 100644 --- a/src/operators/test_arithmetic.py +++ b/src/operators/test_arithmetic.py @@ -19,6 +19,8 @@ def test_arithmetic_operators(): assert 5 * 3 == 15 assert isinstance(5 * 3, int) + assert isinstance(5 == 3, int) + # Division. # Result of division is float number. assert 5 / 3 == 1.6666666666666667 @@ -28,6 +30,7 @@ def test_arithmetic_operators(): # Modulus. assert 5 % 3 == 2 + assert 4 % 2 == 0 # Exponentiation. assert 5 ** 3 == 125