本文整理汇总了Python中kyotocabinet.DB.commit方法的典型用法代码示例。如果您正苦于以下问题:Python DB.commit方法的具体用法?Python DB.commit怎么用?Python DB.commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kyotocabinet.DB
的用法示例。
在下文中一共展示了DB.commit方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DataStorage
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import commit [as 别名]
class DataStorage(object):
"""
Parent class for RowData and KeyValueData.
"""
def __init__(self, filename, headers=None):
self.filename = filename
self.ext = os.path.splitext(filename)[1]
self.headers = headers
if os.path.exists(self.filename):
self.init_read()
else:
self.init_write()
def init_write(self):
self.mode = "write"
if self.ext == ".csv":
self._data_file = open(self.filename, "wb")
self._writer = csv.writer(self._data_file)
if self.headers:
self._writer.writerow(self.headers)
elif self.ext == ".json":
self._storage = {}
elif self.ext == ".kch":
from kyotocabinet import DB
self._storage = DB()
if not self._storage.open(self.filename, DB.OWRITER | DB.OCREATE):
msg = "Error opening kyotocabinet db: %s" % (self._storage.error())
raise dexy.commands.UserFeedback(msg)
elif self.ext == ".sqlite3":
self.init_write_sqlite3()
else:
raise dexy.commands.UserFeedback("unsupported extension %s" % self.ext)
def init_read(self):
self.mode = "read"
if self.ext == ".csv":
self._file = open(self.filename, "rb")
elif self.ext == ".json":
with open(self.filename, "rb") as f:
self._storage = json.load(f)
elif self.ext == ".kch":
from kyotocabinet import DB
self._storage = DB()
self._storage.open(self.filename, DB.OREADER)
elif self.ext == ".sqlite3":
import sqlite3
self._storage = sqlite3.connect(self.filename)
self._cursor = self._storage.cursor()
else:
raise dexy.commands.UserFeedback("unsupported extension %s" % self.ext)
def save(self):
if self.ext == ".csv":
self._data_file.close()
elif self.ext == ".json":
with open(self.filename, "wb") as f:
import json
json.dump(self._storage, f)
elif self.ext == ".kch":
if not self._storage.close():
raise dexy.commands.UserFeedback(self._storage.error())
elif self.ext == ".sqlite3":
self._storage.commit()
self._cursor.close()
else:
raise dexy.commands.UserFeedback("unsupported extension %s" % self.ext)