使用 python 个书架存储字典对象

Storing dictionary objects using python shelves

我正在尝试实施医生的每周预约系统。我想知道如何使用 shelve 来存储我的字典对象。 如果有人可以查看我的代码并建议我如何进一步改进它,那就太好了。 如何将时隙字典存储到货架上? (我已将星期一、星期二、星期三、星期四和星期五对象附加到时间段字典中,现在我不确定如何将字典中的这些对象存储到货架中。)

class default_timeslots:
    def __init__(self,day,time):
        self.__day = day
        self.__time = time

class Store:
    timeslots = {}
    def __init__(self):
        if len(__class__.timeslots) <= 0:
            self.create_default_timeslots()

    def create_default_timeslots(self):
        Monday = default_timeslots('Mon',time={"8:00","9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00"})
        Tuesday = default_timeslots('Tue',
                                   time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
                                             "17:00", "18:00"})
        Wednesday = default_timeslots('Wed',
                                   time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
                                             "17:00", "18:00"})
        Thursday = default_timeslots('Thursday',
                                   time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
                                             "17:00", "18:00"})
        Friday = default_timeslots('Friday',
                                   time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
                                             "17:00", "18:00"})

        Store.timeslots[Monday.day] = Monday
        Store.timeslots[Tuesday.day] = Tuesday
        Store.timeslots[Wednesday.day] = Wednesday
        Store.timeslots[Thursday.day] = Thursday
        Store.timeslots[Friday.day] = Friday

免责声明我是python和上架的初学者。

感谢您的帮助!

我修复了你的代码,因为它没有 运行 :

class default_timeslots:
    def __init__(self,day,time):
        self.day = day
        self.time = time

class Store:
    def __init__(self):
        self.timeslots = {}
        self.create_default_timeslots()

    def create_default_timeslots(self):
        Monday = default_timeslots('Mon',time={"8:00","9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00"})
        Tuesday = default_timeslots('Tue',
                                   time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
                                             "17:00", "18:00"})
        Wednesday = default_timeslots('Wed',
                                   time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
                                             "17:00", "18:00"})
        Thursday = default_timeslots('Thursday',
                                   time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
                                             "17:00", "18:00"})
        Friday = default_timeslots('Friday',
                                   time={"8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00",
                                             "17:00", "18:00"})

        self.timeslots[Monday.day] = Monday
        self.timeslots[Tuesday.day] = Tuesday
        self.timeslots[Wednesday.day] = Wednesday
        self.timeslots[Thursday.day] = Thursday
        self.timeslots[Friday.day] = Friday

您试图在 default_timeslots class 中使用双下划线来使用私有属性,这是不好的做法。 + 您正在尝试在 class 之外访问它们。我删除了下划线。

我不知道你为什么使用 class 属性和看起来像 class 方法的方法。我让您阅读 this 以了解为什么 class 属性不会保存在您的货架中。这就是为什么我用实例属性替换了你的 class 属性。

Similarly, when class instances are pickled, their class’s code and data are not pickled along with them. Only the instance data are pickled. This is done on purpose, so you can fix bugs in a class or add methods to the class and still load objects that were created with an earlier version of the class

创建搁置:

import shelve
store = Store()
output_shelve = shelve.open('test')
output_shelve['store'] = store
output_shelve.close()

从书架上阅读:

input_shelve = shelve.open('test')
store = input_shelve['store']

当然,当您尝试从搁置中读取内容时(如果不在同一个脚本中),您当然需要导入商店 class。

store.timeslots['Mon'].time

将 return :

{'10:00',
 '11:00',
 '12:00',
 '13:00',
 '14:00',
 '15:00',
 '16:00',
 '17:00',
 '18:00',
 '8:00',
 '9:00'}

您应该远离 SHELVES 和其他基于文本的数据存储。而是使用 SQLITE3 并将您的字典转换为 json 格式并将其保存在数据库中。

为什么SQLITE3?它得到了更好的支持,它是一个实际的数据库,可以带到其他系统中。它可以更好地处理错误并且不容易损坏。它也不会被弃用。

如果您想要实际的索引 JSON 存储,postgres 是另一个非常适合这种情况的选项。