我想用“搁置”来写模块,但是输入3个数据后,我无法再输入并且文件中的密钥存储不正确

发布于 2025-01-10 08:06:47 字数 1546 浏览 2 评论 0原文

我想使用'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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文