Python实现undo操作
生活随笔
收集整理的这篇文章主要介绍了
Python实现undo操作
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
Python实现UNDO日志操作
import re# 磁盘 Disk = {'A': 8,'B': 8, }# 内存 Memory = {}# 缓冲区 T = 0# undo日志 log = []# 从磁盘读取到内存缓冲区 def INPUT(read_data):Memory[read_data] = Disk[read_data]# 从内存缓冲区读取到临时变量T def READ(read_data, t):global TT = Memory[read_data]# 把临时变量T的值读入到内存缓冲区 def WRITE(write_data, t):# 再写入的时候 就把recover写入日志中log.append('<T,{},{}>'.format(write_data, Disk[write_data]))log.append('recover(\'{}\', {})'.format(write_data, Disk[write_data]))Memory[write_data] = T# 把内存缓冲区的内容放入磁盘中 def OUTPUT(write_data):Disk[write_data] = Memory[write_data]# undo日志恢复函数 def recover(key, value):Disk[key] = value# 打印当前动作 内存 磁盘 和 log def print_now_status(ac):print('动作' + ' ' * 11 + 'T' + ' ' * 5 + '内存' + ' ' * 26 + '磁盘' + ' ' * 26 + 'LOG')print('{: <15s}{: <6d}{: <30s}{: <30s}{}'.format(ac, T, str(Memory)[1:-1], str(Disk)[1:-1], log))# 打印当前磁盘情况 def print_now_disk_status():print('磁盘数据 : ', str(Disk)[1:-1])# 打印当前日志内容 def print_now_log_status():print('日志数据 : ', log)# undo操作 def undo():print('■ 开始回退')while (len(log)):undo_cmd = log.pop()print('■ 执行指令 {}'.format(undo_cmd))if undo_cmd == '<START T>' or undo_cmd == '<COMMIT T>' or undo_cmd == '<T,A,8>' or undo_cmd == '<T,B,8>':breakexec(undo_cmd)para_list = re.search('\(.*?\)', undo_cmd).group().split()print('■ 回退变量 {} 到值 {}'.format(para_list[0], para_list[1]))print('■ 回退结束, 当前磁盘情况:')print_now_disk_status()print(' 当前日志情况:')print_now_log_status()# 定义事务类 class Transaction:# 初始化def __init__(self, df, cmd):self.define = dfself.command = cmd# 打印当前命令的指令def print_cmd(self):print('\n■ 事务 {} ■\n指令'.format(self.define))print('\n'.join(self.command) + '\n')def pause(self):print('执行哪个事务?')print('执行一步 : 按n | 执行完事务:按c | 从此步开始回滚 : 按u')c = input()print(c)while (c != 'n' and c != 'c' and c != 'u'):print('输入错误')c = input()print(c)return cdef excute(self):self.print_cmd()log.append('<START T>')can_pause = Truefor cmd in self.command:print('■ 执行动作 {}'.format(cmd))exc_cmd = cmdif '*' in cmd:exc_cmd = 'global T\n' + cmdexec(exc_cmd) # INPUT(\'A\') - > INPUT(A) | 'global T \n INPUT(A)'print_now_status(cmd)if can_pause:c = self.pause()if c == 'c':can_pause = Falseelif c == 'u':undo()returnlog.append('<COMMIT T>')print_now_log_status()def listFunction():print('■ 执行哪个事务?')print('■ 执行事务1 : 按1 ■ 查看事务1 : 按2 ■ 退出0 : 按0')c = input()while (c != '1' and c != '2' and c != '0'):print('输入错误')c = input()return cif __name__ == "__main__":# 定义事务df1 = 'A*=2, B*=2'command1 = ['INPUT(\'A\')', 'READ(\'A\', T)', 'T=T*2', 'WRITE(\'A\',T)', 'INPUT(\'B\')', 'READ(\'B\', T)', 'T=T*2','WRITE(\'B\',T)', 'OUTPUT(\'A\')', 'OUTPUT(\'B\')']Transaction1 = Transaction(df1, command1)print('初始磁盘情况')print_now_disk_status()c = listFunction()while (True):# 按输入指令执行c = int(c)if c == 1:Transaction1.excute()elif c == 2:Transaction1.print_cmd()elif c == 0:exit()if c == '1':print('事务执行完成')print('现在磁盘情况')print_now_disk_status()print('现在日志情况')print_now_log_status()c = listFunction()总结
以上是生活随笔为你收集整理的Python实现undo操作的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: replica文件服务器,mongodb
- 下一篇: python基本数_python--基本