2012年5月27日 星期日

[Python Std Library] Data Persistence : anydbm — Generic access to DBM-style databases


來源自 這裡
Preface :
anydbm is a generic interface to variants of the DBM database — dbhash (requires bsddb), gdbm, or dbm. If none of these modules is installed, the slow-but-simple implementation in module dumbdbm will be used. The anydbm module has been renamed to dbm in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3

Supported APIs :
anydbm.open(filename[, flag[, mode]])
Open the database file filename and return a corresponding object.

If the database file already exists, the whichdb module is used to determine its type and the appropriate module is used; if it does not exist, the first module listed above that can be imported is used.

The optional flag argument must be one of these values (If not specified, the default value is 'r'.) :


The optional mode argument is the Unix mode of the file, used only when the database has to be created. It defaults to octal 0666 (and will be modified by the prevailing umask).

- exception anydbm.error
A tuple containing the exceptions that can be raised by each of the supported modules, with a unique exception also named anydbm.error as the first item — the latter is used when anydbm.error is raised.

Usage Example :
The object returned by open() supports most of the same functionality as dictionaries; keys and their corresponding values can be stored, retrieved, and deleted, and the has_key() and keys() methods are available. Keys and values must always be strings.

The following example records some hostnames and a corresponding title, and then prints out the contents of the database :
  1. import dbm  
  2.   
  3. # Open database, creating it if necessary.  
  4. db = dbm.open('cache''c')  
  5.   
  6. # Record some values  
  7. db['www.python.org'] = 'Python Website'  
  8. db['www.cnn.com'] = 'Cable News Network'  
  9.   
  10. # Loop through contents.  Other dictionary methods  
  11. # such as .keys(), .values() also work.  
  12. print("\t[Info] Show db :")  
  13. for k, v in db.items():  
  14.     print("\t[Info] Key={0} ; Value={1}...".format(k, v))  
  15.   
  16. # Storing a non-string key or value will raise an exception (most  
  17. # likely a TypeError).  
  18. #db['www.yahoo.com'] = 4 <--- Will have exception : TypeError: values must be bytes or strings  
  19.   
  20. # Close when done.  
  21. db.close()  
  22.   
  23. db2 = dbm.open('cache')  
  24.   
  25. print("\t[Info] Show db2 restored from file :")  
  26. for k, v in db2.items():  
  27.     print("\t[Info] Key={0} ; Value={1}...".format(k, v))  
Execution result :
This message was edited 6 times. Last update was at 28/05/2012 10:35:10

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...