def test_writing_file(self): fb = FitbitCache('test_user') if os.path.exists(fb.filename): os.remove(fb.filename) populate_fb_object(fb) fb.write() self.assertTrue(os.path.exists(fb.filename))
def test_adding_data(self): fb = FitbitCache('test_user') date = datetime.date.today() i = 0 for k, v in test_data[0].items(): fb.add_item(date, k, v) i += 1 self.assertEqual(len(fb), 1) self.assertTrue(date in fb) self.assertEqual(len(fb[date]), i)
def test_reading_file(self): fb = FitbitCache('test_user') (dates, items) = populate_fb_object(fb) fb.write() test_object = FitbitCache('test_user') test_object.read() self.assertEqual(len(test_object), dates) i = 0 for day in test_object: for item in test_object[day]: i += 1 self.assertEqual(i, items)
from optparse import OptionParser usage = "usage: %prog user-name filename" parser = OptionParser(usage) (options, args) = parser.parse_args() if len(args) != 2: parser.print_help() exit(1) else: user_name = args[0] filename = args[1] cache = FitbitCache(user_name) cache.read() df = pd.read_csv(filename) df['start'] = pd.to_datetime(df.Start) df = df[['start', 'Distance (mi)', 'Steps (count)', 'Flights Climbed (count)']] df.columns = ['start', 'distance', 'steps', 'flights'] df = df.head(len(df) - 1) # need to drop last day because it's a partial day for key, row in df.iterrows(): date = row.start.date() # convert from pandas timestamp to date if date in cache: # round values to the precision we want
try: value = float(value) except: pass cache.add_item(day, name, value) item_count += 1 return item_count if __name__ == '__main__': if len(sys.argv) != 3: print('Usage: python add_csv_to_user.py user_name csv_file.csv') exit(1) user_name = sys.argv[1] fbcache = FitbitCache(user_name) if fbcache.data_exists(): fbcache.read() fn = sys.argv[2] if not os.path.exists(fn): print('Filename {} does not exist'.format(fn)) print('Usage: python add_csv_to_user.py user_name csv_file.csv') exit(1) count = add_csv_data(fbcache, fn) fbcache.write() print('Items added = {}'.format(count))
def test_object_creation(self): fb = FitbitCache('test_user') self.assertTrue(isinstance(fb, dict)) self.assertEqual(fb.user_name, 'test_user')
def test_missing_days(self): fb = FitbitCache('test_user') (dates, items) = populate_fb_object(fb, day_incr=2) x = fb.find_missing_days() self.assertEqual(len(x), dates - 1) # given n days with a gap, there are n-1 missing
def test_num_days(self): fb = FitbitCache('test_user') (dates, items) = populate_fb_object(fb) self.assertEqual(fb.num_days(), dates)
def dump_data(fp, debug): stats = cache.write_as_csv(fp) if debug: print(stats, file=sys.stderr) if __name__ == '__main__': (options, args) = parser.parse_args() if len(args) != 1: parser.print_help() exit(1) else: user_name = args[0] cache = FitbitCache(user_name) if not cache.data_exists(): print( "ERROR: Could not open data for user '{}'\n".format(user_name)) parser.print_help() exit(1) else: cache.read() if options.verbose: debug = True else: debug = False if options.filename: fp = open(options.filename, 'w') if debug:
parser.add_option("-c", "--commit", action="store_true", dest="commit", help="commit changes to database") (options, args) = parser.parse_args() if len(args) != 1: parser.print_help() exit(1) else: user_name = args[0] cache = FitbitCache(user_name) if cache.data_exists(): cache.read() else: parser.print_help() print('\n ERROR: user {} does not have any data'.format(user_name)) exit(1) print("-------------- User: {} ----------------------".format(user_name)) print("\nDatabase contains %d entries" % len(cache.daylist())) empty_days = cache.remove_days_without_steps() print("\nRemoving days with zero steps") for x in empty_days: print(" %s" % x)
from optparse import OptionParser usage = "usage: %prog user-name filename" parser = OptionParser(usage) (options, args) = parser.parse_args() if len(args) != 2: parser.print_help() exit(1) else: user_name = args[0] filename = args[1] cache = FitbitCache(user_name) cache.read() df = pd.read_csv(filename) df['start'] = pd.to_datetime(df.Start) df = df[['start', 'Distance (mi)', 'Steps (count)', 'Flights Climbed (count)']] df.columns = ['start', 'distance', 'steps', 'flights'] df = df.head(len(df) - 1) # need to drop last day because it's a partial day for key, row in df.iterrows(): date = row.start.date() # convert from pandas timestamp to date if date in cache: # round values to the precision we want row.steps = int(round(row.steps, 0)) row.distance = round(row.distance, 2)
parser.add_argument('user_name', help='name of user to check') args = parser.parse_args() allowed_spm_error = 500 steps_per_mile = 2000 step_total = 0 dist_total = 0 if args.distance: print('will check distance values') else: print('will NOT check distance values') user_name = args.user_name cache = FitbitCache(user_name) if not cache.data_exists(): print("ERROR: Could not open data for user '{}'\n".format(user_name)) parser.print_help() exit(1) cache.read() days = cache.daylist() current_date = days[0] end_date = days[-1] print('Start = {}'.format(days[0])) print('End = {}'.format(days[-1])) print('Num Days = {}'.format(cache.num_days()))
from __future__ import print_function from __future__ import unicode_literals from fbcache import FitbitCache from optparse import OptionParser usage = "usage: %prog user-name" parser = OptionParser(usage) (options, args) = parser.parse_args() if len(args) != 1: parser.print_help() exit(1) else: user_name = args[0] cache = FitbitCache(user_name) if not cache.data_exists(): print("ERROR: Could not open data for user '{}'\n".format(user_name)) parser.print_help() exit(1) cache.read() days = cache.daylist() print('Start = {}'.format(days[0])) print('End = {}'.format(days[-1])) print('Num Days = {}'.format(cache.num_days()))
def dump_data(fp, debug): stats = cache.write_as_csv(fp) if debug: print(stats, file=sys.stderr) if __name__ == '__main__': (options, args) = parser.parse_args() if len(args) != 1: parser.print_help() exit(1) else: user_name = args[0] cache = FitbitCache(user_name) if not cache.data_exists(): print("ERROR: Could not open data for user '{}'\n".format(user_name)) parser.print_help() exit(1) else: cache.read() if options.verbose: debug = True else: debug = False if options.filename: fp = open(options.filename, 'w') if debug: print('Writing data to file: {}'.format(options.filename))