mirror of
https://github.com/robindhole/fundamentals.git
synced 2025-03-15 10:00:16 +00:00
Adds python code (#7)
This commit is contained in:
parent
8c24d0345a
commit
59d2d5acfb
3
.gitignore
vendored
3
.gitignore
vendored
@ -2,3 +2,6 @@ os/code/os/target/*
|
||||
os/code/os/target/
|
||||
database/code/__pycache__
|
||||
*.class
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
31
python_code/main.py
Normal file
31
python_code/main.py
Normal 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()
|
14
python_code/oop/SOLID/bird/Bird.py
Normal file
14
python_code/oop/SOLID/bird/Bird.py
Normal 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:
|
||||
...
|
6
python_code/oop/SOLID/bird/BirdType.py
Normal file
6
python_code/oop/SOLID/bird/BirdType.py
Normal file
@ -0,0 +1,6 @@
|
||||
from enum import Enum
|
||||
|
||||
class BirdType(Enum):
|
||||
Eagle = 0
|
||||
Penguin = 1
|
||||
Parrot = 2
|
16
python_code/oop/SOLID/bird/Eagle.py
Normal file
16
python_code/oop/SOLID/bird/Eagle.py
Normal 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:
|
||||
...
|
7
python_code/oop/SOLID/bird/FlappingBehaviour.py
Normal file
7
python_code/oop/SOLID/bird/FlappingBehaviour.py
Normal file
@ -0,0 +1,7 @@
|
||||
from Bird import *
|
||||
from BirdType import *
|
||||
from interfaces.FlyingBehaviourInterface import *
|
||||
|
||||
class FlappingBehaviour(FlyingBehaviourInterface):
|
||||
def makeFly(self) -> None:
|
||||
print("Flapping")
|
7
python_code/oop/SOLID/bird/GlidingBehaviour.py
Normal file
7
python_code/oop/SOLID/bird/GlidingBehaviour.py
Normal file
@ -0,0 +1,7 @@
|
||||
from Bird import *
|
||||
from BirdType import *
|
||||
from interfaces.FlyingBehaviourInterface import *
|
||||
|
||||
class GlidingBehaviour(FlyingBehaviourInterface):
|
||||
def makeFly(self) -> None:
|
||||
print("Gliding")
|
16
python_code/oop/SOLID/bird/Parrot.py
Normal file
16
python_code/oop/SOLID/bird/Parrot.py
Normal 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:
|
||||
...
|
14
python_code/oop/SOLID/bird/Penguin.py
Normal file
14
python_code/oop/SOLID/bird/Penguin.py
Normal 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:
|
||||
...
|
20
python_code/oop/SOLID/bird/Runner.py
Normal file
20
python_code/oop/SOLID/bird/Runner.py
Normal 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()
|
@ -0,0 +1,6 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class FlyableInterface(ABC):
|
||||
@abstractmethod
|
||||
def fly() -> None:
|
||||
...
|
@ -0,0 +1,6 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class FlyingBehaviourInterface(ABC):
|
||||
@abstractmethod
|
||||
def makeFly() -> None:
|
||||
...
|
@ -0,0 +1,6 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class SwimmableInterface:
|
||||
@abstractmethod
|
||||
def swim() -> None:
|
||||
...
|
0
python_code/oop/SOLID/bird/interfaces/__init__.py
Normal file
0
python_code/oop/SOLID/bird/interfaces/__init__.py
Normal file
27
python_code/oop/basic/OopBankAccount.py
Normal file
27
python_code/oop/basic/OopBankAccount.py
Normal 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)
|
||||
|
0
python_code/oop/basic/__init__.py
Normal file
0
python_code/oop/basic/__init__.py
Normal file
20
python_code/oop/inheritance/Student.py
Normal file
20
python_code/oop/inheritance/Student.py
Normal 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
|
6
python_code/oop/inheritance/StudentStatus.py
Normal file
6
python_code/oop/inheritance/StudentStatus.py
Normal file
@ -0,0 +1,6 @@
|
||||
from enum import Enum
|
||||
|
||||
class StudentStatus(Enum):
|
||||
ACTIVE = 1
|
||||
PAUSED = 1
|
||||
COMPLETED = 1
|
16
python_code/oop/inheritance/User.py
Normal file
16
python_code/oop/inheritance/User.py
Normal 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())
|
Loading…
x
Reference in New Issue
Block a user