From ecf986608a6cb82efc613da468e1c21745b69aee Mon Sep 17 00:00:00 2001 From: Brian Simpson Date: Fri, 14 Feb 2014 02:33:57 -0500 Subject: [PATCH] Move get_num_children to cython file. --- r2/r2/lib/utils/comment_tree_utils.pyx | 61 ++++++++++++++++++++++++++ r2/r2/models/builder.py | 36 +-------------- 2 files changed, 62 insertions(+), 35 deletions(-) create mode 100644 r2/r2/lib/utils/comment_tree_utils.pyx diff --git a/r2/r2/lib/utils/comment_tree_utils.pyx b/r2/r2/lib/utils/comment_tree_utils.pyx new file mode 100644 index 000000000..3145475db --- /dev/null +++ b/r2/r2/lib/utils/comment_tree_utils.pyx @@ -0,0 +1,61 @@ +# The contents of this file are subject to the Common Public Attribution +# License Version 1.0. (the "License"); you may not use this file except in +# compliance with the License. You may obtain a copy of the License at +# http://code.reddit.com/LICENSE. The License is based on the Mozilla Public +# License Version 1.1, but Sections 14 and 15 have been added to cover use of +# software over a computer network and provide for limited attribution for the +# Original Developer. In addition, Exhibit A has been modified to be consistent +# with Exhibit B. +# +# Software distributed under the License is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for +# the specific language governing rights and limitations under the License. +# +# The Original Code is reddit. +# +# The Original Developer is the Initial Developer. The Initial Developer of +# the Original Code is reddit Inc. +# +# All portions of the code written by reddit are Copyright (c) 2006-2013 reddit +# Inc. All Rights Reserved. +############################################################################### + +def get_num_children(list comments, dict tree): + """Count the number of children for each comment.""" + + cdef: + dict num_children = {} + list stack = [] + list children = [] + list missing = [] + long comment + long current + long child + + for comment in sorted(comments): + stack.append(comment) + + while stack: + current = stack[-1] + + if current in num_children: + stack.pop() + continue + + children = tree.get(current, []) + + for child in children: + if child not in num_children and not tree.get(child, None): + num_children[child] = 0 + + missing = [child for child in children if not child in num_children] + + if not missing: + num_children[current] = 0 + stack.pop() + for child in children: + num_children[current] += 1 + num_children[child] + else: + stack.extend(missing) + + return num_children diff --git a/r2/r2/models/builder.py b/r2/r2/models/builder.py index cf3fb23a5..fdfdd0005 100755 --- a/r2/r2/models/builder.py +++ b/r2/r2/models/builder.py @@ -47,6 +47,7 @@ from r2.lib.db import operators, tdb_cassandra from r2.lib.filters import _force_unicode from copy import deepcopy from r2.lib.utils import Storage +from r2.lib.utils.comment_tree_utils import get_num_children from r2.models import wiki @@ -907,41 +908,6 @@ class CommentBuilder(Builder): yield j -def get_num_children(comments, tree): - """Count the number of children for each comment.""" - - num_children = {} - stack = [] - - for comment in sorted(comments): - stack.append(comment) - - while stack: - current = stack[-1] - - if current in num_children: - stack.pop() - continue - - children = tree.get(current, []) - - for child in children: - if child not in num_children and not tree.get(child, None): - num_children[child] = 0 - - missing = [child for child in children if not child in num_children] - - if not missing: - num_children[current] = 0 - stack.pop() - for child in children: - num_children[current] += 1 + num_children[child] - else: - stack.extend(missing) - - return num_children - - class MessageBuilder(Builder): def __init__(self, parent = None, focal = None, skip = True, **kw):