Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2023
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from datetime import datetime, timedelta, timezone
  4. from random import choice, randint, seed
  5.  
  6. NUM_COMMENTS = 600
  7. ORIG_POSTS = int(NUM_COMMENTS / 10)
  8. DAY_SECONDS = 24 * 60 * 60
  9. START = datetime.now(tz=timezone.utc) - timedelta(seconds=DAY_SECONDS)
  10. # random interval between a post and a reply to it - range from 0 to this
  11. DELAY_INTERVAL = DAY_SECONDS / 12
  12.  
  13. # deterministic
  14. seed(0)
  15.  
  16.  
  17. class Comment:
  18.     def __init__(self, num: int, replies: list["Comment"]):
  19.         self.content: int = num
  20.         self.replies: list["Comment"] = replies
  21.         self.post_date: datetime | None = None
  22.         self.level: int | None = None
  23.  
  24.     def __lt__(self, other: "Comment"):
  25.         return self.post_date < other.post_date
  26.  
  27.     def total_children(self) -> int:
  28.         return 1 + sum([c.total_children() for c in self.replies])
  29.  
  30.  
  31.  
  32. def flatten_comments(comments: list[Comment]) -> list[Comment]:
  33.     return sorted(
  34.         {
  35.             c for c in
  36.             [
  37.                 r for c in comments
  38.                 for r in
  39.                 [
  40.                     c, *flatten_comments(c.replies)
  41.                 ]
  42.             ]
  43.         }
  44.     )
  45.  
  46.  
  47. # all comments
  48. comments = [Comment(i, []) for i in range(NUM_COMMENTS)]
  49. # set the first one as root of the tree
  50. comments[0].post_date = START
  51. comments[0].level = 0
  52.  
  53. # split into original posts and replies
  54. posts, replies = comments[:ORIG_POSTS], comments[ORIG_POSTS:]
  55.  
  56. # for all but the first post, set the date later than the first, and note they're top-level
  57. for post in posts[1:]:
  58.     post.post_date = START + timedelta(seconds=randint(0, DAY_SECONDS))
  59.     post.level = 0
  60.  
  61. # assign each reply a randomly-chosen parent.  Set its level 1 higher than its parent, a post
  62. # date later than its parent, and add it to that parent's replies
  63. for reply in replies:
  64.     parent = choice(posts)
  65.     reply.post_date = parent.post_date + timedelta(seconds=randint(0, DELAY_INTERVAL))
  66.     reply.level = parent.level + 1
  67.     parent.replies.append(reply)
  68.     # then move the reply into the posts list, so it can be assigned replies as well
  69.     posts.append(reply)
  70.  
  71. flattened = flatten_comments(comments)
  72.  
  73. for comment in flattened:
  74.     print(
  75.         f"{comment.post_date.isoformat()} - "
  76.         f"level {comment.level}: '{comment.content:-3d}' - "
  77.         f"{len(comment.replies):-3d} replies - {comment.total_children():-3d} children"
  78.     )
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement