-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattendance.py
196 lines (167 loc) · 7.22 KB
/
attendance.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import pandas as pd
from datetime import datetime, timedelta
bgs_config = pd.read_excel('config.xlsx', 'Sheet3', header=4, engine='openpyxl')
cx_config = pd.read_excel('config.xlsx', 'Sheet2', header=4, engine='openpyxl')
df = pd.read_excel('file.xlsx', engine='openpyxl')
def cat(lis_s, cats, how="()"):
# 列表中的字符串转换为时间类型
lis = []
for ts in lis_s:
lis.append(datetime.strptime(ts, '%H:%M'))
# 列表中的字符串转换为时间类型
cats_list = []
for ts in cats:
cats_list.append(datetime.strptime(ts, '%H:%M'))
cat_result = [[] for _ in range(len(cats_list) - 1)]
for t in lis:
for i in range(len(cats_list)):
if how == "(]":
if cats_list[i] < t <= cats_list[i + 1]:
cat_result[i].append(t)
elif how == '[)':
if cats_list[i] <= t < cats_list[i + 1]:
cat_result[i].append(t)
elif how == '()':
if cats_list[i] < t < cats_list[i + 1]:
cat_result[i].append(t)
elif how == '[]':
if cats_list[i] <= t <= cats_list[i + 1]:
cat_result[i].append(t)
else:
raise ValueError('how 参数不正确')
return cat_result
def bgs(s):
a, b, c, d, e, f = cat(s.split(' '), '0:00 8:51 12:20 13:41 18:00 18:31 23:59'.split(' '), how='[)')
al, bl, cl, dl, el, fl = [len(i) for i in [a, b, c, d, e, f]]
ab, bb, cb, db, eb, fb = [0 if len(i) == 0 else 1 for i in [a, b, c, d, e, f]]
v = bgs_config[
(bgs_config['a'] == ab) & (bgs_config['b'] == bb) & (bgs_config['c'] == cb) & (bgs_config['d'] == db) & (
bgs_config['e'] == eb) & (bgs_config['f'] == fb)]
fenlei = v['分类'].iloc[0]
work = bgs_work(a, b, c, d, e, f, v['平时上班'].iloc[0])
overtime = bgs_overtime(a, b, c, d, e, f, v['平时加班'].iloc[0])
late = bgs_late(a, b, c, d, e, f, v['迟到'].iloc[0])
return fenlei, work, overtime, late
def t(v):
return datetime.strptime(v, "%H:%M")
def bgs_work(a, b, c, d, e, f, value):
if value == 0:
return timedelta(0).seconds
elif value == '(12:20-8:50)+(d1-13:40)':
return ((t('12:20') - t('8:50')) + (d[-1] - t('13:40'))).seconds
elif value == '(12:20-b0)+(d1-13:40)':
return ((t('12:20') - b[0]) + (d[-1] - t('13:40'))).seconds
elif value == '(18:00-13:40)+(12:20-8:50)':
return ((t('18:00') - t('13:40')) + (t('12:20') - t('8:50'))).seconds
elif value == '12:20-8:50':
return (t('12:20') - t('8:50')).seconds
elif value == '12:20-b0':
return (t('12:20') - b[0]).seconds
elif value == '18:00-13:40':
return (t('18:00') - t('13:40')).seconds
elif value == '18:00-d0':
return (t('18:00') - d[0]).seconds
elif value == 'b1-8:50':
return (b[-1] - t('8:50')).seconds
elif value == 'b1-b0':
return (b[-1] - b[0]).seconds
elif value == 'd1-13:40':
return (d[-1] - t('13:40')).seconds
elif value == 'd1-d0':
return (d[-1] - d[0]).seconds
elif value == '(12:20-b0)+(18:00-13:40)':
return ((t('12:20') - b[0]) + (t('18:00') - t('13:40'))).seconds
def bgs_overtime(a, b, c, d, e, f, value):
if value == 0:
return 0
elif value == 'f1-18:30':
return (f[-1] - t('18:30')).seconds
elif value == 'f1-f0':
return (f[-1] - f[0]).seconds
def bgs_late(a, b, c, d, e, f, value):
print(value)
if value == 0:
return 0
elif value == '(12:20-8:50)+(d0-13:40)':
return ((t('12:20') - t('8:50')) + (d[0] - t('13:40'))).seconds
elif value == '(18:00-13:40)+(12:20-8:50)':
return ((t('18:00') - t('13:40')) + (t('12:20') - t('8:50'))).seconds
elif value == '12:20-8:50':
return (t('12:20') - t('8:50')).seconds
elif value == 'b0-8:50':
return (b[0] - t('8:50')).seconds
def cx(s):
a, b, c, d, e = cat(s.split(' '), '0:00 8:31 12:30 13:31 18:00 23:59'.split(' '), how='[)')
al, bl, cl, dl, el = [len(i) for i in [a, b, c, d, e]]
ab, bb, cb, db, eb = [0 if len(i) == 0 else 1 for i in [a, b, c, d, e]]
v = cx_config[(cx_config['a'] == ab) & (cx_config['b'] == bb) & (cx_config['c'] == cb) & (cx_config['d'] == db) & (
cx_config['e'] == eb)]
fenlei = v['分类'].iloc[0]
work = cx_work(a, b, c, d, e, v['平时上班'].iloc[0])
overtime = cx_overtime(a, b, c, d, e, v['平时加班'].iloc[0])
late = cx_late(a, b, c, d, e, v['迟到'].iloc[0])
return fenlei, work, overtime, late
def cx_work(a, b, c, d, e, value):
if value == 0:
return timedelta(0).seconds
elif value == '(12:30-8:30)+(18:00-13:00)':
return ((t('12:30') - t('8:30')) + (t('18:00') - t('13:00'))).seconds
elif value == '(12:30-8:30)+(d1-13:30)':
return ((t('12:30') - t('8:30')) + (d[-1] - t('13:30'))).seconds
elif value == '(18:00-13:40)+(12:20-8:50)':
return ((t('18:00') - t('13:40')) + (t('12:20') - t('8:50'))).seconds
elif value == '(12:30-b0)+(d1-13:30)':
return ((t('12:30') - b[0]) + (d[-1] - t('13:30'))).seconds
elif value == '(12:30-b1)+(18:00-13:30)':
return ((t('12:30') - b[-1]) + (t('18:00') - t('13:30'))).seconds
elif value == '12:30-8:30':
return (t('12:30') - t('8:30')).seconds
elif value == '12:30-b0':
return (t('12:30') - b[0]).seconds
elif value == '18:00-13:30':
return (t('18:00') - t('13:30')).seconds
elif value == '18:00-d0':
return (t('18:00') - d[0]).seconds
elif value == 'b1-8:30':
return (b[-1] - t('8:30')).seconds
elif value == 'b1-b0':
return (b[-1] - b[0]).seconds
elif value == 'd1-13:30':
return (d[-1] - t('13:30')).seconds
def cx_overtime(a, b, c, d, e, value):
if value == 0:
return 0
elif value == 'e1-18:00':
return (e[-1] - t('18:00')).seconds
else:
raise ValueError('加班计算异常')
def cx_late(a, b, c, d, e, value):
print(a, b, c, d, e)
print(value)
if value == 0:
return 0
elif value == '(12:30-8:30)+(d0-13:30)':
return ((t('12:30') - t('8:30')) + (d[0] - t('13:30'))).seconds
elif value == '12:30-8:30':
return (t('12:30') - t('8:30')).seconds
elif value == 'b0-8:30':
return (b[0] - t('8:30')).seconds
else:
raise ValueError('迟到计算异常')
def main():
df['len'] = df.dropna(subset=['时间'])['时间'].str.split(' ').apply(len)
df2 = df[df['len'] >= 1]
bgs_df = df2[df2['len'] < 4]
cx_df = df2[df2['len'] >= 4]
bgs_df['状态'], bgs_df['平时上班'], bgs_df['平时加班'], bgs_df['迟到'] = zip(*bgs_df['时间'].apply(bgs))
cx_df['状态'], cx_df['平时上班'], cx_df['平时加班'], cx_df['迟到'] = zip(*cx_df['时间'].apply(cx))
bgs_df['平时上班'] = bgs_df['平时上班']/3600
bgs_df['平时加班'] = bgs_df['平时加班'] / 3600
bgs_df['迟到'] = bgs_df['迟到'] / 60
cx_df['平时上班'] = cx_df['平时上班'] / 3600
cx_df['平时加班'] = cx_df['平时加班'] / 3600
cx_df['迟到'] = cx_df['迟到'] / 60
bgs_df.to_excel('办公室人员统计.xlsx', 'w+')
cx_df.to_excel('产线人员统计.xlsx', 'w+')
if __name__ == '__main__':
main()