def status(args): """ Print the user's calories. """ day = datetime.date.today() - datetime.timedelta(days=args.yesterday) day_key = to_key(day) total = db.data['calories'].get(day_key, 0) print_status( total=total, date_offset=args.yesterday, day=day, )
def eat(args): """ eat is the method that executes either a lookup for the calories of a named piece of food from the food database or uses the calories provided by the user and adds them (multiplied by the number of portions) to the calorie database for the right day. args is an argparse Namespace object. """ # if we have to look the calories up... if args.food: # food_db contains dict with Food(namedtuple) values try: calories_base = db.data['food'][args.food]['cal'] except KeyError: results = search_food(args.food) if len(results) == 1: message = ( "No match for '{}', but found '{}'. Using this instead." .format(args.food, results[0])) print(message) calories_base = db.data['food'][results[0]]['cal'] else: print("Could not find '{}' in the food database." .format(args.food)) sys.exit(1) # ...otherwise, we take the user-provided value else: calories_base = args.calories day = datetime.date.today() - datetime.timedelta(days=args.yesterday) day_key = to_key(day) # calories_base times portion size # product will be added to the daily total calories_to_add = calories_base * args.number try: db.data['calories'][day_key] += calories_to_add except KeyError: db.data['calories'][day_key] = calories_to_add db.write_data() # print status _after_ file is written print_status( total=db.data['calories'][day_key], date_offset=args.yesterday, day=day, added=calories_to_add, )