请你把这段代码添加进之前的考勤示例代码中重新编写
时间: 2024-02-17 20:04:06 浏览: 26
好的,以下是修改后的示例代码:
```python
import re
def is_commute_record(text):
time_pattern = re.compile(r'\d{1,2}:\d{2}')
commute_pattern = re.compile(r'(上班|下班)')
has_time = bool(time_pattern.search(text))
has_commute = bool(commute_pattern.search(text))
if has_time and has_commute:
times = time_pattern.findall(text)
for t in times:
hour, minute = map(int, t.split(':'))
if (hour == 8 and abs(minute - 30) <= 30) or (hour == 18 and abs(minute - 30) <= 30):
return True
return False
else:
return False
def attendance_check(records):
commute_count = 0
for record in records:
if is_commute_record(record):
commute_count += 1
if commute_count >= 2:
return True
else:
return False
# 测试代码
records = ['上班打卡时间:8:30', '下班打卡时间:18:45', '开会时间:14:00', '上班打卡时间:8:15']
if attendance_check(records):
print('考勤正常')
else:
print('考勤异常')
```
这个示例代码中,我们将之前的 `is_commute_record` 函数添加进了 `attendance_check` 函数中,用来判断一条记录是否为上下班记录。在 `attendance_check` 函数中,我们遍历所有的记录,统计上下班记录的数量。如果数量大于等于2,则判断为正常考勤,否则判断为异常考勤。