本文整理汇总了Python中r2.models.comment_tree.CommentTree.by_link方法的典型用法代码示例。如果您正苦于以下问题:Python CommentTree.by_link方法的具体用法?Python CommentTree.by_link怎么用?Python CommentTree.by_link使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类r2.models.comment_tree.CommentTree
的用法示例。
在下文中一共展示了CommentTree.by_link方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_comments
# 需要导入模块: from r2.models.comment_tree import CommentTree [as 别名]
# 或者: from r2.models.comment_tree.CommentTree import by_link [as 别名]
def add_comments(comments):
"""Add comments to the CommentTree and update scores."""
from r2.models.builder import write_comment_orders
link_ids = [comment.link_id for comment in tup(comments)]
links = Link._byID(link_ids, data=True)
comments = tup(comments)
comments_by_link_id = defaultdict(list)
for comment in comments:
comments_by_link_id[comment.link_id].append(comment)
for link_id, link_comments in comments_by_link_id.iteritems():
link = links[link_id]
timer = g.stats.get_timer(
'comment_tree.add.%s' % link.comment_tree_version)
timer.start()
# write scores before CommentTree because the scores must exist for all
# comments in the tree
for sort in ("_controversy", "_confidence", "_score"):
scores_by_comment = {
comment._id36: getattr(comment, sort)
for comment in link_comments
}
CommentScoresByLink.set_scores(link, sort, scores_by_comment)
scores_by_comment = _get_qa_comment_scores(link, link_comments)
CommentScoresByLink.set_scores(link, "_qa", scores_by_comment)
timer.intermediate('scores')
with CommentTree.mutation_context(link, timeout=180):
try:
timer.intermediate('lock')
comment_tree = CommentTree.by_link(link, timer)
timer.intermediate('get')
comment_tree.add_comments(link_comments)
timer.intermediate('update')
except InconsistentCommentTreeError:
# failed to add a comment to the CommentTree because its parent
# is missing from the tree. this comment will be lost forever
# unless a rebuild is performed.
comment_ids = [comment._id for comment in link_comments]
g.log.error(
"comment_tree_inconsistent: %s %s" % (link, comment_ids))
g.stats.simple_event('comment_tree_inconsistent')
return
# do this under the same lock because we want to ensure we are using
# the same version of the CommentTree as was just written
write_comment_orders(link)
timer.intermediate('write_order')
timer.stop()
示例2: activate_names_requested_in
# 需要导入模块: from r2.models.comment_tree import CommentTree [as 别名]
# 或者: from r2.models.comment_tree.CommentTree import by_link [as 别名]
def activate_names_requested_in(link):
comment_tree = CommentTree.by_link(link)
acceptable_names = []
if comment_tree.tree:
top_level_cids = comment_tree.tree[None]
comments = chain.from_iterable(Comment._byID(chunk, return_dict=False,
data=True)
for chunk in in_chunks(top_level_cids))
for comment in sorted(comments, key=lambda c: c._ups, reverse=True):
if comment._spam or comment._deleted:
continue
sanitized = comment.body.strip()
match = valid_name_re.search(sanitized)
if match:
acceptable_names.append((comment, match.group(1)))
# we activate one name for each 100% of rev goal met
names = acceptable_names[:link.revenue_bucket]
activate_names(link, names)
activated_names = [name for comment, name in names]
link.server_names = activated_names
link.flair_text = ", ".join(activated_names) if names else "/dev/null"
link.flair_css_class = "goal-bucket-%d" % link.revenue_bucket
link._commit()
示例3: calculate_comment_scores
# 需要导入模块: from r2.models.comment_tree import CommentTree [as 别名]
# 或者: from r2.models.comment_tree.CommentTree import by_link [as 别名]
def calculate_comment_scores(link, sort, comments):
if sort in ("_controversy", "_confidence", "_score"):
scores = {
comment._id36: getattr(comment, sort)
for comment in comments
}
elif sort == "_qa":
comment_tree = CommentTree.by_link(link)
cid_tree = comment_tree.tree
scores = _calculate_qa_comment_scores(link, cid_tree, comments)
else:
raise ValueError("unsupported comment sort %s" % sort)
return scores
示例4: get_comment_tree
# 需要导入模块: from r2.models.comment_tree import CommentTree [as 别名]
# 或者: from r2.models.comment_tree.CommentTree import by_link [as 别名]
def get_comment_tree(link, _update=False, timer=None):
if timer is None:
timer = SimpleSillyStub()
cache = CommentTree.by_link(link)
timer.intermediate('load')
if cache and not _update:
return cache
with CommentTree.mutation_context(link, timeout=180):
timer.intermediate('lock')
cache = CommentTree.rebuild(link)
timer.intermediate('rebuild')
# the tree rebuild updated the link's comment count, so schedule it for
# search reindexing
link.update_search_index()
timer.intermediate('update_search_index')
return cache
示例5: get_comment_tree
# 需要导入模块: from r2.models.comment_tree import CommentTree [as 别名]
# 或者: from r2.models.comment_tree.CommentTree import by_link [as 别名]
def get_comment_tree(link, _update=False, timer=None):
if timer is None:
timer = SimpleSillyStub()
cache = CommentTree.by_link(link)
timer.intermediate("load")
if cache and not _update:
return cache
with CommentTree.mutation_context(link, timeout=180):
timer.intermediate("lock")
cache = CommentTree.rebuild(link)
timer.intermediate("rebuild")
# the tree rebuild updated the link's comment count, so schedule it for
# search reindexing
from r2.lib.db.queries import changed
changed([link])
timer.intermediate("changed")
return cache
示例6: _get_qa_comment_scores
# 需要导入模块: from r2.models.comment_tree import CommentTree [as 别名]
# 或者: from r2.models.comment_tree.CommentTree import by_link [as 别名]
def _get_qa_comment_scores(link, comments):
"""Return a dict of comment_id36 -> qa score"""
# Responder is usually the OP, but there could be support for adding
# other answerers in the future.
responder_ids = link.responder_ids
# An OP response will change the sort value for its parent, so we need
# to process the parent, too.
parent_cids = []
for comment in comments:
if comment.author_id in responder_ids and comment.parent_id:
parent_cids.append(comment.parent_id)
parent_comments = Comment._byID(parent_cids, data=True, return_dict=False)
comments.extend(parent_comments)
comment_tree = CommentTree.by_link(link)
cid_tree = comment_tree.tree
# Fetch the comments in batch to avoid a bunch of separate calls down
# the line.
all_child_cids = []
for comment in comments:
child_cids = cid_tree.get(comment._id, None)
if child_cids:
all_child_cids.extend(child_cids)
all_child_comments = Comment._byID(all_child_cids, data=True)
comment_sorter = {}
for comment in comments:
child_cids = cid_tree.get(comment._id, ())
child_comments = (all_child_comments[cid] for cid in child_cids)
sort_value = comment._qa(child_comments, responder_ids)
comment_sorter[comment._id36] = sort_value
return comment_sorter
示例7: get_comment_tree
# 需要导入模块: from r2.models.comment_tree import CommentTree [as 别名]
# 或者: from r2.models.comment_tree.CommentTree import by_link [as 别名]
def get_comment_tree(link, timer=None):
if timer is None:
timer = SimpleSillyStub()
cache = CommentTree.by_link(link, timer)
return cache
示例8: add_comments
# 需要导入模块: from r2.models.comment_tree import CommentTree [as 别名]
# 或者: from r2.models.comment_tree.CommentTree import by_link [as 别名]
def add_comments(comments):
"""Add comments to the CommentTree and update scores."""
from r2.models.builder import write_comment_orders
link_ids = [comment.link_id for comment in tup(comments)]
links = Link._byID(link_ids, data=True)
comments = tup(comments)
comments_by_link_id = defaultdict(list)
for comment in comments:
comments_by_link_id[comment.link_id].append(comment)
for link_id, link_comments in comments_by_link_id.iteritems():
link = links[link_id]
new_comments = [
comment for comment in link_comments if not comment._deleted]
deleted_comments = [
comment for comment in link_comments if comment._deleted]
timer = g.stats.get_timer(
'comment_tree.add.%s' % link.comment_tree_version)
timer.start()
# write scores before CommentTree because the scores must exist for all
# comments in the tree
for sort in ("_controversy", "_confidence", "_score"):
scores_by_comment = {
comment._id36: getattr(comment, sort)
for comment in link_comments
}
CommentScoresByLink.set_scores(link, sort, scores_by_comment)
scores_by_comment = _get_qa_comment_scores(link, link_comments)
CommentScoresByLink.set_scores(link, "_qa", scores_by_comment)
timer.intermediate('scores')
with CommentTree.mutation_context(link, timeout=180):
try:
timer.intermediate('lock')
comment_tree = CommentTree.by_link(link, timer)
timer.intermediate('get')
if new_comments:
comment_tree.add_comments(new_comments)
for comment in deleted_comments:
comment_tree.delete_comment(comment, link)
timer.intermediate('update')
except InconsistentCommentTreeError:
# this exception occurs when we add a comment to the tree but
# its parent isn't in the tree yet, need to rebuild the tree
# from scratch
comment_ids = [comment._id for comment in link_comments]
g.log.exception(
'add_comments_nolock failed for link %s %s, recomputing',
link_id, comment_ids)
comment_tree = CommentTree.rebuild(link)
timer.intermediate('rebuild')
# the tree rebuild updated the link's comment count, so schedule
# it for search reindexing
link.update_search_index()
timer.intermediate('update_search_index')
g.stats.simple_event('comment_tree_inconsistent')
# do this under the same lock because we want to ensure we are using
# the same version of the CommentTree as was just written
write_comment_orders(link, timer)
timer.intermediate('write_order')
timer.stop()