From 4ff137d75872c9d1f7032d56dfc25977a3d7205e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 29 Mar 2020 17:58:04 +0200 Subject: [PATCH] make communicator singleton --- communicator.py | 8 ++------ singleton.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 singleton.py diff --git a/communicator.py b/communicator.py index dd8c348..93351a6 100644 --- a/communicator.py +++ b/communicator.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import requests +from singleton import Singleton """ Communicator module @@ -11,15 +12,10 @@ __copyright__ = "Copyright 2020, GoldenPogácsa Team" __module_name__ = "messagesender" __version__text__ = "1" -class Communicator: +class Communicator(Singleton): """ Class handling low level communication with consumers. """ - def __init__(self): - """ - Initalize class - """ - pass def sendmessage(self, message: str) -> None: """ diff --git a/singleton.py b/singleton.py new file mode 100644 index 0000000..daae25c --- /dev/null +++ b/singleton.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +""" +Singleton meta module +""" + +__author__ = "@tormakris" +__copyright__ = "Copyright 2020, GoldenPogácsa Team" +__module_name__ = "singleton" +__version__text__ = "1" + + +class Singleton(object): + """ + Singleton metaclass + """ + _instances = {} + + def __new__(class_, *args, **kwargs): + """ + New + :param args: + :param kwargs: + :return: + """ + if class_ not in class_._instances: + class_._instances[class_] = super( + Singleton, class_).__new__( + class_, *args, **kwargs) + return class_._instances[class_]