本文整理汇总了Python中r2.lib.wrapped.Wrapped.count方法的典型用法代码示例。如果您正苦于以下问题:Python Wrapped.count方法的具体用法?Python Wrapped.count怎么用?Python Wrapped.count使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类r2.lib.wrapped.Wrapped
的用法示例。
在下文中一共展示了Wrapped.count方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: context_from_comment
# 需要导入模块: from r2.lib.wrapped import Wrapped [as 别名]
# 或者: from r2.lib.wrapped.Wrapped import count [as 别名]
def context_from_comment(self, comment):
if isinstance(comment, Comment):
link = Link._byID(comment.link_id)
wrapped, = self.wrap_items((comment,))
# If there are any child comments, add an expand link
children = list(Comment._query(Comment.c.parent_id == comment._id))
if children:
more = Wrapped(MoreChildren(link, 0))
more.children.extend(children)
more.count = len(children)
wrapped.child = self.empty_listing()
wrapped.child.things.append(more)
# If there's a parent comment, surround the child comment with it
parent_id = getattr(comment, 'parent_id', None)
if parent_id is not None:
parent_comment = Comment._byID(parent_id)
if parent_comment:
parent_wrapped, = self.wrap_items((parent_comment,))
parent_wrapped.child = self.empty_listing()
parent_wrapped.child.things.append(wrapped)
wrapped = parent_wrapped
wrapped.show_response_to = True
else:
wrapped, = self.wrap_items((comment,))
return wrapped
示例2: get_items
# 需要导入模块: from r2.lib.wrapped import Wrapped [as 别名]
# 或者: from r2.lib.wrapped.Wrapped import count [as 别名]
#.........这里部分代码省略.........
comments = Comment._byID(items, data=True, return_dict=False,
stale=self.stale)
timer.intermediate("lookup_comments")
wrapped = self.wrap_items(comments)
timer.intermediate("wrap_comments")
wrapped_by_id = {comment._id: comment for comment in wrapped}
final = []
for comment in wrapped:
# skip deleted comments with no children
if (comment.deleted and not cid_tree.has_key(comment._id)
and not c.user_is_admin):
continue
comment.num_children = num_children[comment._id]
if comment.collapsed and comment._id in dont_collapse:
comment.collapsed = False
# add the comment as a child of its parent or to the top level of
# the tree if it has no parent
parent = wrapped_by_id.get(comment.parent_id)
if parent:
if not hasattr(parent, 'child'):
parent.child = empty_listing()
if not parent.deleted:
parent.child.parent_name = parent._fullname
parent.child.things.append(comment)
else:
final.append(comment)
for parent_id, more_recursion in more_recursions.iteritems():
if parent_id not in wrapped_by_id:
continue
parent = wrapped_by_id[parent_id]
parent.child = empty_listing(more_recursion)
if not parent.deleted:
parent.child.parent_name = parent._fullname
timer.intermediate("build_comments")
if not self.load_more:
timer.stop()
return final
# build MoreChildren for visible comments
visible_comments = wrapped_by_id.keys()
for visible_id in visible_comments:
if visible_id in more_recursions:
# don't add a MoreChildren if we already have a MoreRecursion
continue
children = cid_tree.get(visible_id, ())
missing_children = [child for child in children
if child not in visible_comments]
if missing_children:
visible_children = (child for child in children
if child in visible_comments)
visible_count = sum(1 + num_children[child]
for child in visible_children)
missing_count = num_children[visible_id] - visible_count
missing_depth = depth.get(visible_id, 0) + 1 - offset_depth
if missing_depth < self.max_depth:
mc = MoreChildren(self.link, depth=missing_depth,
parent_id=visible_id)
mc.children.extend(missing_children)
w = Wrapped(mc)
w.count = missing_count
else:
mr = MoreRecursion(self.link, depth=missing_depth,
parent_id=visible_id)
w = Wrapped(mr)
# attach the MoreChildren
parent = wrapped_by_id[visible_id]
if hasattr(parent, 'child'):
parent.child.things.append(w)
else:
parent.child = empty_listing(w)
if not parent.deleted:
parent.child.parent_name = parent._fullname
# build MoreChildren for missing root level comments
if top_level_candidates:
mc = MoreChildren(self.link, depth=0, parent_id=None)
mc.children.extend(top_level_candidates)
w = Wrapped(mc)
w.count = sum(1 + num_children[comment]
for comment in top_level_candidates)
final.append(w)
if isinstance(self.sort, operators.shuffled):
shuffle(final)
timer.intermediate("build_morechildren")
timer.stop()
return final
示例3: _make_wrapped_tree
# 需要导入模块: from r2.lib.wrapped import Wrapped [as 别名]
# 或者: from r2.lib.wrapped.Wrapped import count [as 别名]
def _make_wrapped_tree(self):
timer = self.timer
comments = self.comments
cid_tree = self.cid_tree
top_level_candidates = self.top_level_candidates
depth = self.depth
more_recursions = self.more_recursions
offset_depth = self.offset_depth
dont_collapse = self.dont_collapse
timer.intermediate("waiting")
if not comments and not top_level_candidates:
timer.stop()
return []
# retrieve num_children for the visible comments
needs_num_children = [c._id for c in comments] + top_level_candidates
num_children = get_num_children(needs_num_children, cid_tree)
timer.intermediate("calc_num_children")
wrapped = self.wrap_items(comments)
timer.intermediate("wrap_comments")
wrapped_by_id = {comment._id: comment for comment in wrapped}
if self.children:
# rewrite the parent links to use anchor tags
for comment_id in self.children:
if comment_id in wrapped_by_id:
item = wrapped_by_id[comment_id]
if item.parent_id:
item.parent_permalink = '#' + to36(item.parent_id)
final = []
# We have some special collapsing rules for the Q&A sort type.
# However, we want to show everything when we're building a specific
# set of children (like from "load more" links) or when viewing a
# comment permalink.
qa_sort_hiding = ((self.sort.col == '_qa') and not self.children and
self.comment is None)
if qa_sort_hiding:
special_responder_ids = self.link.responder_ids
else:
special_responder_ids = ()
max_relation_walks = g.max_comment_parent_walk
for comment in wrapped:
# skip deleted comments with no children
if (comment.deleted and not cid_tree.has_key(comment._id)
and not self.show_deleted):
comment.hidden_completely = True
continue
comment.num_children = num_children[comment._id]
comment.edits_visible = self.edits_visible
parent = wrapped_by_id.get(comment.parent_id)
if qa_sort_hiding:
author_is_special = comment.author_id in special_responder_ids
else:
author_is_special = False
# In the Q&A sort type, we want to collapse all comments other than
# those that are:
#
# 1. Top-level comments,
# 2. Responses from the OP(s),
# 3. Responded to by the OP(s) (dealt with below),
# 4. Within one level of an OP reply, or
# 5. Otherwise normally prevented from collapse (eg distinguished
# comments).
if (qa_sort_hiding and
depth[comment._id] != 0 and # (1)
not author_is_special and # (2)
not (parent and
parent.author_id in special_responder_ids and
feature.is_enabled('qa_show_replies')) and # (4)
not comment.prevent_collapse): # (5)
comment.hidden = True
if comment.collapsed:
if comment._id in dont_collapse or author_is_special:
comment.collapsed = False
comment.hidden = False
if parent:
if author_is_special:
# Un-collapse parents as necessary. It's a lot easier to
# do this here, upwards, than to check through all the
# children when we were iterating at the parent.
ancestor = parent
counter = 0
while (ancestor and
not getattr(ancestor, 'walked', False) and
counter < max_relation_walks):
ancestor.hidden = False
# In case we haven't processed this comment yet.
ancestor.prevent_collapse = True
# This allows us to short-circuit when the rest of the
# tree has already been uncollapsed.
#.........这里部分代码省略.........
示例4: get_items
# 需要导入模块: from r2.lib.wrapped import Wrapped [as 别名]
# 或者: from r2.lib.wrapped.Wrapped import count [as 别名]
#.........这里部分代码省略.........
and not self.show_deleted):
comment.hidden_completely = True
continue
comment.num_children = num_children[comment._id]
comment.edits_visible = self.edits_visible
# In the Q&A sort type, we want to collapse all comments other than
# those that are:
#
# 1. Top-level comments,
# 2. Responses from the OP(s),
# 3. Responded to by the OP(s) (dealt with below), or
# 4. Otherwise normally prevented from collapse (eg distinguished
# comments).
if (qa_sort_hiding and
depth[comment._id] != 0 and # (1)
comment.author_id not in special_responder_ids and # (2)
not comment.prevent_collapse): # (4)
comment.hidden = True
if comment.collapsed and comment._id in dont_collapse:
comment.collapsed = False
comment.hidden = False
parent = wrapped_by_id.get(comment.parent_id)
if parent:
if (qa_sort_hiding and
comment.author_id in special_responder_ids):
# Un-collapse parents as necessary. It's a lot easier to
# do this here, upwards, than to check through all the
# children when we were iterating at the parent.
ancestor = parent
counter = 0
while (ancestor and
not getattr(ancestor, 'walked', False) and
counter < max_relation_walks):
ancestor.hidden = False
# In case we haven't processed this comment yet.
ancestor.prevent_collapse = True
# This allows us to short-circuit when the rest of the
# tree has already been uncollapsed.
ancestor.walked = True
ancestor = wrapped_by_id.get(ancestor.parent_id)
counter += 1
# One more time through to actually add things to the final list. We
# couldn't do that the first time because in the Q&A sort we don't know
# if a comment should be visible until after we've processed all its
# children.
for comment in wrapped:
if getattr(comment, 'hidden_completely', False):
# Don't add it to the tree, don't put it in "load more", don't
# acknowledge its existence at all.
continue
if getattr(comment, 'hidden', False):
# Remove it from the list of visible comments so it'll
# automatically be a candidate for the "load more" links.
del wrapped_by_id[comment._id]
# And don't add it to the tree.
continue
# add the comment as a child of its parent or to the top level of
# the tree if it has no parent