我想用“搁置”来写模块,但是输入3个数据后,我无法再输入并且文件中的密钥存储不正确
我想使用'shelve'模块编写,但是输入3个数据后,我无法再输入并且文件中的键存储不正确['3','1','2']。这就是在linux上发生的事情,在windows上一切都很好
class BasicModel(ABC):
table = '.db'
def __init__(self):
self.id = None
self.created_date = None
self.updated_date = None
self.isValid = True
def save(self):
if self.isValid:
path = getAbsolutePath(self.table)
with shelve.open(path, writeback=True) as db:
# Shu obj yangi yaratilayotgani
if self.id is None:
self.created_date = datetime.now()
if list(db.keys()):
print(list(db.keys()))
self.id = int(list(db.keys())[-1]) + 1
else:
self.id = '1'
else:
self.updated_date = datetime.now()
db[str(self.id)] = self
else:
raise ValueError("Qiymatlardan biri noto'g'iri")
def delete(self):
with shelve.open(getAbsolutePath(self.table)) as db:
del db[str(self.id)]
def objects(table: str):
with shelve.open(getAbsolutePath(table)) as db:
for obj in db.values():
yield obj
@property
def isValid(self):
return self.__isValid
@isValid.setter
def isValid(self, value):
self.__isValid = value
class Brand(BasicModel):
table = 'Brands'
def __init__(self, name):
super().__init__()
self.name = name
I want to write using the 'shelve' module, but after entering 3 data, I can't enter anymore and the keys in the file are stored incorrectly ['3', '1', '2']. that's what happens on linux, and it's all good on windows
class BasicModel(ABC):
table = '.db'
def __init__(self):
self.id = None
self.created_date = None
self.updated_date = None
self.isValid = True
def save(self):
if self.isValid:
path = getAbsolutePath(self.table)
with shelve.open(path, writeback=True) as db:
# Shu obj yangi yaratilayotgani
if self.id is None:
self.created_date = datetime.now()
if list(db.keys()):
print(list(db.keys()))
self.id = int(list(db.keys())[-1]) + 1
else:
self.id = '1'
else:
self.updated_date = datetime.now()
db[str(self.id)] = self
else:
raise ValueError("Qiymatlardan biri noto'g'iri")
def delete(self):
with shelve.open(getAbsolutePath(self.table)) as db:
del db[str(self.id)]
def objects(table: str):
with shelve.open(getAbsolutePath(table)) as db:
for obj in db.values():
yield obj
@property
def isValid(self):
return self.__isValid
@isValid.setter
def isValid(self, value):
self.__isValid = value
class Brand(BasicModel):
table = 'Brands'
def __init__(self, name):
super().__init__()
self.name = name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论