From 59d2d5acfbd3af957adbb3294251ea50bc06a19e Mon Sep 17 00:00:00 2001 From: Abhishek Singh Thakur Date: Thu, 13 Oct 2022 19:54:41 +0530 Subject: [PATCH] Adds python code (#7) --- .gitignore | 3 ++ python_code/main.py | 31 +++++++++++++++++++ python_code/oop/SOLID/bird/Bird.py | 14 +++++++++ python_code/oop/SOLID/bird/BirdType.py | 6 ++++ python_code/oop/SOLID/bird/Eagle.py | 16 ++++++++++ .../oop/SOLID/bird/FlappingBehaviour.py | 7 +++++ .../oop/SOLID/bird/GlidingBehaviour.py | 7 +++++ python_code/oop/SOLID/bird/Parrot.py | 16 ++++++++++ python_code/oop/SOLID/bird/Penguin.py | 14 +++++++++ python_code/oop/SOLID/bird/Runner.py | 20 ++++++++++++ .../SOLID/bird/interfaces/FlyableInterface.py | 6 ++++ .../interfaces/FlyingBehaviourInterface.py | 6 ++++ .../bird/interfaces/SwimmableInterface.py | 6 ++++ .../oop/SOLID/bird/interfaces/__init__.py | 0 python_code/oop/basic/OopBankAccount.py | 27 ++++++++++++++++ python_code/oop/basic/__init__.py | 0 python_code/oop/inheritance/Student.py | 20 ++++++++++++ python_code/oop/inheritance/StudentStatus.py | 6 ++++ python_code/oop/inheritance/User.py | 16 ++++++++++ 19 files changed, 221 insertions(+) create mode 100644 python_code/main.py create mode 100644 python_code/oop/SOLID/bird/Bird.py create mode 100644 python_code/oop/SOLID/bird/BirdType.py create mode 100644 python_code/oop/SOLID/bird/Eagle.py create mode 100644 python_code/oop/SOLID/bird/FlappingBehaviour.py create mode 100644 python_code/oop/SOLID/bird/GlidingBehaviour.py create mode 100644 python_code/oop/SOLID/bird/Parrot.py create mode 100644 python_code/oop/SOLID/bird/Penguin.py create mode 100644 python_code/oop/SOLID/bird/Runner.py create mode 100644 python_code/oop/SOLID/bird/interfaces/FlyableInterface.py create mode 100644 python_code/oop/SOLID/bird/interfaces/FlyingBehaviourInterface.py create mode 100644 python_code/oop/SOLID/bird/interfaces/SwimmableInterface.py create mode 100644 python_code/oop/SOLID/bird/interfaces/__init__.py create mode 100644 python_code/oop/basic/OopBankAccount.py create mode 100644 python_code/oop/basic/__init__.py create mode 100644 python_code/oop/inheritance/Student.py create mode 100644 python_code/oop/inheritance/StudentStatus.py create mode 100644 python_code/oop/inheritance/User.py diff --git a/.gitignore b/.gitignore index 0fd6427..5cc5d29 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ os/code/os/target/* os/code/os/target/ database/code/__pycache__ *.class +__pycache__/ +*.py[cod] +*$py.class diff --git a/python_code/main.py b/python_code/main.py new file mode 100644 index 0000000..4dcf15a --- /dev/null +++ b/python_code/main.py @@ -0,0 +1,31 @@ +from oop.basic.OopBankAccount import OopBankAccount + +from oop.inheritance.Student import * +from oop.inheritance.User import * +from oop.inheritance.StudentStatus import * + +def main(): + #oop - basic + abhi = OopBankAccount(100, 1) + bob = OopBankAccount(200, 2) + + abhi.transfer(bob, 50) + + print(abhi.getBalance()) + + #oop - inheritance + + sam = Student("sam", "abhi@a.cm", 25, "khulri", "Oct", 90, StudentStatus.ACTIVE) + sam.print_details() + + # oop - inheritance and polymorphism + + student = Student("Student", "stu@scaler", "batch", 100) + student.__class__ = User + student.print_details() + + user = User("user", "user@gmail") + # user.print_details() Python does not support method overloading like java or C++ + # we need to define method with default arguments +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/python_code/oop/SOLID/bird/Bird.py b/python_code/oop/SOLID/bird/Bird.py new file mode 100644 index 0000000..2def247 --- /dev/null +++ b/python_code/oop/SOLID/bird/Bird.py @@ -0,0 +1,14 @@ +from abc import ABC, abstractmethod +import BirdType + +class Bird(ABC): + def __init__(self, weight: int, colour: str, size: int, beakType : str, birdType : BirdType) -> None: + self.__weight = weight + self.__colour = colour + self.__size = size + self.__beakType = beakType + self.__birdType = birdType + + @abstractmethod + def makeSound(self) -> None: + ... \ No newline at end of file diff --git a/python_code/oop/SOLID/bird/BirdType.py b/python_code/oop/SOLID/bird/BirdType.py new file mode 100644 index 0000000..1a35f4a --- /dev/null +++ b/python_code/oop/SOLID/bird/BirdType.py @@ -0,0 +1,6 @@ +from enum import Enum + +class BirdType(Enum): + Eagle = 0 + Penguin = 1 + Parrot = 2 diff --git a/python_code/oop/SOLID/bird/Eagle.py b/python_code/oop/SOLID/bird/Eagle.py new file mode 100644 index 0000000..e28416d --- /dev/null +++ b/python_code/oop/SOLID/bird/Eagle.py @@ -0,0 +1,16 @@ +from Bird import * +from BirdType import * +from interfaces.FlyableInterface import * +from interfaces.FlyingBehaviourInterface import * + +class Eagle(Bird, FlyableInterface): + __flyingBehaviour: FlyingBehaviourInterface = None + def __init__(self, weight: int, colour: str, size: int, beakType: str, birdType: BirdType, flyingBehaviour: FlyingBehaviourInterface) -> None: + super().__init__(weight, colour, size, beakType, birdType) + self.__flyingBehaviour = flyingBehaviour + + def fly(self) -> None: + self.__flyingBehaviour.makeFly() + + def makeSound(self) -> None: + ... \ No newline at end of file diff --git a/python_code/oop/SOLID/bird/FlappingBehaviour.py b/python_code/oop/SOLID/bird/FlappingBehaviour.py new file mode 100644 index 0000000..cef6b36 --- /dev/null +++ b/python_code/oop/SOLID/bird/FlappingBehaviour.py @@ -0,0 +1,7 @@ +from Bird import * +from BirdType import * +from interfaces.FlyingBehaviourInterface import * + +class FlappingBehaviour(FlyingBehaviourInterface): + def makeFly(self) -> None: + print("Flapping") \ No newline at end of file diff --git a/python_code/oop/SOLID/bird/GlidingBehaviour.py b/python_code/oop/SOLID/bird/GlidingBehaviour.py new file mode 100644 index 0000000..9e791fd --- /dev/null +++ b/python_code/oop/SOLID/bird/GlidingBehaviour.py @@ -0,0 +1,7 @@ +from Bird import * +from BirdType import * +from interfaces.FlyingBehaviourInterface import * + +class GlidingBehaviour(FlyingBehaviourInterface): + def makeFly(self) -> None: + print("Gliding") \ No newline at end of file diff --git a/python_code/oop/SOLID/bird/Parrot.py b/python_code/oop/SOLID/bird/Parrot.py new file mode 100644 index 0000000..805d003 --- /dev/null +++ b/python_code/oop/SOLID/bird/Parrot.py @@ -0,0 +1,16 @@ +from Bird import * +from BirdType import * +from interfaces.FlyableInterface import * +from interfaces.FlyingBehaviourInterface import * + +class Parrot(Bird, FlyableInterface): + __flyingBehaviour: FlyingBehaviourInterface = None + def __init__(self, weight: int, colour: str, size: int, beakType: str, birdType: BirdType, flyingBehaviour: FlyingBehaviourInterface) -> None: + super().__init__(weight, colour, size, beakType, birdType) + self.__flyingBehaviour = flyingBehaviour + + def fly(self) -> None: + self.__flyingBehaviour.makeFly() + + def makeSound(self) -> None: + ... \ No newline at end of file diff --git a/python_code/oop/SOLID/bird/Penguin.py b/python_code/oop/SOLID/bird/Penguin.py new file mode 100644 index 0000000..449e284 --- /dev/null +++ b/python_code/oop/SOLID/bird/Penguin.py @@ -0,0 +1,14 @@ +from Bird import * +from BirdType import * +from interfaces.SwimmableInterface import * +from interfaces.FlyingBehaviourInterface import * + +class Penguin(Bird, SwimmableInterface): + def __init__(self, weight: int, colour: str, size: int, beakType: str, birdType: BirdType) -> None: + super().__init__(weight, colour, size, beakType, birdType) + + def fly(self) -> None: + self.__flyingBehaviour.makeFly() + + def makeSound(self) -> None: + ... \ No newline at end of file diff --git a/python_code/oop/SOLID/bird/Runner.py b/python_code/oop/SOLID/bird/Runner.py new file mode 100644 index 0000000..f0df16e --- /dev/null +++ b/python_code/oop/SOLID/bird/Runner.py @@ -0,0 +1,20 @@ +from distutils.ccompiler import gen_lib_options +from Parrot import * +from Eagle import * +from Penguin import * +from BirdType import * +from interfaces.FlyableInterface import * +from interfaces.SwimmableInterface import * +from FlappingBehaviour import * +from GlidingBehaviour import * + +#import FlappingBehaviour gives error bcoz bird is package + +parrot = Parrot(10, "Green", "Small", "Sharp", BirdType.Parrot, FlappingBehaviour()) +parrot.fly() + +eagle = Eagle(20, "Brown", "Medium", "Sharp", BirdType.Eagle, GlidingBehaviour()) +eagle.fly() + +penguin = Penguin(30, "Black", "Large", "Sharp", BirdType.Penguin) +penguin.makeSound() \ No newline at end of file diff --git a/python_code/oop/SOLID/bird/interfaces/FlyableInterface.py b/python_code/oop/SOLID/bird/interfaces/FlyableInterface.py new file mode 100644 index 0000000..9e30b92 --- /dev/null +++ b/python_code/oop/SOLID/bird/interfaces/FlyableInterface.py @@ -0,0 +1,6 @@ +from abc import ABC, abstractmethod + +class FlyableInterface(ABC): + @abstractmethod + def fly() -> None: + ... \ No newline at end of file diff --git a/python_code/oop/SOLID/bird/interfaces/FlyingBehaviourInterface.py b/python_code/oop/SOLID/bird/interfaces/FlyingBehaviourInterface.py new file mode 100644 index 0000000..db2370b --- /dev/null +++ b/python_code/oop/SOLID/bird/interfaces/FlyingBehaviourInterface.py @@ -0,0 +1,6 @@ +from abc import ABC, abstractmethod + +class FlyingBehaviourInterface(ABC): + @abstractmethod + def makeFly() -> None: + ... \ No newline at end of file diff --git a/python_code/oop/SOLID/bird/interfaces/SwimmableInterface.py b/python_code/oop/SOLID/bird/interfaces/SwimmableInterface.py new file mode 100644 index 0000000..f26285a --- /dev/null +++ b/python_code/oop/SOLID/bird/interfaces/SwimmableInterface.py @@ -0,0 +1,6 @@ +from abc import ABC, abstractmethod + +class SwimmableInterface: + @abstractmethod + def swim() -> None: + ... \ No newline at end of file diff --git a/python_code/oop/SOLID/bird/interfaces/__init__.py b/python_code/oop/SOLID/bird/interfaces/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python_code/oop/basic/OopBankAccount.py b/python_code/oop/basic/OopBankAccount.py new file mode 100644 index 0000000..bb5e27f --- /dev/null +++ b/python_code/oop/basic/OopBankAccount.py @@ -0,0 +1,27 @@ +class OopBankAccount: + def __init__(self, balance, number): + self.__number = number + self.__balance = balance + + def getNumber(self): + return self.__number + + def setNumber(self, number): + self.__number = number + + def getBalance(self): + return self.__balance + + def setBalance(self, balance): + self.__balance = balance + + def deposit(self, amount): + self.__balance += amount + + def withdraw(self, amount): + self.__balance -= amount + + def transfer(self, destination, amount): + self.withdraw(amount) + destination.deposit(amount) + diff --git a/python_code/oop/basic/__init__.py b/python_code/oop/basic/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python_code/oop/inheritance/Student.py b/python_code/oop/inheritance/Student.py new file mode 100644 index 0000000..e3da219 --- /dev/null +++ b/python_code/oop/inheritance/Student.py @@ -0,0 +1,20 @@ +from oop.inheritance.User import * +from oop.inheritance.StudentStatus import * + +class Student(User): + + __status = StudentStatus.ACTIVE + + def __init__(self, name: str, email: str, batch_name: str, psp: int) -> None: + super().__init__(name, email) + self.__batch_name = batch_name + + if (psp < 0 or psp > 100): + raise Exception("PSP should be between 0 and 100") + self.__psp = psp + + def print_details(self) -> None: + print("In Student", self.get_name(), self.__batch_name) + + def change_batch(self, batch_name: str) -> None: + self.batch_name = batch_name diff --git a/python_code/oop/inheritance/StudentStatus.py b/python_code/oop/inheritance/StudentStatus.py new file mode 100644 index 0000000..acbf0ab --- /dev/null +++ b/python_code/oop/inheritance/StudentStatus.py @@ -0,0 +1,6 @@ +from enum import Enum + +class StudentStatus(Enum): + ACTIVE = 1 + PAUSED = 1 + COMPLETED = 1 \ No newline at end of file diff --git a/python_code/oop/inheritance/User.py b/python_code/oop/inheritance/User.py new file mode 100644 index 0000000..4104f10 --- /dev/null +++ b/python_code/oop/inheritance/User.py @@ -0,0 +1,16 @@ +class User: + def __init__(self, name: str, email: str) -> None: + self.__name = name + self.__email = email + + def change_email(self, email: str) -> None: + self.__email = email + + def get_name(self) -> str: + return self.__name + + def print_details(self) -> None: + print("Print with no args") + + def print_details(self, title: str) -> None: + print("\n In User:", title, self.get_name()) \ No newline at end of file