Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix singleton bug #1581

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions qlib/finco/conf.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# TODO: use pydantic for other modules in Qlib
from pydantic import BaseSettings
from qlib.finco.utils import Singleton
from qlib.finco.utils import SingletonBaseClass

import os


class Config(Singleton):
class Config(SingletonBaseClass):
"""
This config is for fast demo purpose.
Please use BaseSettings insetead in the future
Expand Down
4 changes: 2 additions & 2 deletions qlib/finco/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import json
from typing import Optional
from qlib.finco.conf import Config
from qlib.finco.utils import Singleton
from qlib.finco.utils import SingletonBaseClass
from qlib.finco.log import FinCoLog


class APIBackend(Singleton):
class APIBackend(SingletonBaseClass):
def __init__(self):
self.cfg = Config()
openai.api_key = self.cfg.openai_api_key
Expand Down
4 changes: 2 additions & 2 deletions qlib/finco/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging

from typing import Dict, List
from qlib.finco.utils import Singleton
from qlib.finco.utils import SingletonBaseClass
from contextlib import contextmanager


Expand Down Expand Up @@ -66,7 +66,7 @@ def formatting_log(logger, title="Info"):
logger.info("")


class FinCoLog(Singleton):
class FinCoLog(SingletonBaseClass):
# TODO:
# - config to file logger and save it into workspace
def __init__(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions qlib/finco/prompt_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from jinja2 import Template
import yaml

from qlib.finco.utils import Singleton
from qlib.finco.utils import SingletonBaseClass
from qlib.finco import get_finco_path


class PromptTemplate(Singleton):
class PromptTemplate(SingletonBaseClass):
def __init__(self) -> None:
super().__init__()
_template = yaml.load(open(Path.joinpath(get_finco_path(), "prompt_template.yaml"), "r"),
Expand Down
4 changes: 2 additions & 2 deletions qlib/finco/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,9 @@ def execute(self):
# TODO hotfix for the bug that the record signalrecord config is not updated
for record in target_config['task']['record']:
if record['class'] == 'SignalRecord':
if 'model' in record['kwargs']:
if 'kwargs' in record and 'model' in record['kwargs']:
del record['kwargs']["model"]
if 'dataset' in record['kwargs']:
if 'kwargs' in record and 'dataset' in record['kwargs']:
del record['kwargs']["dataset"]

# 4) save the config file
Expand Down
8 changes: 5 additions & 3 deletions qlib/finco/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
from fuzzywuzzy import fuzz


class Singleton:
class Singleton(type):
_instance = None

def __new__(cls, *args, **kwargs):
def __call__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls, *args, **kwargs)
cls._instance = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instance

class SingletonBaseClass(metaclass=Singleton):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this example before.
I can't understand why we need two level of class for Singleton?

pass

def parse_json(response):
try:
Expand Down