implemented Py2 & Py3 compatible Singleton

This commit is contained in:
Dhruvan Ganesh 2016-08-10 15:41:51 +05:30
parent 5bb98aa007
commit 0f89ff4d63
2 changed files with 12 additions and 6 deletions

View File

@ -6,14 +6,10 @@ from urlparse import urlparse
from logger import logger from logger import logger
from parser import request from parser import request
from nhentai.utils import Singleton
class Downloader(object):
_instance = None
def __new__(cls, *args, **kwargs): class Downloader(Singleton):
if not cls._instance:
cls._instance = super(Downloader, cls).__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self, path='', thread=1, timeout=30): def __init__(self, path='', thread=1, timeout=30):
if not isinstance(thread, (int, )) or thread < 1 or thread > 10: if not isinstance(thread, (int, )) or thread < 1 or thread > 10:

10
nhentai/utils.py Normal file
View File

@ -0,0 +1,10 @@
# coding: utf-8
class _Singleton(type):
""" A metaclass that creates a Singleton base class when called. """
_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]
class Singleton(_Singleton('SingletonMeta', (object,), {})): pass