- 论坛徽章:
- 0
|
不用说啥了,注释很清楚了~
[color="#008080"]# encoding:gb2312
[color="#008080"]#
[color="#008080"]# 本模块利用timeItv模块提供的转换器进行加班时间统计
[color="#008080"]# 输入文件分行列出每天的签到和签退时间
[color="#008080"]# 读入后计算每个有效行的加班时间
[color="#008080"]# 并在最后对超过最低时限的加班时间做出统计
[color="#008080"]# 结果输出到另一文件中
[color="#008080"]# 其中两种方式确定输入文件名:bModeInput来控制
[color="#0000ff"]import [color="#000000"]timeItv
[color="#0000ff"]import [color="#800000"]os[color="#000000"],[color="#800000"]sys[color="#000000"],[color="#800000"]re
[color="#0000ff"]def [color="#000000"]file2list (_uf):
[color="#808000"]f [color="#008080"]= [color="#808000"]file[color="#000000"](_uf,[color="#ff00ff"]'r'[color="#000000"])
[color="#000000"]l [color="#008080"]= [color="#808000"]f[color="#008080"].[color="#808000"]readlines[color="#000000"]()
[color="#0000ff"]return [color="#000000"]l
[color="#0000ff"]def [color="#000000"]list2file (_uf,_l):
[color="#808000"]f [color="#008080"]= [color="#808000"]file[color="#000000"](_uf,[color="#ff00ff"]'w'[color="#000000"])
[color="#808000"]f[color="#008080"].[color="#808000"]writelines[color="#000000"](_l)
[color="#808000"]f[color="#008080"].[color="#808000"]close[color="#000000"]()
[color="#0000ff"]def [color="#000000"]exitByMsg (_sMsg):
[color="#0000ff"]print [color="#000000"]_sMsg
[color="#800000"]sys[color="#008080"].[color="#808000"]exit[color="#000000"]()
[color="#0000ff"]def [color="#000000"]getTimeAddWork (_sb,_se):
[color="#000000"]_iTimeAdd [color="#008080"]= [color="#000000"]timeItv[color="#008080"].[color="#000000"]time2itv(_se) [color="#008080"]- [color="#000000"]timeItv[color="#008080"].[color="#000000"]time2itv(_sb) [color="#008080"]- [color="#000000"]iTimeNormal
[color="#0000ff"]return [color="#000000"]timeItv[color="#008080"].[color="#000000"]itv2time(_iTimeAdd ),_iTimeAdd
[color="#0000ff"]def [color="#000000"]processFile (_uf):
[color="#000000"]_lOutput [color="#008080"]= [color="#000000"][]
[color="#000000"]_iTimeSumAdd [color="#008080"]= [color="#000000"]0
[color="#000000"]_lInput [color="#008080"]= [color="#000000"]file2list(_uf)
[color="#000000"]_pc [color="#008080"]= [color="#800000"]re[color="#008080"].[color="#808000"]compile[color="#000000"]( r[color="#ff00ff"]'^\s*(?P\S+)\s*(?P\S+)\s*$' [color="#000000"])
[color="#0000ff"]for [color="#000000"]_l [color="#0000ff"]in [color="#000000"]_lInput:
[color="#000000"]_m [color="#008080"]= [color="#000000"]_pc[color="#008080"].[color="#808000"]match[color="#000000"](_l)
[color="#0000ff"]if [color="#000000"]_m:
[color="#0000ff"]print [color="#ff00ff"]'m'
[color="#000000"]_sTimeAddWork,_iTimeAddWork [color="#008080"]= [color="#000000"]getTimeAddWork([color="#008080"]*[color="#000000"]_m[color="#008080"].[color="#808000"]group[color="#000000"]([color="#ff00ff"]'sTimeBegin'[color="#000000"],[color="#ff00ff"]'sTimeEnd'[color="#000000"]) )
[color="#0000ff"]if [color="#000000"]_sTimeAddWork [color="#008080"]> [color="#000000"]sTimeLimitBelow:
[color="#000000"]_iTimeSumAdd [color="#008080"]+= [color="#000000"]_iTimeAddWork
[color="#000000"]_lOutput[color="#008080"].[color="#808000"]append[color="#000000"]( _sTimeAddWork [color="#008080"]+[color="#ff00ff"]'\n' [color="#000000"])
[color="#0000ff"]else[color="#000000"]:
[color="#0000ff"]print [color="#ff00ff"]'n'
[color="#000000"]_lOutput[color="#008080"].[color="#808000"]append[color="#000000"]( [color="#ff00ff"]'\n' [color="#000000"])
[color="#000000"]_lOutput[color="#008080"].[color="#808000"]append[color="#000000"]( [color="#ff00ff"]'超过' [color="#008080"]+ [color="#000000"]sTimeLimitBelow [color="#008080"]+ [color="#ff00ff"]'的统计结果:' [color="#008080"]+ [color="#000000"]timeItv[color="#008080"].[color="#000000"]itv2time(_iTimeSumAdd))
[color="#0000ff"]return [color="#000000"]_lOutput
[color="#0000ff"]def [color="#000000"]getFilePath ():
[color="#000000"]_uf [color="#008080"]= [color="#000000"]uCoFinancialBase [color="#008080"]+ [color="#808000"]raw_input[color="#000000"]([color="#ff00ff"]'输入加班计算时间文件名\n'[color="#008080"]+[color="#000000"]uCoFinancialBase[color="#008080"]+[color="#ff00ff"]'>'[color="#000000"])[color="#008080"].[color="#808000"]strip[color="#000000"]()
[color="#0000ff"]if [color="#800000"]os[color="#008080"].[color="#000000"]path[color="#008080"].[color="#808000"]isfile[color="#000000"](_uf):
[color="#0000ff"]return [color="#000000"]_uf
[color="#0000ff"]else [color="#000000"]:
[color="#000000"]exitByMsg([color="#ff00ff"]'file not found: '[color="#008080"]+[color="#000000"]_uf)
[color="#0000ff"]def [color="#000000"]getOutFilePath (_ufIn):
[color="#000000"]_iIdxDot [color="#008080"]= [color="#000000"]_ufIn[color="#008080"].[color="#808000"]rfind[color="#000000"]([color="#ff00ff"]'.'[color="#000000"])
[color="#0000ff"]return [color="#000000"]_ufIn[:_iIdxDot] [color="#008080"]+ [color="#ff00ff"]'_deal' [color="#008080"]+ [color="#000000"]_ufIn[_iIdxDot:]
[color="#0000ff"]if [color="#008080"]__name__ == [color="#ff00ff"]'__main__'[color="#000000"]:
[color="#008080"]# 基础路径
[color="#000000"]uCoFinancialBase [color="#008080"]= [color="#000000"]r[color="#ff00ff"]'E:\Company Financial'[color="#008080"]+[color="#800000"]os[color="#008080"].[color="#000000"]sep
[color="#008080"]# **输入模式
[color="#000000"]bModeInput [color="#008080"]= [color="#ff00ff"]'auto'
[color="#0000ff"]if [color="#000000"]bModeInput [color="#008080"]== [color="#ff00ff"]'auto'[color="#000000"]:
[color="#000000"]ufInput [color="#008080"]= [color="#000000"]uCoFinancialBase [color="#008080"]+ [color="#000000"]r[color="#ff00ff"]'time_count_temp.txt'
[color="#0000ff"]elif [color="#000000"]bModeInput [color="#008080"]== [color="#ff00ff"]'interact' [color="#000000"]:
[color="#008080"]# 交互输入文件路径
[color="#000000"]ufInput [color="#008080"]= [color="#000000"]getFilePath()
[color="#008080"]# 正常工作时间
[color="#000000"]iTimeNormal [color="#008080"]= [color="#000000"]timeItv[color="#008080"].[color="#000000"]time2itv([color="#ff00ff"]'18:00:00'[color="#000000"]) [color="#008080"]- [color="#000000"]timeItv[color="#008080"].[color="#000000"]time2itv([color="#ff00ff"]'09:15:00'[color="#000000"])
[color="#008080"]# 统计限制时间下限
[color="#000000"]sTimeLimitBelow [color="#008080"]= [color="#ff00ff"]'00:59:59'
[color="#008080"]# 处理输入
[color="#000000"]lResult [color="#008080"]= [color="#000000"]processFile (ufInput)
[color="#008080"]# 输出文件路径
[color="#000000"]ufOutput [color="#008080"]= [color="#000000"]getOutFilePath(ufInput)
[color="#008080"]# 输出结果
[color="#000000"]list2file(ufOutput,lResult)
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/28253/showart_289142.html |
|