Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import necessary libraries
- import datetime
- import json
- import os
- import time
- from datetime import datetime
- # instantiate class to handle program
- class Program:
- # create init method to initialize variables, etc.
- def __init__(self):
- # create a time object for a time to check for
- # self.time_to_check = "08:00:00"
- self.time_to_check = "21:20:00"
- # call the main function to run after initialization
- self.main()
- """
- The main program runs all the official checks.
- It starts by checking if there is already an entry for the day that has been logged.
- If an entry for the current date exists, it sleeps. Otherwise, it runs the rest of the program.
- """
- def main(self):
- # create datetime object
- now = datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
- # check if json file already has a log for today
- # if so, tell the program to sleep for 1 hour
- if self.check_for_entry(now):
- # check if time equals 8am
- if self.check_time():
- self.start_program(now)
- else:
- time.sleep(10)
- self.main()
- # if log does not exists, tell the program to run as normal
- else:
- # check if time equals 8am
- if self.check_time():
- self.start_program(now)
- else:
- time.sleep(10)
- self.main()
- def start_program(self, now):
- # using multi-line string to put 3 print statements together for easier usage
- msg = """
- It's Time To Take Your Medicine!
- Enter 'y' for Yes or 'n' for No
- If you've taken your medicine or not.
- """
- # print message
- print(msg)
- # get user input
- user_input = input("Your Entry: ")
- # use a while loop to get user input give that input does not equal 'y' or 'n'
- while user_input.lower() not in ('y', 'n'):
- print("\nInvalid Input. Enter 'y' or 'n'")
- user_input = input("Your Entry: ")
- # if user enters 'y' then record medicine taken entry
- if user_input.lower() == 'y':
- # call write to json function with correct parameters of the current datetime, yes, and the reason
- self.write_to_json(now, "yes", "Medicine Taken")
- # if the user enters 'n' then request reason for not taking medicine, then record to logs
- elif user_input.lower() == 'n':
- # tell user they entered no
- print("You Entered No for Not taking your medicine.")
- # request reason for not taking mediciation
- reason = input("Enter Reason For Not Taking Medicine:\n")
- # call write to json with necessary parameters
- self.write_to_json(now, "no", reason)
- def check_time(self):
- # check if current time equals 8 am
- if datetime.now().strftime("%H:%M:%S") == self.time_to_check:
- # time matches, return True
- return True
- else:
- # time does not match, return False
- return False
- def write_to_json(self, datetime, yes_or_no, reason):
- pass
- def check_for_entry(self, date):
- # call function to check for file exist
- self.check_for_file()
- # open the json file
- with open("./logs.json", 'r', encoding="utf-8-sig") as f:
- # load the data into a data variable
- data = json.load(f)
- # check if the date exists within the json files keys
- if date not in data.keys():
- # if the date is not found, return false
- return False
- # the date was found, return True
- return True
- def check_for_file(self):
- # check if the file exists using the os library
- if not os.path.exists("./logs.json"):
- # file was not found, create the new file
- with open("./logs.json", 'w', encoding="utf-8-sig") as f:
- # dump the empty dictionary into the new file
- data = json.dump({}, f, indent=4)
- if __name__ == '__main__':
- Program()
Advertisement
Add Comment
Please, Sign In to add comment