Adds python code (#7)

This commit is contained in:
Abhishek Singh Thakur 2022-10-13 19:54:41 +05:30 committed by GitHub
parent 8c24d0345a
commit 59d2d5acfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 221 additions and 0 deletions

3
.gitignore vendored
View File

@ -2,3 +2,6 @@ os/code/os/target/*
os/code/os/target/ os/code/os/target/
database/code/__pycache__ database/code/__pycache__
*.class *.class
__pycache__/
*.py[cod]
*$py.class

31
python_code/main.py Normal file
View File

@ -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()

View File

@ -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:
...

View File

@ -0,0 +1,6 @@
from enum import Enum
class BirdType(Enum):
Eagle = 0
Penguin = 1
Parrot = 2

View File

@ -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:
...

View File

@ -0,0 +1,7 @@
from Bird import *
from BirdType import *
from interfaces.FlyingBehaviourInterface import *
class FlappingBehaviour(FlyingBehaviourInterface):
def makeFly(self) -> None:
print("Flapping")

View File

@ -0,0 +1,7 @@
from Bird import *
from BirdType import *
from interfaces.FlyingBehaviourInterface import *
class GlidingBehaviour(FlyingBehaviourInterface):
def makeFly(self) -> None:
print("Gliding")

View File

@ -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:
...

View File

@ -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:
...

View File

@ -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()

View File

@ -0,0 +1,6 @@
from abc import ABC, abstractmethod
class FlyableInterface(ABC):
@abstractmethod
def fly() -> None:
...

View File

@ -0,0 +1,6 @@
from abc import ABC, abstractmethod
class FlyingBehaviourInterface(ABC):
@abstractmethod
def makeFly() -> None:
...

View File

@ -0,0 +1,6 @@
from abc import ABC, abstractmethod
class SwimmableInterface:
@abstractmethod
def swim() -> None:
...

View File

@ -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)

View File

View File

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

View File

@ -0,0 +1,6 @@
from enum import Enum
class StudentStatus(Enum):
ACTIVE = 1
PAUSED = 1
COMPLETED = 1

View File

@ -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())