Skip to content
Snippets Groups Projects
Commit fda3cda5 authored by Anton Gusev's avatar Anton Gusev
Browse files

rewrite Singleton class

parent 4e5b7b4e
No related branches found
No related tags found
2 merge requests!5Develop,!4Develop
This commit is part of merge request !5. Comments created here will be created in the context of that merge request.
......@@ -3,7 +3,7 @@ from utils.Singleton import Singleton
import MySQLdb as sql
class AltConnector(metaclass=Singleton):
class AltConnector(Singleton):
__connection = None
@staticmethod
......
......@@ -4,7 +4,7 @@ from scrapy.conf import settings
from utils.Singleton import Singleton
class Connector(metaclass=Singleton):
class Connector(Singleton):
__connection = None
__engine = create_engine(settings['CONNECTION_STRING'])
__Session = sessionmaker(bind=__engine)
......
class MetaSingleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
print('__call__ __MetaSingleton')
if cls not in cls._instances:
cls._instances[cls] = super(MetaSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
from .MetaSingleton import MetaSingleton
class Singleton(type):
_instances = {}
class Singleton:
instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
\ No newline at end of file
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(Singleton, cls).__new__(cls)
return cls.instance
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment