From d78cf1f8e9dba50ce3d9b04d67b8c26f2d16537a Mon Sep 17 00:00:00 2001 From: muassif <32828975+muassif@users.noreply.github.com> Date: Sat, 16 Jan 2021 15:14:59 +0400 Subject: [PATCH] Adding Chapter 3 files --- Chapter3/abstraction/abstraction1.py | 25 ++++++++++++++++ Chapter3/classes/carexample1.py | 10 +++++++ Chapter3/classes/carexample2.py | 13 +++++++++ Chapter3/classes/carexample3.py | 25 ++++++++++++++++ Chapter3/classes/carexample4.py | 15 ++++++++++ Chapter3/classes/carexample5.py | 25 ++++++++++++++++ Chapter3/classes/carexample6.py | 30 +++++++++++++++++++ Chapter3/classes/carexample7.py | 32 +++++++++++++++++++++ Chapter3/classes/carwithInnerexample1.py | 25 ++++++++++++++++ Chapter3/classes/methodsexample1.py | 30 +++++++++++++++++++ Chapter3/classes/snippets.py | 13 +++++++++ Chapter3/composition/composition1.py | 32 +++++++++++++++++++++ Chapter3/ductyyping/ducttype1.py | 17 +++++++++++ Chapter3/inheritance/inheritance1.py | 32 +++++++++++++++++++++ Chapter3/inheritance/inheritance2.py | 30 +++++++++++++++++++ Chapter3/polymorphism/methodoverloading1.py | 17 +++++++++++ Chapter3/polymorphism/methodoverriding1.py | 31 ++++++++++++++++++++ 17 files changed, 402 insertions(+) create mode 100644 Chapter3/abstraction/abstraction1.py create mode 100644 Chapter3/classes/carexample1.py create mode 100644 Chapter3/classes/carexample2.py create mode 100644 Chapter3/classes/carexample3.py create mode 100644 Chapter3/classes/carexample4.py create mode 100644 Chapter3/classes/carexample5.py create mode 100644 Chapter3/classes/carexample6.py create mode 100644 Chapter3/classes/carexample7.py create mode 100644 Chapter3/classes/carwithInnerexample1.py create mode 100644 Chapter3/classes/methodsexample1.py create mode 100644 Chapter3/classes/snippets.py create mode 100644 Chapter3/composition/composition1.py create mode 100644 Chapter3/ductyyping/ducttype1.py create mode 100644 Chapter3/inheritance/inheritance1.py create mode 100644 Chapter3/inheritance/inheritance2.py create mode 100644 Chapter3/polymorphism/methodoverloading1.py create mode 100644 Chapter3/polymorphism/methodoverriding1.py diff --git a/Chapter3/abstraction/abstraction1.py b/Chapter3/abstraction/abstraction1.py new file mode 100644 index 0000000..908c0a6 --- /dev/null +++ b/Chapter3/abstraction/abstraction1.py @@ -0,0 +1,25 @@ +#abstraction1.py +from abc import ABC, abstractmethod + +class Vehicle(ABC): + def hello(self): + print(f"Hello from abstract class") + @abstractmethod + def print_me(self): + pass + +class Car (Vehicle): + def __init__(self, color, seats): + self.i_color = color + self.i_seats = seats + + """It is must to implemented this method""" + def print_me(self): + print( f"Car with color {self.i_color} and no of seats {self.i_seats}") + +if __name__ == "__main__": + #vehicle = Vehicle() + #vehicle.hello() + car = Car ("blue", 5) + car.print_me() + car.hello() \ No newline at end of file diff --git a/Chapter3/classes/carexample1.py b/Chapter3/classes/carexample1.py new file mode 100644 index 0000000..68f9795 --- /dev/null +++ b/Chapter3/classes/carexample1.py @@ -0,0 +1,10 @@ +#carexample1.py +class Car: + pass + +if __name__ == "__main__": + car = Car () + car.color = "blue" + car.miles = 1000 + print (car.color) + print (car.miles) \ No newline at end of file diff --git a/Chapter3/classes/carexample2.py b/Chapter3/classes/carexample2.py new file mode 100644 index 0000000..339e07c --- /dev/null +++ b/Chapter3/classes/carexample2.py @@ -0,0 +1,13 @@ +#carexample2.py +class Car: + c_mileage_units = "Mi" + def __init__(self, color, miles): + self.i_color = color + self.i_mileage = miles + +if __name__ == "__main__": + car = Car ("blue", 1000) + print (car.i_color) + print (car.i_mileage) + print (car.c_mileage_units) + print (Car.c_mileage_units) diff --git a/Chapter3/classes/carexample3.py b/Chapter3/classes/carexample3.py new file mode 100644 index 0000000..e36f456 --- /dev/null +++ b/Chapter3/classes/carexample3.py @@ -0,0 +1,25 @@ +#carexample3.py +class Car: + c_mileage_units = "Mi" + def __init__(self, color, miles): + self.i_color = color + self.i_mileage = miles + +if __name__ == "__main__": + car1 = Car ("blue", 1000) + car2 = Car("red", 2000) + + print("using car1: " + car1.c_mileage_units) + print("using car2: " + car2.c_mileage_units) + print("using Class: " + Car.c_mileage_units) + + car1.c_mileage_units = "km" + + print("using car1: " + car1.c_mileage_units) + print("using car2: " + car2.c_mileage_units) + print("using Class: " + Car.c_mileage_units) + + Car.c_mileage_units = "NP" + print("using car1: " + car1.c_mileage_units) + print("using car2: " + car2.c_mileage_units) + print("using Class: " + Car.c_mileage_units) \ No newline at end of file diff --git a/Chapter3/classes/carexample4.py b/Chapter3/classes/carexample4.py new file mode 100644 index 0000000..c498e62 --- /dev/null +++ b/Chapter3/classes/carexample4.py @@ -0,0 +1,15 @@ +#carexampl4.py +class Car: + c_mileage_units = "Mi" + def __init__(self, color, miles): + self.i_color = color + self.i_mileage = miles + + def __str__(self): + return f"car with color {self.i_color} and mileage {self.i_mileage}" + +if __name__ == "__main__": + car1 = Car ("blue", 1000) + print (str(car1)) + print(repr(car1)) + print(dir(car1)) diff --git a/Chapter3/classes/carexample5.py b/Chapter3/classes/carexample5.py new file mode 100644 index 0000000..cbcc4d4 --- /dev/null +++ b/Chapter3/classes/carexample5.py @@ -0,0 +1,25 @@ +#carexample5.py +class Car: + c_mileage_units = "Mi" + __max_speed = 200 + + def __init__(self, color, miles, model): + self.i_color = color + self.i_mileage = miles + self.__no_doors = 4 + self._model = model + + def __str__(self): + return f"car with color {self.i_color}, mileage {self.i_mileage}, model {self._model} and doors {self.__doors()}" + + def __doors(self): + return self.__no_doors + +if __name__ == "__main__": + car = Car ("blue", 1000, "Camry") + print (car) + + """These lines are only for illustration and not best practice""" + print(Car._Car__max_speed) + print (car._Car__doors()) + print (car._model) #possible but against the protected definition diff --git a/Chapter3/classes/carexample6.py b/Chapter3/classes/carexample6.py new file mode 100644 index 0000000..1903a13 --- /dev/null +++ b/Chapter3/classes/carexample6.py @@ -0,0 +1,30 @@ +#carexample6.py +class Car: + __mileage_units = "Mi" + def __init__(self, col, mil): + self.__color = col + self.__mileage = mil + + def __str__(self): + return f"car with color {self.get_color()} and mileage {self.get_mileage()}" + + def get_color(self): + return self.__color + + def get_mileage(self): + return self.__mileage + + def set_mileage (self, new_mil): + self.__mileage = new_mil + + +if __name__ == "__main__": + car = Car ("blue", 1000) + + print (car) + print (car.get_color()) + print(car.get_mileage()) + car.set_mileage(2000) + print (car.get_color()) + print(car.get_mileage()) + diff --git a/Chapter3/classes/carexample7.py b/Chapter3/classes/carexample7.py new file mode 100644 index 0000000..18be533 --- /dev/null +++ b/Chapter3/classes/carexample7.py @@ -0,0 +1,32 @@ +#carexample7.py +class Car: + __mileage_units = "Mi" + def __init__(self, col, mil): + self.__color = col + self.__mileage = mil + + def __str__(self): + return f"car with color {self.color} and mileage {self.mileage}" + + @property + def color(self): + return self.__color + + @property + def mileage(self): + return self.__mileage + + @mileage.setter + def mileage (self, new_mil): + self.__mileage = new_mil + + +if __name__ == "__main__": + car = Car ("blue", 1000) + + print (car) + print (car.color) + print(car.mileage) + car.mileage = 2000 + print (car.color) + print(car.mileage) diff --git a/Chapter3/classes/carwithInnerexample1.py b/Chapter3/classes/carwithInnerexample1.py new file mode 100644 index 0000000..5729b8a --- /dev/null +++ b/Chapter3/classes/carwithInnerexample1.py @@ -0,0 +1,25 @@ +#carwithinnerexample1.py +class Car: + """outer class""" + c_mileage_units = "Mi" + def __init__(self, color, miles, eng_size): + self.i_color = color + self.i_mileage = miles + self.i_engine = self.Engine(eng_size) + + def __str__(self): + return f"car with color {self.i_color}, mileage {self.i_mileage} and engine of {self.i_engine}" + + class Engine: + """inner class""" + def __init__(self, size): + self.i_size = size + + def __str__(self): + return self.i_size + + +if __name__ == "__main__": + car = Car ("blue", 1000, "2.5L") + print (car) + print (car.i_engine.i_size) diff --git a/Chapter3/classes/methodsexample1.py b/Chapter3/classes/methodsexample1.py new file mode 100644 index 0000000..34df992 --- /dev/null +++ b/Chapter3/classes/methodsexample1.py @@ -0,0 +1,30 @@ +#methodsexample1.py +class Car: + c_mileage_unit = "Mi" + + def __init__(self, color, miles): + self.i_color = color + self.i_mileage = miles + + def print_color (self): + print (f"Color of the car is {self.i_color}") + + @classmethod + def print_units(cls): + print (f"mileage unit are {cls.c_mileage_unit}") + print(f"class name is {cls.__name__}") + + @staticmethod + def print_hello(): + print ("Hello from a static method") + + +if __name__ == "__main__": + car = Car ("blue", 1000) + car.print_color() + car.print_units() + car.print_hello() + + Car.print_color(car); + Car.print_units(); + Car.print_hello() \ No newline at end of file diff --git a/Chapter3/classes/snippets.py b/Chapter3/classes/snippets.py new file mode 100644 index 0000000..adeea0c --- /dev/null +++ b/Chapter3/classes/snippets.py @@ -0,0 +1,13 @@ +#snippet.py +class Name: + def __init__(self): + self.first = "John" + +class Name: + #class attributes + c_name_length = 80 + + #constructor + def __init__(self, first, last): + self.i_first = first #i_first is an instance attribute + self.i_last = last #i_last is an instance attribute \ No newline at end of file diff --git a/Chapter3/composition/composition1.py b/Chapter3/composition/composition1.py new file mode 100644 index 0000000..6ac563f --- /dev/null +++ b/Chapter3/composition/composition1.py @@ -0,0 +1,32 @@ +#composition1.py +class Seat: + def __init__(self, type): + self.i_type = type + def __str__(self): + return f"Seat type: {self.i_type}" + +class Engine: + def __init__(self, size): + self.i_size = size + + def __str__(self): + return f"Engine: {self.i_size}" + +class Car: + def __init__(self, color, eng_size, seat_type): + self.i_color = color + self.engine = Engine(eng_size) + self.seat = Seat(seat_type) + + def print_me(self): + print(f"This car of color {self.i_color} with {self.engine} and {self.seat}") + + +if __name__ == "__main__": + car = Car ("blue", "2.5L", "leather" ) + car.print_me() + print(car.engine) + print(car.seat) + print(car.i_color) + print(car.engine.i_size) + print(car.seat.i_type) diff --git a/Chapter3/ductyyping/ducttype1.py b/Chapter3/ductyyping/ducttype1.py new file mode 100644 index 0000000..d4fceca --- /dev/null +++ b/Chapter3/ductyyping/ducttype1.py @@ -0,0 +1,17 @@ +#ducttype1.py +class Car: + def start(self): + print ("start engine by ignition /battery") + +class Cycle: + def start(self): + print ("start by pushing paddles") + +class Horse: + def start(self): + print ("start by pulling/releasing the reins") + + +if __name__ == "__main__": + for obj in Car(), Cycle(), Horse(): + obj.start() \ No newline at end of file diff --git a/Chapter3/inheritance/inheritance1.py b/Chapter3/inheritance/inheritance1.py new file mode 100644 index 0000000..033c6e8 --- /dev/null +++ b/Chapter3/inheritance/inheritance1.py @@ -0,0 +1,32 @@ +#inheritance1.py +class Vehicle: + def __init__(self, color): + self.i_color = color + + def print_vehicle_info(self): + print(f"This is vehicle and I know my color is {self.i_color}") + +class Car (Vehicle): + def __init__(self, color, seats): + self.i_color = color + self.i_seats = seats + + def print_me(self): + print( f"Car with color {self.i_color} and no of seats {self.i_seats}") + +class Truck (Vehicle): + def __init__(self, color, capacity): + self.i_color = color + self.i_capacity = capacity + + def print_me(self): + print( f"Truck with color {self.i_color} and loading capacity {self.i_capacity} tons") + + +if __name__ == "__main__": + car = Car ("blue", 5) + car.print_vehicle_info() + car.print_me() + truck = Truck("white", 1000) + truck.print_vehicle_info() + truck.print_me() \ No newline at end of file diff --git a/Chapter3/inheritance/inheritance2.py b/Chapter3/inheritance/inheritance2.py new file mode 100644 index 0000000..0f69e60 --- /dev/null +++ b/Chapter3/inheritance/inheritance2.py @@ -0,0 +1,30 @@ +#inheritance2.py +class Vehicle: + def __init__(self, color): + self.i_color = color + + def print_vehicle_info(self): + print( f"This is vehicle and I know my color is {self.i_color}") + +class Engine: + def __init__(self, size): + self.i_size = size + + def print_engine_info(self): + print(f"This is Engine and I know my size is {self.i_size}") + +class Car (Vehicle, Engine): + def __init__(self, color, size, seat): + self.i_color = color + self.i_size = size + self.i_seat = seat + + def print_car_info(self): + print(f"This car of color {self.i_color} with seats {self.i_seat} with engine of size {self.i_size}") + + +if __name__ == "__main__": + car = Car ("blue", "2.5L", 5 ) + car.print_vehicle_info() + car.print_engine_info() + car.print_car_info() diff --git a/Chapter3/polymorphism/methodoverloading1.py b/Chapter3/polymorphism/methodoverloading1.py new file mode 100644 index 0000000..74180a9 --- /dev/null +++ b/Chapter3/polymorphism/methodoverloading1.py @@ -0,0 +1,17 @@ +#methodoverloading1.py +class Car: + def __init__(self, color, seats): + self.i_color = color + self.i_seat = seats + + def print_me(self, i='basic'): + if(i =='basic'): + print(f"This car is of color {self.i_color}") + else: + print(f"This car is of color {self.i_color} with seats {self.i_seat}") + +if __name__ == "__main__": + car = Car("blue", 5 ) + car.print_me() + car.print_me('blah') + car.print_me('detail') diff --git a/Chapter3/polymorphism/methodoverriding1.py b/Chapter3/polymorphism/methodoverriding1.py new file mode 100644 index 0000000..08dd765 --- /dev/null +++ b/Chapter3/polymorphism/methodoverriding1.py @@ -0,0 +1,31 @@ +#methodoverriding1.py +class Vehicle: + def __init__(self, color): + self.i_color = color + + def print_me(self): + print(f"This is vehicle and I know my color is {self.i_color}") + +class Car (Vehicle): + def __init__(self, color, seats): + self.i_color = color + self.i_seats = seats + + def print_me(self): + print( f"Car with color {self.i_color} and no of seats {self.i_seats}") + +class Truck (Vehicle): + def __init__(self, color, capacity): + self.i_color = color + self.i_capacity = capacity + + def print_me(self): + print( f"Truck with color {self.i_color} and loading capacity {self.i_capacity} tons") + +if __name__ == "__main__": + vehicle = Vehicle("red") + vehicle.print_me() + car = Car ("blue", 5) + car.print_me() + truck = Truck("white", 1000) + truck.print_me()