当我尝试向存储库中添加某些内容时,出现“输入用尽”错误?为什么会这样呢?泡菜文件现在是空的吗?这是我第一次使用泡菜(在uni上做作业)。
class PickleRepo(Repo):
def __init__(self, filename, read_entity, write_entity):
super().__init__()
self.__filename = filename
self.__read_entity = read_entity
self.__write_entity = write_entity
def _read_all_from_file(self):
self._items_dictionary = {}
with open(self.__filename, 'rb') as file:
lines = pickle.load(file)
for line in lines:
line = line.strip()
if line != "":
entity = self.__read_entity(line)
self._items_dictionary[entity.id] = entity
def _write_all_to_file(self):
with open(self.__filename, 'wb') as file:
for entity in self._items_dictionary.values():
line = self.__write_entity(entity)
file.write(line + "\n")
def add_item(self, new_item):
self._read_all_from_file()
super().add_item(new_item)
self._write_all_to_file()
def search_item(self, attribute, search_value):
self._read_all_from_file()
return super().search_item(attribute, search_value)
def update_item(self, id, attribute, new_value):
self._read_all_from_file()
super().update_item(id, attribute, new_value)
self._write_all_to_file()
def remove_item(self, id):
self._read_all_from_file()
object = super().remove_item(id)
self._write_all_to_file()
return object
这在用户界面中称为:
def __add_new_client(self, command):
clientId = command[0]
name = command[1]
try:
FunctionsManager.add_undo_operation(self.__client_service, UndoHandler.return_movie_entry,
int(clientId))
operation = partial(self.__add_new_client,command)
FunctionsManager.add_redo_operation(operation)
self.__client_service.add_new_client(int(clientId), name)
except ValueError:
print('Client ID must be an integer!')
然后它就在这里: def add_new_client(self,clientId,name): new_client =客户端(clientId,名称) self .__ validate_clients.negative_integer_id(clientId) self._repository.add_item(new_client)