简体   繁体   中英

Storing dictionary objects using python shelves

I am trying to implement a Doctor's weekly scheduling appointment system. I would like to know how can i use shelve to store my dictionary objects. It would be great if someone can review my code and suggest how I can improve it further. How can i store timeslot dictionary into shelves? (I have appended Monday,Tue,wed,thursday and Friday objects into timeslot dictionary and now im unsure of how can I store these objects in dictionary into shelves.)

 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 

Disclaimer I am a beginner for python and shelves.

Thanks for helping!

I fixed your code as it does not run :

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

You tried to use private attributes using double underscores inside default_timeslots class and this is bad practice. + You are trying to access them outside the class. I removes the underscores.

I have no idea why you use class attribute and methods that look like class methods. I let you read this to understand why the class attributes won't be saved in your shelve. That's why I replaced your class attributes by instance attributes.

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

To create shelve :

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

To read from shelve :

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

Of course you need to import the Store class when you try to read from shelve (if that is not inside the same script).

store.timeslots['Mon'].time

will return :

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

You should get away from SHELVES, and other text based data storage. Instead use SQLITE3 and convert your dictionary into a json format and save it inside the database.

Why SQLITE3 ? It is better supported, it is an actual database, and can be brought over to other systems. It handles errors better and isn't as easily corrupted. It won't be deprecated either.

If you want actual indexed JSON storage, postgres is another option that is perfect for this situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM