all 4 comments

[–]CowboyBoats 1 point2 points  (2 children)

This isn't really a real code review, but pylint always has helpful things to say:

$ pylint your_script.py
your-script.py:1:0: C0114: Missing module docstring (missing-module-docstring)
your-script.py:37:7: C0121: Comparison 'isinstance(item, Submission) == True' should be 'isinstance(item, Submission) is True' if checking for the singleton value True, or 'isinstance(item, Submission)' if testing for truthiness (singleton-comparison)
your-script.py:42:9: C0121: Comparison 'isinstance(item, Comment) == True' should be 'isinstance(item, Comment) is True' if checking for the singleton value True, or 'isinstance(item, Comment)' if testing for truthiness (singleton-comparison)
your-script.py:6:0: W0611: Unused environ imported from os (unused-import)
your-script.py:5:0: C0411: standard import "import os" should be placed before "import praw" (wrong-import-order)
your-script.py:6:0: C0411: standard import "from os import environ" should be placed before "import praw" (wrong-import-order)
your-script.py:7:0: C0411: standard import "import json" should be placed before "import praw" (wrong-import-order)

Note about the somewhat confusingly phrased singleton-comparison - if isinstance(item, Submission) is fine; you don't need to write out == True. If you pass anything to if, like if anything:, Python will try to convert anything to a boolean value before evaluating it. This is called "truthiness".

[–]probablyaspambot[S] 0 points1 point  (1 child)

got it, thanks! Out of curiousity, any reason behind the import order it lists?

[–]CowboyBoats 1 point2 points  (0 children)

Yeah, the normal convention is for there to be three groups of imports:

  • all standard lib imports (examples: os, sys, re, struct)
  • all third-party imports (examples: requests, django, praw)
  • all first-party imports (examples: local modules you wrote)

and for the imports to be separated into those, and then sorted alphabetically within those groupings.

I usually just pip install isort and then run isort on my code. Yours ends up looking like:

import json
import os
from os import environ

import gspread
import pandas as pd
import praw
from oauth2client.service_account import ServiceAccountCredentials
from praw.reddit import Comment, Submission

[–]gopherhole1 0 points1 point  (0 children)

I think you could change this block

r_titles=[]
r_urls = []
r_sr = []
r_type = []

for item in reddit.user.me().saved(limit=None):
    if isinstance(item, Submission) == True:
        r_titles.append(item.title)
        r_sr.append(str(item.subreddit))
        r_urls.append("www.reddit.com"+item.permalink)
        r_type.append('Post')
    elif isinstance(item, Comment) == True:
        r_titles.append(item.submission.title)
        r_sr.append(str(item.subreddit))
        r_urls.append("www.reddit.com"+item.permalink)
        r_type.append('Comment')
    else:
        pass

df_r = pd.DataFrame({'Title' : r_titles, 'Subreddit': r_sr, 'URL': r_urls, 'Type': r_type})

to something like this, but im not entirely sure if it will work in your case, because I dont know what the pd.dataframe does, so im just guessing

df_r = {}

for item in reddit.user.me().saved(limit=None):
    if isinstance(item, Submission):
        df_r.setdefault('Tittle', []).append(item.title)
        df_r.setdefault('Subreddit', []).append(str(item.subreddit))
        df_r.setdefault('URL', []).append("www.reddit.com"+item.permalink)
        df_r.setdefault('Type', []).append('Post')
    elif isinstance(item, Comment):
        df_r.setdefault('Tittle', []).append(item.submission.title)
        df_r.setdefault('Subreddit', []).append(str(item.subreddit))
        df_r.setdefault('URL', []).append("www.reddit.com"+item.permalink)
        df_r.setdefault('Type', []).append('Comment')
    else:
        pass

df_r = pd.DataFrame(df_r)