本文整理汇总了Python中kyotocabinet.DB类的典型用法代码示例。如果您正苦于以下问题:Python DB类的具体用法?Python DB怎么用?Python DB使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DB类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_items
def get_items(item_filter, db_file, page=0):
item_iter = 0
items = []
db = DB()
if not db.open("{0}".format(db_file), DB.OREADER | DB.OCREATE):
print "Could not open database."
cur = db.cursor()
cur.jump_back()
while len(items) < FILTER_MAX:
rec = cur.get(False)
if not rec:
break
if item_iter != (FILTER_MAX * page):
if item_filter(rec):
item_iter = item_iter + 1
cur.step_back()
continue
if item_filter(rec):
items.append(rec)
cur.step_back()
cur.disable()
db.close()
sorted_items = sorted(items, key=get_key, reverse=True)
sorted_items_for_viewing = [loads(item[1]) for item in sorted_items]
for item in sorted_items_for_viewing:
if item['title'] is None or item['title'] == "":
item['title'] = item['url']
return sorted_items_for_viewing
示例2: aggregate_by_hour
def aggregate_by_hour(db_file):
# Initialize the dict with each hour
hours = {key: 0 for key in range(0,24)}
db = DB()
if not db.open("{0}".format(db_file), DB.OREADER | DB.OCREATE):
print "Could not open database."
cur = db.cursor()
cur.jump_back()
while True:
rec = cur.get(False)
if not rec:
break
loaded = loads(rec[1])
unix = float(loaded['created_at'])
time = datetime.fromtimestamp(unix)
hours[time.hour] = hours[time.hour] + 1
cur.step_back()
cur.disable()
db.close()
hours = [{'name': "{}:00".format(key), 'data': [hours[key]]} for key in hours]
return hours
示例3: get_post_num
def get_post_num(post_num, db_file):
item = None
db = DB()
if not db.open("{0}".format(db_file), DB.OREADER | DB.OCREATE):
print "Could not open database."
cur = db.cursor()
cur.jump()
i = 0
while True:
rec = cur.get(False)
if not rec:
break
if i == post_num:
item = rec
cur.step()
i = i + 1
cur.disable()
db.close()
if item is not None:
return loads(item[1])
return dict()
示例4: open
def open(self, filename):
db = DB()
# self.db=DB(DB.GEXCEPTIONAL)
if not db.open(filename, DB.OWRITER | DB.OCREATE | DB.ONOLOCK):
raise IOError("open error: '" + str(self.db.error()) + "' on file:"
+ filename)
return db
示例5: top_things
def top_things(db_file):
urls = {}
people = {}
graph = {}
db = DB()
if not db.open("{0}".format(db_file), DB.OREADER | DB.OCREATE):
print "Could not open database. (Top things)"
cur = db.cursor()
cur.jump_back()
while True:
rec = cur.get(False)
if not rec:
break
loaded_rec = loads(rec[1])
split = get_domain(loaded_rec)
if urls.get(split, False) == False:
urls[split] = 1
else:
urls[split] = urls[split] + 1
person = loaded_rec['person']
if people.get(person, False) == False:
people[person] = 1
else:
people[person] = people[person] + 1
if split is not None and split is not "" and \
person is not None and person is not "":
# Build a crazy relational graph out of my nosql data
if graph.get(split, False) == False:
graph[split] = {"is_person": False, "data": [person], "linked_to_count": 1}
elif person not in graph[split]:
graph[split]["data"].append(person)
graph[split]["linked_to_count"] = graph[split]["linked_to_count"] + 1
if graph.get(person, False) == False:
graph[person] = {"is_person": True, "data": [split]}
elif split not in graph[person]:
graph[person]["data"].append(split)
cur.step_back()
cur.disable()
db.close()
def get_one(x):
return x[1]
return (sorted(urls.items(), key=get_one, reverse=True),
sorted(people.items(), key=get_one, reverse=True),
graph)
示例6: get_post_by_date
def get_post_by_date(key, db_file):
item = None
db = DB()
if not db.open("{0}".format(db_file), DB.OREADER | DB.OCREATE):
print "Could not open database."
item = db.get(key)
db.close()
if item is not None:
return loads(item)
return dict()
示例7: dbOpen
def dbOpen(name):
db = DB()
if self.create:
# if not db.open(abspath(self.path) + '/' + name + ".kch",
# DB.OWRITER | DB.OCREATE | DB.OAUTOSYNC | DB.OAUTOTRAN):
if not db.open(abspath(self.path) + "/" + name + ".kch", DB.OWRITER | DB.OCREATE):
raise IOError("open error: " + str(db.error()))
return db
else:
# if not db.open(abspath(self.path) + '/' + name + ".kch",
# DB.OWRITER | DB.OAUTOSYNC | DB.OAUTOTRAN):
if not db.open(abspath(self.path) + "/" + name + ".kch", DB.OWRITER):
raise IOError("open error: " + str(db.error()))
return db
示例8: dbOpen
def dbOpen(name):
db = DB()
dbpathname = abspath(self.path) + '/' + name + ".kch"
if self.create:
# if not db.open(abspath(self.path) + '/' + name + ".kch",
# DB.OWRITER | DB.OCREATE | DB.OAUTOSYNC | DB.OAUTOTRAN):
if not db.open(dbpathname, DB.OWRITER | DB.OCREATE):
raise IOError("open error: %s %s" % (dbpathname, str(db.error()))) #pragma: NO COVER
return db
else:
# if not db.open(abspath(self.path) + '/' + name + ".kch",
# DB.OWRITER | DB.OAUTOSYNC | DB.OAUTOTRAN):
if not db.open(dbpathname, DB.OWRITER): #pragma: NO COVER
raise IOError("open error: %s %s" % (dbpathname, str(db.error()))) #pragma: NO COVER
return db
示例9: 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)
示例10: __enter__
def __enter__( self ):
if self._mode == "r":
with open( self._fn, "rb" ) as f:
state = pickle_load( f );
self._len_c = state[ "c" ];
self._len_b = state[ "b" ];
self._len_x = state[ "x" ];
self._lenrow = self._len_c + self._len_b + self._len_x;
self._ic = state[ "ic" ];
self._icbp = state[ "icbp" ];
if self._mode == "w":
with NamedTemporaryFile() as tmpfn:
self._kdbfn = tmpfn.name + '.kch';
self._kdb = KDB();
try:
assert self._kdb.open( self._kdbfn, KDB.OWRITER | KDB.OCREATE );
except:
print( str( self._kdb.error() ) );
raise;
with TemporaryDirectory() as tmpdirname:
self._ldbdn = tmpdirname;
self._ldb = LDB( self._ldbdn, create_if_missing=True );
return self;
示例11: purge
def purge(domain, genid):
if request.remote_addr not in settings.ALLOW:
return text_response("Not permitted.\n", 403)
db = DB()
if not db.open(settings.GENID_DATABASE, DB.OWRITER | DB.OCREATE):
return text_response("Failed to purge: cannot open database.\n", 501)
set_ok = db.set(domain, genid)
db.close()
if not set_ok:
return text_response("Failed to purge: cannot set genid.\n", 501)
else:
return text_response("Purged <%s>\n" % (domain,))
示例12: __enter__
def __enter__( self ):
self._cdata = DB();
self._ddata = DB();
try:
if self._mode == "r":
assert self._cdata.open( self._fn_cdata, DB.OREADER );
elif self._mode == "w":
if isfile( self._fn_cdata ):
remove( self._fn_cdata );
assert self._cdata.open( self._fn_cdata, DB.OWRITER | DB.OCREATE );
else:
assert False;
except:
if self._cdata is not None:
print( str( self._cdata.error() ) );
raise;
try:
if self._mode == "r":
assert self._ddata.open( self._fn_ddata, DB.OREADER );
elif self._mode == "w":
if isfile( self._fn_ddata ):
remove( self._fn_ddata );
assert self._ddata.open( self._fn_ddata, DB.OWRITER | DB.OCREATE );
else:
assert False;
except:
if self._ddata is not None:
print( str( self._ddata.error() ) );
raise;
if self._mode == "r":
with open( self._fn_meta, 'rb' ) as f:
r = pickle_load( f );
self._len_c = r[ "c" ];
self._len_b = r[ "b" ];
self._len_x = r[ "x" ];
self._co = r[ "co" ];
with open( self._fn_icov, 'rb' ) as f:
self._icov = pickle_load( f );
return self;
示例13: get_items_last_X_days
def get_items_last_X_days(db_file, X, munge=True):
dates = {}
db = DB()
if not db.open("{0}".format(db_file), DB.OREADER | DB.OCREATE):
print "Could not open database."
X_days_ago = datetime.now() - timedelta(days=X)
cur = db.cursor()
cur.jump_back()
while True:
rec = cur.get(False)
if not rec:
break
loaded = loads(rec[1])
unix = float(loaded['created_at'])
time = datetime.fromtimestamp(unix)
if time > X_days_ago:
if munge:
date_obj = date(year=time.year, month=time.month, day=time.day)
else:
date_obj = time
# Javascript expects Date.UTC to spit out dates of a certain
# length.
day_unix = int(mktime(date_obj.timetuple()))*1000
if dates.get(day_unix, False) == False:
dates[day_unix] = {loaded["person"]: 1}
else:
relevant_dict = dates[day_unix]
if relevant_dict.get(loaded["person"], False) == False:
relevant_dict[loaded["person"]] = 1
else:
relevant_dict[loaded["person"]] = relevant_dict[loaded["person"]] + 1
else:
break;
cur.step_back()
cur.disable()
db.close()
return dates
示例14: get_last_items
def get_last_items(db_file, pages=1):
items = []
db = DB()
if not db.open("{0}".format(db_file), DB.OREADER | DB.OCREATE):
print "Could not open database."
cur = db.cursor()
cur.jump_back()
while len(items) < (pages * FILTER_MAX):
rec = cur.get(False)
if not rec:
break
items.append(rec)
cur.step_back()
cur.disable()
db.close()
return items
示例15: get_all_items
def get_all_items(db_file):
items = []
db = DB()
if not db.open("{0}".format(db_file), DB.OREADER | DB.OCREATE):
print "Could not open database."
cur = db.cursor()
cur.jump()
while True:
rec = cur.get(False)
if not rec:
break
items.append(rec)
cur.step()
cur.disable()
db.close()
sorted_items_for_viewing = [loads(item[1]) for item in items]
return sorted_items_for_viewing