[python]實現Simple Database

閒來本身寫了一個小的簡單數據庫(Simple Database),要求最好用python寫。由於好久沒寫python了,語法都忘了不少,寫的過程當中溫故知新。python

首先這個數據庫實現了以下功能:數據庫

  數據命令:vim

  • SET name value – Set the variable name to the value value. Neither variable names nor values will contain spaces.
  • GET name – Print out the value of the variable name, or NULL if that variable is not set.
  • UNSET name – Unset the variable name, making it just like that variable was never set.
  • NUMEQUALTO value – Print out the number of variables that are currently set tovalue. If no variables equal that value, print 0.
  • END – Exit the program. Your program will always receive this as its last command.

     事務命令:數據結構

  • BEGIN – Open a new transaction block. Transaction blocks can be nested; a BEGINcan be issued inside of an existing block.
  • ROLLBACK – Undo all of the commands issued in the most recent transaction block, and close the block. Print nothing if successful, or print NO TRANSACTION if no transaction is in progress.
  • COMMIT – Close all open transaction blocks, permanently applying the changes made in them. Print nothing if successful, or print NO TRANSACTION if no transaction is in progress.

個人程序的時間複雜度
  SET O(1)
  GET O(1)
  UNSET O(1)
  NUMEQUALTO O(1)
  BEGIN O(1)
  ROLLBACK O(k) where k is the number of set and unset command within a transaction.
  COMMIT O(1)
app

#!/usr/bin/python


class SimpleDatabase:
    keysDict = {} numsDict = {} blocksNum = 0 isBegin = False def setFun(self, name, value): if name in self.keysDict and len(self.keysDict[name]) > 0: preValue = self.keysDict[name][-1] self.numsDict[preValue].remove(name) if value not in self.numsDict: self.numsDict.setdefault(value, []).append(name) else: self.numsDict[value].append(name) if self.isBegin == True: self.keysDict.setdefault(name, []).append(value) else: if name not in self.keysDict: self.keysDict.setdefault(name, []).append(value) else: self.keysDict[name][-1] = value def getFun (self, name): if name not in self.keysDict or len(self.keysDict[name]) == 0 or self.keysDict[name][-1] == None: print "NULL" else: print self.keysDict[name][-1] def unsetFun(self, name): setFun(name, None) def numEqualToFun(self, value): if value in self.numsDict: print len(self.numsDict[value]) else: print 0 def rollbackFun(self): if self.blocksNum == 0: print "NO TRANSACTION" else: self.blocksNum -= 1 self.isBegin = False for item in self.keysDict: if self.keysDict[item][-1] != None: self.numsDict[self.keysDict[item][-1]].remove(item) self.keysDict[item].pop() if len(self.keysDict[item]) > 0: self.numsDict[self.keysDict[item][-1]].append(item) def commitFun (self): if self.blocksNum == 0: print "NO TRANSACTION" else: self.blocksNum = 0 self.isBegin = False def beginFun (self): self.blocksNum += 1 self.isBegin = True def run(self): while True: raw_command = raw_input() if raw_command == "END": break commands = raw_command.split() if commands[0] == "SET": assert (len(commands) >= 3) self.setFun (commands[1], commands[2]) elif commands[0] == "GET": assert (len(commands) >= 2) self.getFun (commands[1]) elif commands[0] == "UNSET": assert (len(commands) >= 2) self.unsetFun (commands[1]) elif commands[0] == "NUMEQUALTO": assert (len(commands) >= 2) self.numEqualToFun (commands[1]) elif commands[0] == "BEGIN": self.beginFun() elif commands[0] == "COMMIT": self.commitFun() elif commands[0] == "ROLLBACK": self.rollbackFun() sd = SimpleDatabase() sd.run()

先說個人思路:ide

1.先介紹這裏的事務命令(begin,rollback和commit),在這裏採用棧的方式實現,即進入一個新的事務就將其中的操做加到棧中,若是仍是在同一個事務中就覆蓋以前的結果函數

2. 兩個dict分別存儲key的values(列表)和num的values(列表),values爲列表相似於棧的功能,由於python中沒有stack這個數據結構.學習

3. blocksNum控制blocks的數量,isBegin控制是否進入了一個block的操做.因此這兩個變量是控制邏輯的關鍵. this

總結學習心得:spa

1. python中global變量:dict能夠省去global,其它類型變量不能省。global A, B

2. python中的類:__init__是構造函數,不定義就是默認構造函數,實例方法的第一個參數self我理解爲C++的this; python類名以後能夠加括號

3. python的split默認以空格換行;

4. 儘可能用in不用has_key

5. 區分len() == 0, () == None, not in等

6. vim中的添加註釋和取消註釋命令!

7. raw_input和input的區別

8. list的remove, [-1], append, extend等使用

9.dict的value爲list的狀況:dict = {}; dict.setdefault(name, []).append(1)

相關文章
相關標籤/搜索