-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_data.py
More file actions
77 lines (63 loc) · 2.73 KB
/
add_data.py
File metadata and controls
77 lines (63 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import csv
import argparse
import datetime
cat_aliasing = {'cleopatra': ['c', 'cleo', 'cleopatra'],
'amumu': ['m', 'mumu', 'amumu']}
attribute_aliasing = {'weight': ['w', 'weight', 'm', 'mass'],
'injury': ['injury', 'inj', 'i']}
class ArgHolder:
def __init__(self):
self.cat = None
self.attribute = None
self.value = None
self.date = None
def create_data_dict(args):
# Put all of the data into the dictionary first
data_dict = {'date': datetime.date.today() if args.date is None else datetime.datetime.strptime(args.date, "%d%m%Y").date(),
'cat': args.cat,
'attribute': args.attribute,
'value': args.value}
# Then parse through to get it in the right format
data_dict['date'] = data_dict['date'].strftime("%m/%d/%Y")
for name, aliases in cat_aliasing.items():
if args.cat.casefold() in aliases:
data_dict['cat'] = name
for name, aliases in cat_aliasing.items():
if args.attribute.casefold() in aliases:
data_dict['attribute'] = name
if args.value == -99:
data_dict['value'] = None
return data_dict
def append_dict_to_csv(args, file_path='./cat_health.csv', fieldnames=['date', 'cat', 'attribute', 'value']):
# Appends a dictionary as a row to a CSV file.
with open(file_path, mode='a', newline='') as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
# If the file is empty, write the header
if file.tell() == 0:
writer.writeheader()
data_dict = create_data_dict(args)
writer.writerow(data_dict)
def main():
parser = argparse.ArgumentParser(prog='cat_log',
description="Process some integers.")
parser.add_argument("cat",
metavar="cat_name",
type=str,
help="The name of the cat for the data record.")
parser.add_argument("attribute",
metavar="cat_attribute",
type=str,
help="The attribute of the cat for the data record.")
parser.add_argument("value",
metavar="data_value",
type=float,
help="The value of the measurement taken (e.g. the weight (in grams) of the cat)")
parser.add_argument("--date",
metavar="measurement_date",
type=str,
help="The date that the measurement was taken. In the format \"%d%m%Y\".")
runtime_args = ArgHolder()
parser.parse_args(namespace=runtime_args)
append_dict_to_csv(runtime_args)
if __name__ == '__main__':
main()