为了账号安全,请及时绑定邮箱和手机立即绑定

Python:创建字典

Python:创建字典

斯蒂芬大帝 2021-03-28 12:19:17
#---------------------------------------------------------#  Print days diff by Converting Unix timestamp to Readable Date/time#---------------------------------------------------------def convUnixTime(t):        return 1+(datetime.datetime.fromtimestamp(t*60*60*24)              - datetime.datetime.today()).days#---------------------------------------------------------# Read shadow file and check for account expires and create dictionary #---------------------------------------------------------with open( "/etc/shadow" ) as shadow:        for aLine in shadow:                filed = aLine.split(":")                f = filed[7]                try:                    f = int(f)                    f=convUnixTime(f)                except ValueError:                    f = "NULL"                if f != "NULL" and f <= 0:                        total_expired_users += 1                        expr_list[ filed[0] ] = f                elif f != "NULL" and f <= min_days:                        total_expring_users += 1                        expr_list[ filed[0] ] = f我已经创建了帐户已过期的用户词典,但是我认为这样做更加简洁明了。
查看完整描述

3 回答

?
弑天下

TA贡献1818条经验 获得超8个赞

使用try-except子句可能看起来更干净:


try:

    f = int(f)

    f=convUnixTime(f)

except ValueError:

    pass

else:

    if f <= 0:

      total_expired_users += 1

      expr_list[ filed[0] ] = f

    elif f <= min_days:

      total_expring_users += 1

      expr_list[ filed[0] ] = f

您也可以稍微更改顺序,以避免expr_list[filed[0]]重复:


if f <= min_days:

    expr_list[filed[0]] = f


    if f <= 0:

        total_expired_users += 1

    else:

        total_expiring_users += 1


查看完整回答
反对 回复 2021-04-01
  • 3 回答
  • 0 关注
  • 232 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号