-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessData.py
More file actions
executable file
·90 lines (71 loc) · 2.49 KB
/
ProcessData.py
File metadata and controls
executable file
·90 lines (71 loc) · 2.49 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
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
import csv
import sys
import getopt
import datetime
import numpy
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def usage():
print("You should provide 3 arguments: Start date, End date and commodity type [gold/silver]")
print("Start date and end date should conform to format: %y-%m-%d")
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "h", ["help"])
except getopt.error as msg:
raise Usage(msg)
except Usage as err:
print(sys.stderr, err.msg)
print(sys.stderr, "for help use --help")
return 2
for o, _ in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
else:
assert False, "unhandled option"
if len(args) < 3:
raise Usage("ERROR! Not enough arguments!")
start_date_str = args[0]
end_date_str = args[1]
commodity = args[2].lower()
try:
start_date = datetime.datetime.strptime(start_date_str, '%y-%m-%d')
end_date = datetime.datetime.strptime(end_date_str, '%y-%m-%d')
except Exception:
print(sys.stderr, "Start date or/and end date has wrong format")
print(sys.stderr, "for help use --help")
return 3
if start_date >= end_date:
print(sys.stderr, "End date should be higher than start date")
print(sys.stderr, "for help use --help")
return 4
if commodity not in ["gold", "silver"]:
print(sys.stderr, "Commodity should be one of the following: gold, silver")
print(sys.stderr, "for help use --help")
return 5
prices = []
with open('{}.csv'.format(commodity), 'r') as csvfile:
entries = csv.reader(csvfile, delimiter='\t', quotechar='|')
for date_str, price in entries:
try:
date = datetime.datetime.strptime(date_str, '%y-%m-%d')
except Exception:
print(sys.stderr, "Stored data is corrupted")
print(sys.stderr, "for help use --help")
return 6
if date >= start_date and date <= end_date:
prices.append(float(price))
if len(prices) < 1:
print(sys.stderr, "No entries match set dates")
print(sys.stderr, "for help use --help")
return 7
mean = numpy.mean(prices)
variance = numpy.var(prices)
print(commodity, mean, variance)
if __name__ == "__main__":
sys.exit(main())