2
#---------------------------------------------------------
#  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

I have created dictionary of user which account has been expires, but I think this has been done in more cleaner and simple way..

Thanks in Advance!!

Rahul Patil
  • 1,014
  • 3
  • 14
  • 30

3 Answers3

1

This may look cleaner with the try-except clause:

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

You can also change the order a bit, to avoid the expr_list[filed[0]] duplication:

if f <= min_days:
    expr_list[filed[0]] = f

    if f <= 0:
        total_expired_users += 1
    else:
        total_expiring_users += 1
Community
  • 1
  • 1
bereal
  • 32,519
  • 6
  • 58
  • 104
1

You could use list comprehension to simplify your code:

import datetime

min_days = 20000

def conv_unix_time(t):
    return (1+(datetime.datetime.fromtimestamp(int(t)*60*60*24)
            - datetime.datetime.today()).days) if t else None

with open('foo.txt') as shadow:
    expire_durations = {line.split(':')[0]: conv_unix_time(line.split(':')[7]) for line in shadow}

print [user for user, time in expire_durations.iteritems() if time and time < 0]
print [user for user, time in expire_durations.iteritems() if time and 0 < time < min_days]
Thomas Fenzl
  • 4,342
  • 1
  • 17
  • 25
0

Thanks bereal,

With help of your correction , I have created final..

#---------------------------------------------------------
#  Convert Unix timestamp to Readable Date/time
#---------------------------------------------------------
def convUnixTime(t):
        return datetime.datetime.fromtimestamp(t*60*60*24)


#---------------------------------------------------------
# Read shadow file and check for account expriry
#---------------------------------------------------------
with open( "/etc/shadow" ) as shadow:
        for aLine in shadow:
                filed = aLine.split(":")
                f = filed[7]
                try:
                        f = int(f)
                        exprdate = convUnixTime(f)
                        f=1+( exprdate - datetime.datetime.today()).days
                        l=[f,exprdate]
                except ValueError:
                        pass
                else:
                        if f <= min_days:
                                expr_list[filed[0]]=l
                        if f <= 0:
                                total_expired_users += 1
                        else:
                                total_expring_users += 1

After Parsing Whole Script.. Output:

====>>Following 3 Account has been Expired
rahul    :: Expired on 2013-06-12 05:30:00
test     :: Expired on 2013-06-01 05:30:00
vinayak  :: Expired on 1970-01-01 05:30:00
====>>Following 4 Account will be Expire in 20 Days
suraj    :: 2  days || Last date :: 2013-06-15 05:30:00
ram      :: 1  days || Last date :: 2013-06-14 05:30:00
javed    :: 2  days || Last date :: 2013-06-15 05:30:00
shyam    :: 2  days || Last date :: 2013-06-15 05:30:00
Rahul Patil
  • 1,014
  • 3
  • 14
  • 30