mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-14 00:06:58 +08:00
rename & test
This commit is contained in:
@@ -3,16 +3,23 @@ import json
|
|||||||
from fuzzywuzzy import fuzz
|
from fuzzywuzzy import fuzz
|
||||||
|
|
||||||
|
|
||||||
class Singleton(type):
|
class SingletonMeta(type):
|
||||||
_instance = None
|
_instance = None
|
||||||
|
|
||||||
def __call__(cls, *args, **kwargs):
|
def __call__(cls, *args, **kwargs):
|
||||||
if cls._instance is None:
|
if cls._instance is None:
|
||||||
cls._instance = super(Singleton, cls).__call__(*args, **kwargs)
|
cls._instance = super(SingletonMeta, cls).__call__(*args, **kwargs)
|
||||||
return cls._instance
|
return cls._instance
|
||||||
|
|
||||||
class SingletonBaseClass(metaclass=Singleton):
|
|
||||||
pass
|
class SingletonBaseClass(metaclass=SingletonMeta):
|
||||||
|
"""
|
||||||
|
Because we try to support defining Singleton with `class A(SingletonBaseClass)` instead of `A(metaclass=SingletonMeta)`
|
||||||
|
This class becomes necessary
|
||||||
|
|
||||||
|
"""
|
||||||
|
# TODO: Add move this class to Qlib's general utils.
|
||||||
|
|
||||||
|
|
||||||
def parse_json(response):
|
def parse_json(response):
|
||||||
try:
|
try:
|
||||||
|
|||||||
23
tests/finco/test_utils.py
Normal file
23
tests/finco/test_utils.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import unittest
|
||||||
|
from qlib.finco.utils import SingletonBaseClass
|
||||||
|
|
||||||
|
|
||||||
|
class TimeUtils(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_singleton(self):
|
||||||
|
# self.assertEqual(self.to_str(data.tail()), self.to_str(res))
|
||||||
|
closure_checker = []
|
||||||
|
|
||||||
|
class A(SingletonBaseClass):
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
closure_checker.append(0)
|
||||||
|
|
||||||
|
A()
|
||||||
|
self.assertEqual(len(closure_checker), 1)
|
||||||
|
A()
|
||||||
|
self.assertEqual(len(closure_checker), 1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user