mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-14 08:16:54 +08:00
Merge pull request #1581 from microsoft/xuyang1/fix_singleton_bug
fix singleton bug
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
# TODO: use pydantic for other modules in Qlib
|
# TODO: use pydantic for other modules in Qlib
|
||||||
from pydantic import BaseSettings
|
from pydantic import BaseSettings
|
||||||
from qlib.finco.utils import Singleton
|
from qlib.finco.utils import SingletonBaseClass
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
class Config(Singleton):
|
class Config(SingletonBaseClass):
|
||||||
"""
|
"""
|
||||||
This config is for fast demo purpose.
|
This config is for fast demo purpose.
|
||||||
Please use BaseSettings insetead in the future
|
Please use BaseSettings insetead in the future
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ import openai
|
|||||||
import json
|
import json
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from qlib.finco.conf import Config
|
from qlib.finco.conf import Config
|
||||||
from qlib.finco.utils import Singleton
|
from qlib.finco.utils import SingletonBaseClass
|
||||||
from qlib.finco.log import FinCoLog
|
from qlib.finco.log import FinCoLog
|
||||||
|
|
||||||
|
|
||||||
class APIBackend(Singleton):
|
class APIBackend(SingletonBaseClass):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.cfg = Config()
|
self.cfg = Config()
|
||||||
openai.api_key = self.cfg.openai_api_key
|
openai.api_key = self.cfg.openai_api_key
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ This module will base on Qlib's logger module and provides some interactive func
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from typing import Dict, List
|
from typing import Dict, List
|
||||||
from qlib.finco.utils import Singleton
|
from qlib.finco.utils import SingletonBaseClass
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ def formatting_log(logger, title="Info"):
|
|||||||
logger.info("")
|
logger.info("")
|
||||||
|
|
||||||
|
|
||||||
class FinCoLog(Singleton):
|
class FinCoLog(SingletonBaseClass):
|
||||||
# TODO:
|
# TODO:
|
||||||
# - config to file logger and save it into workspace
|
# - config to file logger and save it into workspace
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ from pathlib import Path
|
|||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from qlib.finco.utils import Singleton
|
from qlib.finco.utils import SingletonBaseClass
|
||||||
from qlib.finco import get_finco_path
|
from qlib.finco import get_finco_path
|
||||||
|
|
||||||
|
|
||||||
class PromptTemplate(Singleton):
|
class PromptTemplate(SingletonBaseClass):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
_template = yaml.load(open(Path.joinpath(get_finco_path(), "prompt_template.yaml"), "r"),
|
_template = yaml.load(open(Path.joinpath(get_finco_path(), "prompt_template.yaml"), "r"),
|
||||||
|
|||||||
@@ -723,9 +723,9 @@ class YamlEditTask(ActionTask):
|
|||||||
# TODO hotfix for the bug that the record signalrecord config is not updated
|
# TODO hotfix for the bug that the record signalrecord config is not updated
|
||||||
for record in target_config['task']['record']:
|
for record in target_config['task']['record']:
|
||||||
if record['class'] == 'SignalRecord':
|
if record['class'] == 'SignalRecord':
|
||||||
if 'model' in record['kwargs']:
|
if 'kwargs' in record and 'model' in record['kwargs']:
|
||||||
del record['kwargs']["model"]
|
del record['kwargs']["model"]
|
||||||
if 'dataset' in record['kwargs']:
|
if 'kwargs' in record and 'dataset' in record['kwargs']:
|
||||||
del record['kwargs']["dataset"]
|
del record['kwargs']["dataset"]
|
||||||
|
|
||||||
# 4) save the config file
|
# 4) save the config file
|
||||||
|
|||||||
@@ -3,14 +3,16 @@ import json
|
|||||||
from fuzzywuzzy import fuzz
|
from fuzzywuzzy import fuzz
|
||||||
|
|
||||||
|
|
||||||
class Singleton:
|
class Singleton(type):
|
||||||
_instance = None
|
_instance = None
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
def __call__(cls, *args, **kwargs):
|
||||||
if cls._instance is None:
|
if cls._instance is None:
|
||||||
cls._instance = super().__new__(cls, *args, **kwargs)
|
cls._instance = super(Singleton, cls).__call__(*args, **kwargs)
|
||||||
return cls._instance
|
return cls._instance
|
||||||
|
|
||||||
|
class SingletonBaseClass(metaclass=Singleton):
|
||||||
|
pass
|
||||||
|
|
||||||
def parse_json(response):
|
def parse_json(response):
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user