To enable subclassing for the TinyDB class, you should use the classmethod decorator and a factory function to create instances of the class. Here's an updated version of the TinyDB class with subclassing enabled:
python
from typing import Optional, Callable
import tinydb
class TinyDBManager:
@classmethod
def from_config(cls, config: dict, storage: Optional[str] = None, table: Optional[str] = None) -> 'TinyDBManager':
if storage is None:
storage = config.get('storage', TinyDB.DEFAULT_STORAGE)
if table is None:
table = config.get('table', TinyDB.DEFAULT_TABLE)
# Prepare the storage
tinydb.register_type(TinyDBManager)
db_manager = TinyDBManager(storage=storage, table=table)
# Apply the config options to the instance
for key, value in config.items():
setattr(db_manager, key, value)
return db_manager
def __init__(self, storage: str = TinyDB.DEFAULT_STORAGE, table: str = TinyDB.DEFAULT_TABLE):
self.storage = storage
self.table = table
self._opened = False
def __getattribute__(self, name):
if name == '__getattribute__':
return super().__getattribute__(name)
return getattr(self, name)
def __getattr__(self, name):
try:
return self._get_attribute(name)
except AttributeError:
raise AttributeError(f"{self.__class__.__name__} has no attribute: {name}")
def __get_attribute(self, name):
if name == 'storage':
return self.storage
elif name == 'table':
return self.table
else:
raise AttributeError
Now, when you create a TinyDBManager instance, it will be a subclass with all the attributes you set during instantiation. Here's an example of creating a subclass: