What I am attempting to do is iterate over a list of comments, remove the current comment that has been added to a value called cText
, and if cText
goes over 9000 characters make a recursive call into the same function and add it's returned value into botComments
. I'm also attempting to merge the returned botComments
list from a recursed function to the top-most botComments
list with botComments + repliesToComments(comments, author)
though I don't know if I'm doing anything right. Oddly, it also seems it can't find a comment in a list that for-sure has the comment throwing the exception I pasted below. I was horrible at recursion during my AP Comp-Sci class though I wish to learn what I'm doing wrong from whomever can fix my mess of code below. Thanks :)
Here's my current exception:
Traceback (most recent call last):
File "C:\Users\Josh\Desktop\bot.py", line 62, in <module>
print(repliesToComments(submission.comments, submission.author.name))
File "C:\Users\Josh\Desktop\bot.py", line 36, in repliesToComments
botComments + repliesToComments(comments, author)
File "C:\Users\Josh\Desktop\bot.py", line 39, in repliesToComments
comments.remove(comment)
ValueError: list.remove(x): x not in list
Here's my code:
def repliesToComments(comments, author):
botComments = []
multiReplies = False
cText = "Here is a list of all the comments /u/" + author + ''' has submitted to top-level comments:
***
Original Comment | /u/''' + author + ''''s Reply
---------|---------
'''
for comment in comments[:]:
if (hasattr(comment, "replies")):
for comment2 in comment.replies:
if (hasattr(comment2, "author") and comment2.author.name == author):
oCommentLink = "[" + comment.body.replace("\n", "")[:50] + "](" + submission.permalink + "" + comment2.parent_id.replace("t1_", "") + ")..."
rCommentLink = "[" + comment2.body.replace("\n", "")[:50] + "](" + submission.permalink + "" + comment2.name.replace("t1_", "") + ")..."
if (len(cText) + len(oCommentLink + " | " + rCommentLink + "\n") > 9000): #Stop here to make sure we don't go over 10000 char limit!
multiReplies = True
cText += '''
***
[FAQ](http://pastebin.com/raw.php?i=wUGmE8X5) | Generated at ''' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " PST" + " | *This comment only shows a partial amount of OPs total replies due to character limit. The rest are shown in a reply to this comment.*"
botComments.append(cText)
botComments + repliesToComments(comments, author)
break
cText += oCommentLink + " | " + rCommentLink + "\n"
comments.remove(comment)
if (multiReplies == False):
cText += '''
***
[FAQ](http://pastebin.com/raw.php?i=wUGmE8X5) | Generated at ''' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " PST"
botComments.append(cText)
return botComments
You are calling repliesToComments()
recursively, but with the exact same arguments . This can only lead to problems.
Your recursive call is removing entries from the same list, and when it returns, those entries will still be gone . But because you loop over a copy of the list, your outer call won't be updated and try to remove comments the inner recursive call removed already .
Perhaps you wanted to call repliesToComments()
for a different set of comments when recursing? I suspect you meant to call it for comment.replies
instead?
Another problem your code has is that you ignore the return value of the recursive call:
botComments + repliesToComments(comments, author)
This adds the list botComments
and the return value of the recursive call, creating a new list, which you then drop to the floor by not assigning it to a variable. You probably want to use:
botComments += repliesToComments(comments, author)
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.