Advertisement
Guest User

day_data.py

a guest
Jun 29th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. # Day_Data class
  2. # Stores a list of tupples (bool done, string "description")
  3. # This class tries to load saved instances from previous sessions
  4. # Bad form? Whatever. Gets the job done.
  5.  
  6. # TODO currently multi line strings for tasks will break the loader...
  7.  
  8. import os
  9.  
  10. class Day_Data(object):
  11.     def __init__(self, day, month, year, save_dir):
  12.         self.chores = []
  13.         self.day = day
  14.         self.month = month
  15.         self.year = year
  16.         # fname needs to be ddmmyyyy
  17.         self.fname = os.path.join(save_dir, day + month + year)
  18.         self.load_data() # Tries to load self on instanciation
  19.  
  20.     def add_chore(self, bool_done, desc):
  21.         self.chores.append([bool_done, desc])
  22.            
  23.     def delete_chore(self, index):
  24.         del self.chores[index]
  25.  
  26.     def get_index(self, index):
  27.         return self.chores[index]
  28.  
  29.     def get_num_chores(self):
  30.         return len(self.chores)
  31.  
  32.     def load_data(self):
  33.         try:
  34.             with open(self.fname, "r") as text_file:
  35.                 del self.chores[:] # Clear current list
  36.                 iter = int(text_file.readline())
  37.                 for i in range(iter):
  38.                     done = bool(text_file.readline().strip())
  39.                     chore = text_file.readline().strip()
  40.                     self.add_chore(done, chore)
  41.  
  42.         except IOError:
  43.             print self.fname + " Does not exist, creating new instance..."
  44.    
  45.     def save_data(self):
  46.         if len(self.chores): # If the object contains nothing, don't write
  47.             with open(self.fname,"w") as text_file:
  48.                 text_file.write(str(len(self.chores)) + "\n")
  49.                 for i in self.chores:
  50.                     print >> text_file, i[0]
  51.                     print >> text_file, i[1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement