-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
128 lines (103 loc) · 4.26 KB
/
test.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import csv
import helpers
#constants
ID = 0
DATE_TIME = 1
STATUS = 3
CHECK_IN = 0
CHECK_OUT = 1
f = open('./files/may.dat', 'r')
lines = f.readlines()
array, chin, chout = [], [], []
dates = {}
for line in lines:
divByTab = line.split("\t")
empid = int(divByTab[ID].replace(" ", ""))
date, time = divByTab[DATE_TIME].split(" ")
pun_stat = int(divByTab[STATUS])
if len(array) == 0:
#print 1
if pun_stat == CHECK_IN:
array.append( { date : { empid : { 'checkin' : [ time ], 'checkout' : [ 'none' ] } } } )
elif pun_stat == CHECK_OUT: # Useless, i know
array.append( { date : { empid : { 'checkin' : [ 'none' ], 'checkout' : [ time ] } } } )
else:
for day in array:
if date in day:
if empid in day[date]:
if pun_stat == CHECK_IN:
if (len(day[date][empid]['checkin']) > 0) and day[date][empid]['checkin'][-1] != 'none':
# check in without a checkout is passed
continue
else:
array[array.index(day)][date][empid]['checkin'].append(time)
array[array.index(day)][date][empid]['checkout'].append('none')
else:
if (len(day[date][empid]['checkout']) > 0) and day[date][empid]['checkout'][-1] != 'none':
# checkout withou a checkin is passed
continue
else:
array[array.index(day)][date][empid]['checkin'].append('none')
array[array.index(day)][date][empid]['checkout'].append(time)
else:
if pun_stat == CHECK_IN:
array[array.index(day)][date][empid] = { 'checkin' : [ time ], 'checkout' : [ 'none' ] }
else:
continue
else:
if pun_stat == CHECK_IN:
array[array.index(day)][date] = { empid : { 'checkin' : [ time ], 'checkout' : [ 'none' ] } }
else:
continue
ids = []
for dict in array:
dates = dict.keys()
for dict in array:
for date in dates:
ids.append(list(set(dict[date].keys())))
print (len(dates))
print (len(ids))
dates = tuple(dates)
ids = tuple(ids)
for_csv = []
for dict in array:
for date, ii in zip(dates, ids):
for i in ii:
to_a = {
'employeeID' : i,
'date' : date,
'in1' : '',
'out1' : '',
'in2' : '',
'out2' : '',
'in3' : '',
'out3' : '',
'in4' : '',
'out4' : '',
'in5' : '',
'out5' : '',
'hoursWorked_str' : '',
'Raw_hours' : 0
}
if len(tuple(dict[date][i]['checkin'])) != len(tuple(dict[date][i]['checkout'])):
continue
else:
in_time = dict[date][i]['checkin']
out_time = dict[date][i]['checkout']
length = len(dict[date][i]['checkin'])
if length>0 and len(in_time) > 0:
runningSum = 0
for l, i, k in zip(range(length), range(0, length, 2), range(1, length, 2)):
to_a['in' + str((l+1))] = in_time[i]
to_a['out' + str(l+1)] = out_time[k]
runningSum += helpers.to_decimal(out_time[k]) - helpers.to_decimal(in_time[i])
to_a['Raw_hours'] = runningSum
to_a['hoursWorked_str'] = helpers.to_ftime(runningSum)
# Writing to array
for_csv.append(to_a)
print(len(for_csv))
print(for_csv)
row_names = ["employeeID", "date", "in1", "out1", "in2", "out2",
"in3", "out3", "in4", "out4", "in5", "out5", 'hoursWorked_str',
'Raw_hours']
helpers.write_to_csv(for_csv, 'may', row_names)