记住密码

转自:http://www.cnblogs.com/way_testlife/archive/2012/01/07/2298755.html

刚开始学python,看到这篇文章,果断学习了下,收获良多,感谢这位兄弟。

自己模仿着写了份-_-:

  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 ' key save '
  4 __author__ = ''
  5 try:
  6     import json
  7 except ImportError:
  8     import simplejson as json
  9 __all__ = ['fShow', 'fAddAccount', 'fDelAccount', 
 10             'fSearch', 'fModify']
 11 def fShow():
 12     if dSitePassport:
 13         for k, v in dSitePassport.items():
 14             for kV, vV in v.items():
 15                 print k, kV, vV
 16     else:
 17         print "empty..."
 18 def fAddAccount():
 19     site = raw_input('Input the site:')
 20     uid = raw_input('Input your account id:')
 21     passwd = raw_input('Input you password:')
 22     if site in dSitePassport:
 23         dValue = dSitePassport[site]
 24         if uid in dValue:
 25             print 'This account id is already exist.'
 26             return 
 27         else:
 28             dValue[uid] = passwd
 29     else:
 30         dTemp = {}
 31         dTemp[uid] = passwd
 32         dSitePassport[site] = dTemp
 33         dTemp = {}
 34 def fDelAccount():
 35     while True:
 36         site = raw_input('Input the site you want to delete:')
 37         if site not in dSitePassport:
 38             print 'Cannot find: %s' % site
 39             con = raw_input('Do you want to continue? y(es) or n(o)?')
 40             if con.upper() not in ('Y', 'YES'):
 41                 return 
 42         else:
 43             break
 44     dValue = dSitePassport[site]
 45     print 'the account of %s list as follows:' % site
 46     for k, v in dValue.items():
 47         print k, v
 48     if len(dValue) == 1:
 49         del dSitePassport[site]
 50     else:
 51         uid = raw_input('Input the account id you want to delete:')
 52         if uid not in dValue:
 53             print 'Input error!'
 54             return 
 55         else:
 56             dValue.pop(uid)
 57     print 'delete successful'
 58 def fSearch():
 59     site = raw_input('Input the site you want to find:')
 60     if site not in dSitePassport:
 61         print 'Sorry, cannot find [%s].' % site
 62         return 
 63     dValue = dSitePassport[site]
 64     for k, v in dValue.items():
 65         print k, v
 66 def fModify():
 67     while True:
 68         site = raw_input('Input the site you want to modify:')
 69         if site not in dSitePassport:
 70             print 'Can not find %s.' % site
 71             con = raw_input('Do you want to continue? y(es) or n(o)?')
 72             if con.upper() not in ('Y', 'YES'):
 73                 return 
 74         else:
 75             break
 76     dValue = dSitePassport[site]
 77     for k, v in dValue.items():
 78         print k, v
 79     if len(dValue) != 1:
 80         OldId = raw_input('Input the account id you want to modify:')
 81         if OldId not in dValue:
 82             print 'Input error'
 83             return
 84         dValue.pop(OldId)
 85     NewId = raw_input('Input the new account id:')
 86     NewPasswd = raw_input('Input the new password:')
 87     dValue[NewId] = NewPasswd
 88             
 89 dMenu = {'s':fShow, 'a':fAddAccount,
 90          'd':fDelAccount, 'f':fSearch,
 91          'm':fModify}
 92 def fMenu():
 93     Menu = '''\n(s)how (a)dd (d)elete 
 94 (f)ind (m)odify (q)uit
 95 Enter choice:'''
 96     while True:
 97         try:
 98             MenuChoice = raw_input(Menu).strip()[0].lower()
 99         except (EOFError, KeyboardInterrupt, IndexError):
100             MenuChoice = 'q'
101         print 'you picked %s' % MenuChoice
102         if MenuChoice not in 'sadfmq':
103             print 'Invalid option. Try again.'
104             continue
105         if MenuChoice == 'q':
106             jsonFile = json.dumps(dSitePassport)
107             open('passport.db', 'w').write(jsonFile)
108             break
109         dMenu[MenuChoice]()
110 if __name__ == '__main__':
111     try:
112         dSitePassport = json.loads(open('passport.db').read())
113         dPassport = {}
114         for k, v in dSitePassport.items():
115             for kV, vV in v.items():
116                 dPassport[kV] = vV
117     except:
118         print 'There is no passport.db, ready to creatde.'
119         dSitePassport = {}
120         dPassport = {}
121     fMenu()

 

posted @ 2014-12-09 20:43  MDGSF  阅读(860)  评论(0编辑  收藏  举报