1 回答

TA貢獻1848條經驗 獲得超6個贊
我沒有檢查以下是否編譯,但它應該如下所示。如果您發現需要更改的內容并嘗試編譯它,請有人編輯此答案:
class BaseDataRepository(ABC):?
? ? @abstractmethod
? ? def get_all_ids(self) -> List[int]:?
? ? ? ? pass
? ? @abstractmethod
? ? def get_data_from_id(self, _id: int) -> object:?
? ? ? ? pass
class InMemoryDataRepository(BaseDataRepository):?
? ? def __init__(self, ids, data):?
? ? ? ? self.ids: List[int] = ids
? ? ? ? self.data: Dict[int, object] = data
? ? def get_all_ids(self) -> List[int]:?
? ? ? ? return list(self.ids)
? ? def get_data_from_id(self, _id: int) -> object:?
? ? ? ? return self.data[_id]
class ConvertIDsToLoadedData(BaseStep):?
? ? def _transform_data_container(self, data_container: DataContainer, context: ExecutionContext):?
? ? ? ? repo: BaseDataRepository = context.get_service(BaseDataRepository)
? ? ? ? ids = data_container.data_inputs
? ? ? ? # Replace data ids by their loaded object counterpart:?
? ? ? ? data_container.data_inputs = [repo.get_data_from_id(_id) for _id in ids]
? ? ? ? return data_container, context
context = ExecutionContext('caching_folder').set_service_locator({
? ? BaseDataRepository: InMemoryDataRepository(ids, data)? # or insert here any other replacement class that inherits from `BaseDataRepository` when you'll change the database to a real one (e.g.: SQL) rather than a cheap "InMemory" stub.?
})
添加回答
舉報