def make_random_post_tree(parent_post, amount, thread, ratio=0.5): text = '\n\n'.join([sampledata.sample_data() for x in range(0, randrange(1, 6))]) # the post data postdata = { "user": choice(User.objects.all()), "thread": thread, "text": text, "ip": '.'.join([str(choice(range(2, 254))) for x in xrange(4)]), } # Create a post in tree. if parent_post is None: # Root node. post = Post.add_root(**postdata) else: post = parent_post.add_child(**postdata) # allows setting of arbitrary ip post.save() posts_remain = amount - 1 # Minus just-created one # ... got better ideas? # (got non-uniform random distributions?) # Anyway, ratio ~= depth/width. Don't delegate too few / too much to # each child depending on the ratio. fmin = lambda ratio, posts_remain: int(posts_remain * (ratio - 0.5) * 2) + 1 if ratio > 0.5 else 1 fmax = lambda ratio, posts_remain: int(posts_remain * ratio * 2) + 2 \ if ratio < 0.5 else posts_remain + 1 while posts_remain > 0: # Distribute remnants next_tree_posts = randrange(fmin(ratio, posts_remain), fmax(ratio, posts_remain)) #print(" D: delegating %d, %d remain " % (xx, x-xx)) make_random_post_tree(post, next_tree_posts, thread) posts_remain -= next_tree_posts
def _make_testtrees(maxdepth=512, **kwargs): user = User.objects.all()[0] thr = Thread(subject="Test trees.", category=Category.objects.all()[0]) thr.save() top_post = Post.add_root(user=user, thread=thr, text="subj.") try: post = top_post print "lin" for curn in xrange(2, maxdepth): post = post.add_child(text="#1 at %d (lin)" % curn, user=post.user, thread=post.thread) print curn except Exception: traceback.print_exc() try: post = top_post print "dbl" for curn in xrange(2, maxdepth): post1 = post.add_child(text="#1 at %d (dbl)" % curn, user=post.user, thread=post.thread) post2 = post.add_child(text="#2 at %d (dbl)" % curn, user=post.user, thread=post.thread) post = post1 print curn except Exception: traceback.print_exc() try: post = top_post print "rnd" for curn in xrange(2, maxdepth): post1 = post.add_child(text="#1 at %d (rnd)" % curn, user=post.user, thread=post.thread) post2 = post.add_child(text="#2 at %d (rnd)" % curn, user=post.user, thread=post.thread) post1.save() post2.save() post = post1 if random.choice([True, False]) else post2 print curn except Exception: traceback.print_exc()