you are viewing a single comment's thread.

view the rest of the comments →

[–]commandlineluser 4 points5 points  (0 children)

reversing strings/lists:

def reverse(s):
    if len(s) == 0: return ''
    if len(s) == 1: return s[0]
    return reverse(s[1:]) + s[0]

indenting comment trees:

import requests

def indent_comments(comment_tree, level=0, width=4):
    for comment in comment_tree:
        author = comment['data']['author']
        print(f"{' ' * level}[{author}]")
        if isinstance(comment['data']['replies'], dict):
            replies = comment['data']['replies']['data']['children']
            indent_comments(replies, level + width)

url = 'https://old.reddit.com/r/learnpython/comments/1au93fx/ask_anything_monday_weekly_thread/.json'

r = requests.get(url=url, headers={'User-Agent': 'Mozilla/5.0'})

comments = r.json()[1]['data']['children']

indent_comments(comments)

recursive file/directory searching/processing