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
- 下一篇: 视频86免费影院-视频电影网聚平台
