1

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!

  • What exactly do you want here? Please be clear as your question is too broad. – ycx Dec 17 '18 at 15:29
  • sorry my bad I'm very new to stack overflow. I was trying to ask how can i store dictionary - timeslots into shelves as I have appended Monday,Tuesday ,Wednesday,Thursday and Friday objects into the dictionary - timeslots . Im unsure of how can I store these objects that are in dictionary in shelves. –  Dec 17 '18 at 15:36
  • What is a shelve? It is not addressed anywhere in your code. – ycx Dec 17 '18 at 15:38
  • i think that he means https://docs.python.org/3/library/shelve.html – Corentin Limier Dec 17 '18 at 15:39
  • yes i meant that –  Dec 17 '18 at 15:42

2 Answers2

0

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'}
Corentin Limier
  • 4,946
  • 1
  • 13
  • 24
0

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.

eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20