From 3545a5cd634e4f6dc260d876bf40966623351862 Mon Sep 17 00:00:00 2001 From: LaraibWaheed Date: Thu, 4 Aug 2022 15:48:00 +0500 Subject: [PATCH 1/5] Practice Class and instance variables --- src/additions/test_generators.py | 2 +- src/classes/test_practice_classes.py | 85 ++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/classes/test_practice_classes.py 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_practice_classes.py b/src/classes/test_practice_classes.py new file mode 100644 index 00000000..55d8f835 --- /dev/null +++ b/src/classes/test_practice_classes.py @@ -0,0 +1,85 @@ +""" +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 From 4828d13d04ebc0576c159625ea175cf9622084b4 Mon Sep 17 00:00:00 2001 From: LaraibWaheed Date: Thu, 4 Aug 2022 16:15:05 +0500 Subject: [PATCH 2/5] Practice class definition and objects --- ...t_practice_class_definition_and_objects.py | 23 +++++++++++++ src/classes/test_practice_classes.py | 32 +++++++------------ 2 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 src/classes/test_practice_class_definition_and_objects.py 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..bc8576b8 --- /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) diff --git a/src/classes/test_practice_classes.py b/src/classes/test_practice_classes.py index 55d8f835..dfd355a2 100644 --- a/src/classes/test_practice_classes.py +++ b/src/classes/test_practice_classes.py @@ -2,16 +2,16 @@ 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 + # pylint: disbale= too-few-public-methods name = 'Sports Car' - def __init__(self,owner_name): - self.owner_name=owner_name + def __init__(self, owner_name): + self.owner_name = owner_name car1 = Car('Ali') car2 = Car('Hamza') @@ -25,20 +25,17 @@ def __init__(self,owner_name): 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=[] + 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. @@ -46,21 +43,19 @@ def add_feature(self, feature): self.features.append(feature) car1 = CarWithSharedFeatures('Ali') - car2 = CarWithSharedFeatures ('Hamza') + 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"] + # 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=[] + self.features = [] def add_feature(self, feature): """Add feature to the car @@ -69,7 +64,7 @@ def add_feature(self, feature): self.features.append(feature) car1 = CarWithSeperateFeatures('Ali') - car2 = CarWithSeperateFeatures ('Hamza') + car2 = CarWithSeperateFeatures('Hamza') car1.add_feature("Back Camera") car2.add_feature("Leather_seats") @@ -77,9 +72,4 @@ def add_feature(self, feature): # Each car has separate features list assert car1.features == ["Back Camera"] - - - - - \ No newline at end of file From 0d2996484c39ed9769da22306055d10367d0e7cf Mon Sep 17 00:00:00 2001 From: LaraibWaheed Date: Thu, 4 Aug 2022 16:20:41 +0500 Subject: [PATCH 3/5] Inheritance practice --- src/classes/test_inheritance.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/classes/test_inheritance.py b/src/classes/test_inheritance.py index 5884da2c..33612532 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,8 @@ class Employee(Person): in the global scope.) """ def __init__(self, name, staff_id): - Person.__init__(self, name) + #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 @@ -55,12 +55,14 @@ 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') - assert person.get_name() == 'Bill' + assert person.get_name() == 'Jack' assert employee.get_name() == 'John' - assert employee.get_full_id() == 'John, A23' + + employee.name = 'Bill' + assert employee.get_full_id() == 'Bill, A1' # Python has two built-in functions that work with inheritance: # From 0b2564b251428ab035914fe8e311191db7023320 Mon Sep 17 00:00:00 2001 From: LaraibWaheed Date: Thu, 4 Aug 2022 16:47:49 +0500 Subject: [PATCH 4/5] Classes practice --- src/classes/test_inheritance.py | 9 +++++++-- src/classes/test_instance_objects.py | 4 ++++ src/classes/test_method_objects.py | 7 ++++--- src/classes/test_multiple_inheritance.py | 16 +++++++++++++--- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/classes/test_inheritance.py b/src/classes/test_inheritance.py index 33612532..b2975364 100644 --- a/src/classes/test_inheritance.py +++ b/src/classes/test_inheritance.py @@ -47,7 +47,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.""" @@ -57,10 +60,11 @@ def test_inheritance(): # reference is valid if this yields a function object. person = Person('Jack') employee = Employee('John', 'A1') + manager = Manager('Ali', 'A3', 'Finance') assert person.get_name() == 'Jack' assert employee.get_name() == 'John' - + assert manager.get_name() == 'Ali' employee.name = 'Bill' assert employee.get_full_id() == 'Bill, A1' @@ -78,6 +82,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..9c99ce05 100644 --- a/src/classes/test_instance_objects.py +++ b/src/classes/test_instance_objects.py @@ -26,5 +26,9 @@ 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 + + #assert dummy_instance.temporary_attribute == 1 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..1518168f 100644 --- a/src/classes/test_multiple_inheritance.py +++ b/src/classes/test_multiple_inheritance.py @@ -9,13 +9,18 @@ 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 getparentdata(self): + self.temp = "Parent from clock class" + return self.temp + def get_time(self): """Get current time @@ -24,11 +29,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 getparentdata(self): + self.temp = "Parent from calender class" + return self.temp + def get_date(self): """Get current date @@ -66,3 +75,4 @@ class where there is an overlap in the hierarchy. Thus, if an attribute is not f assert calendar_clock.get_date() == '12/08/2018' assert calendar_clock.get_time() == '11:23 PM' + assert calendar_clock.getparentdata() == 'Parent from clock class' From 3f622e23ab862f75de90b0ac6d5e0b3e8c5e633c Mon Sep 17 00:00:00 2001 From: LaraibWaheed Date: Fri, 5 Aug 2022 17:59:56 +0500 Subject: [PATCH 5/5] Corrections in push --- src/classes/test_inheritance.py | 1 - src/classes/test_instance_objects.py | 3 +-- src/classes/test_multiple_inheritance.py | 25 ++++++++++++++++--- ...t_practice_class_definition_and_objects.py | 2 +- src/classes/test_practice_classes.py | 3 +-- src/operators/test_arithmetic.py | 3 +++ 6 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/classes/test_inheritance.py b/src/classes/test_inheritance.py index b2975364..d61d9602 100644 --- a/src/classes/test_inheritance.py +++ b/src/classes/test_inheritance.py @@ -37,7 +37,6 @@ 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) diff --git a/src/classes/test_instance_objects.py b/src/classes/test_instance_objects.py index 9c99ce05..65a83cda 100644 --- a/src/classes/test_instance_objects.py +++ b/src/classes/test_instance_objects.py @@ -30,5 +30,4 @@ class DummyClass: assert dummy_instance.temporary_attribute == 1 assert dummy_instance.name == "Dummy" del dummy_instance.temporary_attribute - - #assert dummy_instance.temporary_attribute == 1 + \ No newline at end of file diff --git a/src/classes/test_multiple_inheritance.py b/src/classes/test_multiple_inheritance.py index 1518168f..5b6f9779 100644 --- a/src/classes/test_multiple_inheritance.py +++ b/src/classes/test_multiple_inheritance.py @@ -12,12 +12,13 @@ def test_multiple_inheritance(): class Parent: temp="Dummy variable" # pylint: disable=too-few-public-methods + class Clock(Parent): """Clock class""" time = '11:23 PM' - def getparentdata(self): + def get_parent_data(self): self.temp = "Parent from clock class" return self.temp @@ -34,7 +35,7 @@ class Calendar(Parent): date = '12/08/2018' - def getparentdata(self): + def get_parent_data(self): self.temp = "Parent from calender class" return self.temp @@ -72,7 +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.getparentdata() == 'Parent from clock class' + 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 index bc8576b8..174e51e9 100644 --- a/src/classes/test_practice_class_definition_and_objects.py +++ b/src/classes/test_practice_class_definition_and_objects.py @@ -15,9 +15,9 @@ def __init__(self, owner_name, model): 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 index dfd355a2..df128957 100644 --- a/src/classes/test_practice_classes.py +++ b/src/classes/test_practice_classes.py @@ -71,5 +71,4 @@ def add_feature(self, feature): # Each car has separate features list assert car1.features == ["Back Camera"] - - \ No newline at end of file + \ 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