您的位置:首页 > 编程语言 > Python开发

《Python核心编程》第二版第405页第十三章练习 续二 -Python核心编程答案-自己做的-

2012-09-13 07:28 851 查看
这是自己做的练习,可能有错误,欢迎讨论和各种优化重构方案。
根据读者反馈,或者code review,我对本篇文章答案或者相关内容的更新补充,一般会被添加在本篇博客的评论中。
我尽量保证每题的答案代码是完整的,不仅仅是函数或者类,打开Python 2.7的IDLE,将代码完整拷贝进去,就能调试运行。
欢迎访问Balian在博客园的家http://www.cnblogs.com/balian

13-4.
用户注册。建立一个用户数据库(包括登录名、密码和上次登录时间戳)类(参考练习7-5和练习9-12),来管理一个系统。该系统要求用户在登录后才能访问某些资源。这个数据库类对用户进行管理,并在实例化操作时加载之前保存的用户信息,提供访问函数来添加或更新数据库的信息。在数据修改后,数据库会在垃圾回收时将新信息保存到磁盘(参见__del__())。
【注】
我使用D盘EX13_4文件夹里的accounts.txt文件记录用户名和密码。只有一个用户Balian,密码是apple。用同位置的文件userlog.txt记录上次登录时间戳。如果用户Balian登陆正确,就返回上次登陆时间,和登陆成功信息。如果用户名或者密码错误,都将提示登陆失败,失败3次退出程序。对用户进行管理指如果登陆成功,可更新密码。这样做的目的是想使代码尽量简单,毕竟这只是一个练习题。
accounts.txt文件内容(仅一行)
balian 123456
userlog.txt为一个空文本文件
运行程序前,请提前准备以上的文件夹和文件。

【答案】
代码如下:

#-*- encoding: utf-8 -*-

class DBMnagement(object):
'数据库管理类'

def __init__(self, nm, pwd):
self.name = nm
self.password = pwd

def updatePWD(self, newpwd):
self.password = newpwd

def __del__(self):
pass

#import os
import time

def CreateLog(username):
'登陆成功写时间戳到log文件'
timeStamp = time.ctime(time.time())
log = 'User' + ' - ' + username + ', last time login Succeed at ' + timeStamp + '\n'
logFile = open('d:\\EX13_4\\userlog.txt', 'a')
logFile.write(log)
logFile.close()

def ReadLog():
'登陆成功,从log文件读取上次时间戳'
logFile = open('d:\\EX13_4\\userlog.txt')
logInfo = logFile.readlines()
if len(logInfo) >= 1:
print logInfo[-1]
logFile.close()

def ChangePWDinLog(username, newpwd):
'修改用户密码'
credentialFile = open('d:\\EX13_4\\accounts.txt', 'w')
credentialtxt = username + ' ' + newpwd
credentialFile.write(credentialtxt)
credentialFile.close()
print '密码已修改 ... Password updated successfully. \n'

# 从这里开始是主程序
print '欢迎使用最简单的用户管理系统'
print '尝试用系统中已经存在的用户名和密码登录,有三次机会'
print '登陆成功后输入 updatepwd 命令可以修改密码,成功后退出系统'
print '登陆成功后输入 quit 命令退出系统\n'

credentialFile = open('d:\\EX13_4\\accounts.txt') # 从文本文件中获取用户名和密码
credentialInfo = credentialFile.readlines()
credentialFile.close()

usernameinDB = credentialInfo[-1].split()[0] # 这里credentialInfo[-1]是一个字符串,包括用户名和口令,空格隔开的
passwordindm = credentialInfo[-1].split()[1]

errorTimes = 3
while errorTimes:
print '请输入用户名和密码:'
input_username = raw_input('Username ... :  ')
input_password = raw_input('Password ... :  ')
if input_username == usernameinDB and input_password == passwordindm:
print '登陆成功  ... Login Successful'
CurrentUser = DBMnagement(input_username, input_password)
ReadLog()
CreateLog(input_username)
input_command = raw_input('请输入你的命令, quit or updatepwd ... \n')
if input_command == 'quit':
del CurrentUser
print '\n已退出系统  ... Logout successfully.'
break
elif input_command == 'updatepwd':
newpwd = raw_input('\n请输入新密码, new password ... :  ')
CurrentUser.updatePWD(newpwd)
ChangePWDinLog(CurrentUser.name, CurrentUser.password)
del CurrentUser
print '\n已退出系统  ... Logout successfully.'
break
else:
print '错误命令  ... Wrong Command'
del CurrentUser
print '\n已退出系统  ... Logout successfully.'
break
else:
print '用户名或口令错误  ... Wrong Username or Password.\n'
errorTimes -= 1
if errorTimes == 0: print '已退出系统  ... Logout successfully.'


【执行结果】

欢迎使用最简单的用户管理系统
尝试用系统中已经存在的用户名和密码登录,有三次机会
登陆成功后输入 updatepwd 命令可以修改密码,成功后退出系统
登陆成功后输入 quit 命令退出系统

请输入用户名和密码:
Username ... :  balian
Password ... :  123
用户名或口令错误  ... Wrong Username or Password.

请输入用户名和密码:
Username ... :  balian
Password ... :  123456
用户名或口令错误  ... Wrong Username or Password.

请输入用户名和密码:
Username ... :  balian
Password ... :  apple
登陆成功  ... Login Successful
User - balian, last time login Succeed at Wed Sep 12 12:32:35 2012

请输入你的命令, quit or updatepwd ...
updatepwd

请输入新密码, new password ... :  egg
密码已修改 ... Password updated successfully.

已退出系统  ... Logout successfully.

【参考】

http://blog.csdn.net/lgfei/article/details/92044
Python中写文本文件的方法

http://magustest.com/blog/softwaretesting/how-to-write-a-file-by-using-python/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐