本文整理汇总了Python中kyotocabinet.DB.cursor方法的典型用法代码示例。如果您正苦于以下问题:Python DB.cursor方法的具体用法?Python DB.cursor怎么用?Python DB.cursor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kyotocabinet.DB
的用法示例。
在下文中一共展示了DB.cursor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_items
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
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
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
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
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
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: top_things
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
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)
示例5: get_items_last_X_days
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
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
示例6: get_last_items
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
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
示例7: get_all_items
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
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
示例8: gen_thumbnails
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
def gen_thumbnails(db_file):
db = DB()
if not db.open("{0}".format(db_file), DB.OREADER | DB.OWRITER):
sys.exit(1)
cur = db.cursor()
cur.jump_back()
while True:
rec = cur.get(False)
if not rec:
break
loaded = loads(rec[1])
is_image = loaded["url"].lower().endswith(("jpg", "jpeg", "gif", "png"))
if is_image:
print "Thumbnailing {}".format(loaded["url"])
loaded["is_image"] = True
try:
thumbnail = gen_thumbnail_for_url(loaded["url"], rec[0])
except IOError as e:
print "IOError: {}".format(e)
print "Save result: {}".format(cur.set_value(dumps(loaded)))
cur.step_back()
continue
if thumbnail:
loaded["thumbnail"] = thumbnail
print "Thumbnailed {}".format(loaded["url"])
print "Save result: {}".format(cur.set_value(dumps(loaded)))
cur.step_back()
cur.disable()
db.close()
return True
示例9: get_page_count
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
def get_page_count(item_filter = lambda x: True):
count = 0
db = DB()
db_file = current_app.config['DB_FILE']
if not db.open("{0}".format(db_file), DB.OREADER | DB.OWRITER | DB.OCREATE):
print "Could not open database (get_page_count). Error: {}".format(db.error())
cur = db.cursor()
cur.jump_back()
while True:
rec = cur.get(False)
if not rec:
break
if item_filter(rec):
count = count + 1
cur.step_back()
cur.disable()
db.close()
return count / FILTER_MAX
示例10: get_items_on_page
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
def get_items_on_page(page, db_file):
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):
items.append(rec)
item_iter = item_iter + 1
cur.step_back()
cur.disable()
db.close()
return items
示例11: main
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
def main():
db_file = argv[1]
username = argv[2]
if not db_file and not username:
print "Need db_file and username."
return -1
db = DB()
if not db.open("{0}".format(db_file), DB.OWRITER):
print "Could not open database."
return -1
all_keys = []
cur = db.cursor()
cur.jump()
while True:
rec = cur.get(False)
if not rec:
break
loaded = loads(rec[1])
if loaded["person"] == username:
all_keys.append(cur.get_key())
cur.step()
cur.disable()
print "Found {} records.".format(len(all_keys))
for key in all_keys:
print "Pending {}...".format(key)
if len(argv) > 3 and argv[3] == '--delete':
print "Removing {}...".format(key)
if not db.remove(key):
print "Could not remove key: {}".format(db.error())
db.close()
示例12: FeatureSelector
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
#.........这里部分代码省略.........
if not ( ( key[0] == i ) and ( key[1] == j ) ):
break;
# key[2] is the y-value, key[3] the i-th value for the i-th dim
a = ( key[2], key[3] );
# key[2] is the y-value, key[4] the i-th value for the j-th dim
b = ( key[2], key[4] );
assert (a,b) not in cnt_by_ab;
cnt_by_ab[ (a,b) ] = cnt_by_ab.get( (a,b), 0 ) + val;
cnt_by_a[ a ] = cnt_by_a.get( a, 0 ) + val;
cnt_by_b[ b ] = cnt_by_b.get( b, 0 ) + val;
total += val;
assert total == self._rowcount;
return self._stats( cnt_by_a, cnt_by_b, cnt_by_ab );
def _finalize( self ):
assert Frontend._finalize( self ) is None;
if False:
print( "unique combinations = ", self._kdb.count() );
keyfmt = '>IIIII';
valfmt = '>Q';
c = self._kdb.cursor();
c.jump();
gt2 = 0;
gt4 = 0;
gt8 = 0;
gt16 = 0;
gt32 = 0;
while True:
r = c.get( True );
if not r:
break;
self._ldb.put( r[0], r[1] );
key = unpack( keyfmt, r[0] );
val = unpack( valfmt, r[1] )[ 0 ];
if val > 2:
gt2 += 1;
if val > 4:
gt4 += 1;
if val > 8:
gt8 += 1;
if val > 16:
gt16 += 1;
if val > 32:
gt32 += 1;
if False:
print( gt2, gt4, gt8, gt16, gt32 );
示例13: get_user_stats
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
def get_user_stats(username, db_file):
item = {
"username": username,
"aliases": [],
"total_posts": 0,
"domains": {},
"first_post_date": None,
"first_post_date_unix": None,
"most_recent_post": None,
"most_recent_post_unix": 0,
"average_posts_per_hour": 0.0,
"average_posts_per_day": 0.0,
"average_posts_per_week": 0.0
}
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
loaded_rec = loads(rec[1])
if loaded_rec['person'] != username:
cur.step()
continue
# Looks like this is a post by the user we're looking for
split = get_domain(loaded_rec)
if item['domains'].get(split, False) == False:
item['domains'][split] = 1
else:
item['domains'][split] = item['domains'][split] + 1
if item['first_post_date_unix'] is None:
item['first_post_date_unix'] = loaded_rec['created_at']
if item['most_recent_post_unix'] < loaded_rec['created_at']:
item['most_recent_post_unix'] = loaded_rec['created_at']
item['total_posts'] = item['total_posts'] + 1
cur.step()
cur.disable()
db.close()
# Clean up everything
first_time = None
if item['first_post_date_unix'] is not None:
unix = float(item['first_post_date_unix'])
first_time = datetime.fromtimestamp(unix)
item['first_post_date'] = first_time.isoformat()
recent_time = None
if item['most_recent_post_unix'] is not None:
unix = float(item['most_recent_post_unix'])
recent_time = datetime.fromtimestamp(unix)
item['most_recent_post'] = recent_time.isoformat()
if first_time and recent_time:
delta = recent_time - first_time
item['user_age_days'] = delta.days
item['user_age_seconds'] = delta.total_seconds()
item['average_posts_per_hour'] = item['total_posts'] / (delta.total_seconds() / 60.0)
item['average_posts_per_day'] = item['total_posts'] / (delta.total_seconds() / 60.0 / 24.0)
item['average_posts_per_week'] = item['total_posts'] / (delta.total_seconds() / 60.0 / 24.0 / 7.0)
return item
示例14: BKNNModel
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [as 别名]
#.........这里部分代码省略.........
if self._rowcount is None:
self._rowcount = 0;
self._rowcount += 1;
dkeyfmt = '>' + ( 'I' * ( 1 + self._len_c + self._len_b ) );
self._ddata.increment( pack( dkeyfmt, y, *(c+b) ), 1, 0 );
ckeyfmt = '>' + ( 'I' * len(x) );
cvalfmt = '>I' + ( 'f' * len(x) );
self._cdata.append( pack( ckeyfmt, *x_ ), pack( cvalfmt, y, *x ) );
if len( self._sample_x ) < 50000:
assert len( self._sample_x ) == len( self._sample_y );
assert len( self._sample_x ) == len( self._sample_c );
assert len( self._sample_x ) == len( self._sample_b );
assert len( self._sample_x ) == len( self._sample_x_ );
self._sample_y.append( y );
self._sample_c.append( c );
self._sample_b.append( b );
self._sample_x.append( x );
self._sample_x_.append( x_ );
return False;
def _init( self ):
self._needs_initialization = False;
c = self._ddata.cursor();
c.jump();
keyfmt = '>' + ( 'I' * ( 1 + self._len_c + self._len_b ) );
valfmt = '>Q';
while True:
r = c.get( True );
if not r:
break;
dbkey = unpack( keyfmt, r[0] );
dbval = unpack( valfmt, r[1] )[ 0 ];
additional_count = dbval;
y = dbkey[ 0 ];
for ( i, value_of_variable_i ) in enumerate( dbkey[ 1: ] ):
if not i in self._dmarginals:
self._dmarginals[ i ] = {};
self._dmarginals[ i ][ (y,value_of_variable_i) ] \
= self._dmarginals[ i ].get( (y,value_of_variable_i), 0 ) \
+ additional_count;
for ( i, count_by_val ) in self._dmarginals.items():
total = 0;
示例15: DataStorage
# 需要导入模块: from kyotocabinet import DB [as 别名]
# 或者: from kyotocabinet.DB import cursor [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)