Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/additions/test_generators.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 13 additions & 7 deletions src/classes/test_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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
Expand All @@ -47,20 +46,26 @@ 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."""

# There’s nothing special about instantiation of derived classes: DerivedClassName() creates a
# 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:
#
Expand All @@ -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)
3 changes: 3 additions & 0 deletions src/classes/test_instance_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

7 changes: 4 additions & 3 deletions src/classes/test_method_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 31 additions & 4 deletions src/classes/test_multiple_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@

def test_multiple_inheritance():
"""Multiple Inheritance"""

class Parent:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this example you'll have to test multiple inheritance, while you are testing only simple inheritance.

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

Expand All @@ -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

Expand Down Expand Up @@ -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'
23 changes: 23 additions & 0 deletions src/classes/test_practice_class_definition_and_objects.py
Original file line number Diff line number Diff line change
@@ -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)

74 changes: 74 additions & 0 deletions src/classes/test_practice_classes.py
Original file line number Diff line number Diff line change
@@ -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"]

3 changes: 3 additions & 0 deletions src/operators/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +30,7 @@ def test_arithmetic_operators():

# Modulus.
assert 5 % 3 == 2
assert 4 % 2 == 0

# Exponentiation.
assert 5 ** 3 == 125
Expand Down