Advertisement
gruntfutuk

simple health records

Sep 1st, 2020
1,398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. import datetime
  2. from collections import namedtuple
  3.  
  4. def gettime():
  5.     return datetime.datetime.now()
  6.  
  7. def menu_option(menu):
  8.     print('\nMENU\n')
  9.     for option, details in menu.items():
  10.         print(f'{option}:  {details.desc}')
  11.     print('\n')
  12.     while True:
  13.         option = input('Option? ').strip().lower()
  14.         if option in menu:
  15.             return menu[option]
  16.         print('Sorry, that option is not available')
  17.  
  18. def people_option(people):
  19.     print('\nPEOPLE\n')
  20.     for counter, person in enumerate(people, start=1):
  21.         print(f'{counter}:  {person}')
  22.     print('\n')
  23.     while True:
  24.         option = input('Person? ').strip()
  25.         if option and option.isdigit():
  26.             option = int(option)
  27.             if 1 <= option <= len(people):
  28.                 return people[option-1]
  29.         print('Sorry, that option is not available')
  30.        
  31. def write_data(prompt, person, info):
  32.     vaalue = input(prompt)
  33.     if vaalue:
  34.         try:
  35.             with open(f"{person}{info.prefix}", "a") as f:
  36.                 f.write(str([str(gettime())]) + ": " + vaalue + "\n")
  37.             print("written successfully")
  38.         except (IOError, ValueError, EOFError) as e:
  39.             print(e)
  40.         except:
  41.             print("Strange error, sorry")
  42.         finally:
  43.             print()
  44.  
  45. def read_data(person, info):
  46.     print(f'\n{info.desc} report for {person}:')
  47.     try:
  48.         with open(f"{person}{info.prefix}") as f:
  49.             for line in f:
  50.                   print(line.strip())
  51.     except (IOError, ValueError, EOFError) as e:
  52.         print(e)
  53.     except:
  54.         print("Strange error, sorry")
  55.     finally:
  56.         print()
  57.                  
  58. def entry(person, info):
  59.     prompt = f"Data for {info.desc} for {person}: "
  60.     write_data(prompt, person, info)
  61.  
  62. def report(person, info):
  63.     read_data(person, info)
  64.                  
  65. def exercise(person, option):
  66.     prompt = "Input here to store data: \n"
  67.     write_date(person, option, prompt)
  68.    
  69. def food(person, option):
  70.     prompt = "Input here to store data: \n"
  71.     write_date(person, option, prompt)
  72.  
  73.  
  74. Menu = namedtuple('Menu', 'desc prefix func')
  75. actions = {"1": Menu("Enter data", "", entry),
  76.            "2": Menu("Report", "", report),
  77.            "q": Menu("exit", "", None),
  78.            }
  79. kind = {"1": Menu("exercise", "_ex.txt", exercise),
  80.         "2": Menu("food", "_food.txt", food),
  81.         "q": Menu("exit", "", None),
  82.         }
  83. people = ["brain", "lara", "jason"]
  84.  
  85. print("\n          Welcome to Health Management Program         \n")
  86. while True:
  87.     action = menu_option(actions)
  88.     if not action.func:
  89.         break
  90.     person = people_option(people)
  91.     info = menu_option(kind)
  92.     if info.func:
  93.         action.func(person, info)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement