mekasu0124

Untitled

Dec 5th, 2023
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.27 KB | None | 0 0
  1. # import necessary libraries
  2. import datetime
  3. import json
  4. import os
  5. import time
  6.  
  7. from datetime import datetime
  8.  
  9. # instantiate class to handle program
  10. class Program:
  11.     # create init method to initialize variables, etc.
  12.     def __init__(self):
  13.         # create a time object for a time to check for
  14.         # self.time_to_check = "08:00:00"
  15.         self.time_to_check = "21:20:00"
  16.         # call the main function to run after initialization
  17.         self.main()
  18.  
  19.     """
  20.    The main program runs all the official checks.
  21.    It starts by checking if there is already an entry for the day that has been logged.
  22.    If an entry for the current date exists, it sleeps. Otherwise, it runs the rest of the program.
  23.    """
  24.     def main(self):
  25.         # create datetime object
  26.         now = datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
  27.  
  28.         # check if json file already has a log for today
  29.         # if so, tell the program to sleep for 1 hour
  30.         if self.check_for_entry(now):
  31.             # check if time equals 8am
  32.             if self.check_time():
  33.                 self.start_program(now)
  34.             else:
  35.                 time.sleep(10)
  36.                 self.main()
  37.        
  38.         # if log does not exists, tell the program to run as normal
  39.         else:
  40.             # check if time equals 8am
  41.             if self.check_time():
  42.                 self.start_program(now)
  43.             else:
  44.                 time.sleep(10)
  45.                 self.main()
  46.  
  47.     def start_program(self, now):
  48.         # using multi-line string to put 3 print statements together for easier usage
  49.         msg = """
  50.                It's Time To Take Your Medicine!
  51.                Enter 'y' for Yes or 'n' for No
  52.                If you've taken your medicine or not.
  53.                """
  54.        
  55.         # print message
  56.         print(msg)
  57.  
  58.         # get user input
  59.         user_input = input("Your Entry: ")
  60.  
  61.         # use a while loop to get user input give that input does not equal 'y' or 'n'
  62.         while user_input.lower() not in ('y', 'n'):
  63.             print("\nInvalid Input. Enter 'y' or 'n'")
  64.  
  65.             user_input = input("Your Entry: ")
  66.  
  67.         # if user enters 'y' then record medicine taken entry
  68.         if user_input.lower() == 'y':
  69.             # call write to json function with correct parameters of the current datetime, yes, and the reason
  70.             self.write_to_json(now, "yes", "Medicine Taken")
  71.            
  72.         # if the user enters 'n' then request reason for not taking medicine, then record to logs
  73.         elif user_input.lower() == 'n':
  74.             # tell user they entered no
  75.             print("You Entered No for Not taking your medicine.")
  76.  
  77.             # request reason for not taking mediciation
  78.             reason = input("Enter Reason For Not Taking Medicine:\n")
  79.            
  80.             # call write to json with necessary parameters
  81.             self.write_to_json(now, "no", reason)
  82.  
  83.     def check_time(self):
  84.         # check if current time equals 8 am
  85.         if datetime.now().strftime("%H:%M:%S") == self.time_to_check:
  86.             # time matches, return True
  87.             return True
  88.         else:
  89.             # time does not match, return False
  90.             return False
  91.  
  92.     def write_to_json(self, datetime, yes_or_no, reason):
  93.         pass
  94.  
  95.     def check_for_entry(self, date):
  96.         # call function to check for file exist
  97.         self.check_for_file()
  98.  
  99.         # open the json file
  100.         with open("./logs.json", 'r', encoding="utf-8-sig") as f:
  101.             # load the data into a data variable
  102.             data = json.load(f)
  103.  
  104.             # check if the date exists within the json files keys
  105.             if date not in data.keys():
  106.                 # if the date is not found, return false
  107.                 return False
  108.  
  109.             # the date was found, return True
  110.             return True
  111.                
  112.     def check_for_file(self):
  113.         # check if the file exists using the os library
  114.         if not os.path.exists("./logs.json"):
  115.             # file was not found, create the new file
  116.             with open("./logs.json", 'w', encoding="utf-8-sig") as f:
  117.                 # dump the empty dictionary into the new file
  118.                 data = json.dump({}, f, indent=4)
  119.  
  120. if __name__ == '__main__':
  121.   Program()
Advertisement
Add Comment
Please, Sign In to add comment