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 ...@@ -3,7 +3,7 @@ from utils.Singleton import Singleton
import MySQLdb as sql import MySQLdb as sql
class AltConnector(metaclass=Singleton): class AltConnector(Singleton):
__connection = None __connection = None
@staticmethod @staticmethod
......
...@@ -4,7 +4,7 @@ from scrapy.conf import settings ...@@ -4,7 +4,7 @@ from scrapy.conf import settings
from utils.Singleton import Singleton from utils.Singleton import Singleton
class Connector(metaclass=Singleton): class Connector(Singleton):
__connection = None __connection = None
__engine = create_engine(settings['CONNECTION_STRING']) __engine = create_engine(settings['CONNECTION_STRING'])
__Session = sessionmaker(bind=__engine) __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): class Singleton:
_instances = {} instances = {}
def __call__(cls, *args, **kwargs): def __new__(cls):
if cls not in cls._instances: if not hasattr(cls, 'instance'):
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) cls.instance = super(Singleton, cls).__new__(cls)
return cls._instances[cls] return cls.instance
\ No newline at end of file
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