Skip to content

Instantly share code, notes, and snippets.

View kcsanjeeb's full-sized avatar
🎯
Focusing

sanjeeb kc kcsanjeeb

🎯
Focusing
  • offix services pvt. ltd.
  • kapan, kathmandu
  • X @sanjeebkc9
View GitHub Profile
class You(object):
def __init__(self):
print("You :: How can i arrange such a huge software product launch event all by myself. What a mess.")
def askEventOrganizer(self):
print("You :: I think i should contact an event organizer\n")
eo = EventOrganizer()
eo.arrange()
def __del__(self):
print("you :: Thanks to the event organizer, all preparations are successfully completed.")
class Hotelier(object):
def __init__(self):
print("--- Hotel Arrangement ---")
def __isAvailable(self):
print("Is the hotel available to oraganize launch event on the date ?")
return True
def bookHotel(self):
if self.__isAvailable():
print("Registered the Booking\n")
class EventOrganizer(object):
def __init__(self):
print("I will manage the event for you. I will talk to the service providers.\n")
def arrange(self):
self.hotelier = Hotelier()
self.hotelier.bookHotel()
self.florist = Florist()
self.florist.setFlowerRequirements()
class EventOrganizer(object):
def __init__(self):
print("I will manage the event for you. I will talk to the service providers.\n")
def arrange(self):
self.hotelier = Hotelier()
self.hotelier.bookHotel()
self.florist = Florist()
self.florist.setFlowerRequirements()
class EventOrganizer(object):
def __init__(self):
print("I will manage the event for you. I will talk to the service providers.\n")
def arrange(self):
self.hotelier = Hotelier()
self.hotelier.bookHotel()
self.florist = Florist()
self.florist.setFlowerRequirements()
@kcsanjeeb
kcsanjeeb / singletonDB.py
Created July 24, 2020 16:24
A Real-world scenario - the singleton pattern
import sqlite3
class MetaSingleton(type):
_instances ={}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(MetaSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Database(metaclass=MetaSingleton):
class ClassA(type):
def __call__(cls,*args,**kwds):
print("This is my integers : ",args)
return type.__call__(cls,*args,**kwds)
class int(metaclass=ClassA):
def __init__(self,x,y):
self.x = x
self.y = y
test = int(20,40)
class Singleton:
__instance = None
def __init__(self):
if not Singleton.__instance:
print("__init__ method is called.")
else:
print("Instance is already created :",self.getInstance())
@classmethod
def getInstance(cls):
class Singleton(object):
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(Singleton,cls).__new__(cls)
return cls.instance
s = Singleton()
print("Object created",s)
sA = Singleton()
from abc import ABCMeta, abstractmethod
class PizzaFactory(metaclass=ABCMeta):
@abstractmethod
def createVegPizza(self):
pass
@abstractmethod
def createNonVegPizza(self):
pass