Refactor/move singleton out of config module (#3161)

This commit is contained in:
James Collins
2023-04-24 15:24:57 -07:00
committed by GitHub
parent 83b91a31bc
commit dfcbf6eee6
8 changed files with 10 additions and 9 deletions

24
autogpt/singleton.py Normal file
View File

@@ -0,0 +1,24 @@
"""The singleton metaclass for ensuring only one instance of a class."""
import abc
class Singleton(abc.ABCMeta, type):
"""
Singleton metaclass for ensuring only one instance of a class.
"""
_instances = {}
def __call__(cls, *args, **kwargs):
"""Call method for the singleton metaclass."""
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class AbstractSingleton(abc.ABC, metaclass=Singleton):
"""
Abstract singleton class for ensuring only one instance of a class.
"""
pass