Probabilistic-programming-and-bayesian-methods-for-hackers: `praw` configuration or version issue

Created on 20 Mar 2017  路  4Comments  路  Source: CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers

There appears to be a compatibility issue with praw or an undocumented config file that needs to get setup. When executing this cell:

#adding a number to the end of the %run call with get the ith top post.
%run top_showerthoughts_submissions.py 2

print("Post contents: \n")
print(top_post)

It gives this error.

KeyError                                  Traceback (most recent call last)
/opt/conda/lib/python3.6/configparser.py in items(self, section, raw, vars)
    842         try:
--> 843             d.update(self._sections[section])
    844         except KeyError:

KeyError: 'BayesianMethodsForHackers'

During handling of the above exception, another exception occurred:

NoSectionError                            Traceback (most recent call last)
/opt/notebooks/Chapter4_TheGreatestTheoremNeverTold/top_showerthoughts_submissions.py in <module>()
      7 
      8 
----> 9 reddit = praw.Reddit("BayesianMethodsForHackers")
     10 subreddit  = reddit.get_subreddit("showerthoughts")
     11 

/opt/conda/lib/python3.6/site-packages/praw/reddit.py in __init__(self, site_name, **config_settings)
    101         self._unique_counter = 0
    102         self.config = Config(site_name or os.getenv('praw_site') or 'DEFAULT',
--> 103                              **config_settings)
    104 
    105         required_message = ('Required configuration setting {!r} missing. \n'

/opt/conda/lib/python3.6/site-packages/praw/config.py in __init__(self, site_name, **settings)
     64 
     65         self._settings = settings
---> 66         self.custom = dict(Config.CONFIG.items(site_name), **settings)
     67 
     68         self.client_id = self.client_secret = self.oauth_url = None

/opt/conda/lib/python3.6/configparser.py in items(self, section, raw, vars)
    844         except KeyError:
    845             if section != self.default_section:
--> 846                 raise NoSectionError(section)
    847         # Update with the entry specific variables
    848         if vars:

NoSectionError: No section: 'BayesianMethodsForHackers'

Post contents: 

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-be2145cbdc50> in <module>()
      3 
      4 print("Post contents: \n")
----> 5 print(top_post)

NameError: name 'top_post' is not defined

I'm running version praw==4.4.0

Most helpful comment

Yep, same here. Looks like the new PRAW needs more authentication. Mine ran fine when I added this:

reddit = praw.Reddit(client_id=[my id],
                     client_secret=[my secret],
                     user_agent=[a random string explaining my 'app'],
                     username=[my reddit username],
                     password=[my reddit password)

In addition to more authentication being needed, the PRAW devs changed some of their method names and moved some stuff around (namely, you can access upvote_ratio in the submission object now):

for sub in top_submissions:
    try:
        ratio = sub.upvote_ratio
        ups = int(round((ratio*sub.score)/(2*ratio - 1)) if ratio != 0.5 else round(sub.score/2))
        upvotes.append(sub.ups)
        downvotes.append(ups - sub.score)
        contents.append(sub.title)
    except Exception as e:
        raise e

All 4 comments

I had the same issue. It appears the praw API has changed significantly as of v4. I was able to get the code working by installing praw 3.6.1, but it would be good if the code could be updated to work on the more recent versions.

Yep, same here. Looks like the new PRAW needs more authentication. Mine ran fine when I added this:

reddit = praw.Reddit(client_id=[my id],
                     client_secret=[my secret],
                     user_agent=[a random string explaining my 'app'],
                     username=[my reddit username],
                     password=[my reddit password)

In addition to more authentication being needed, the PRAW devs changed some of their method names and moved some stuff around (namely, you can access upvote_ratio in the submission object now):

for sub in top_submissions:
    try:
        ratio = sub.upvote_ratio
        ups = int(round((ratio*sub.score)/(2*ratio - 1)) if ratio != 0.5 else round(sub.score/2))
        upvotes.append(sub.ups)
        downvotes.append(ups - sub.score)
        contents.append(sub.title)
    except Exception as e:
        raise e

With PRAW 6 I had to make a couple of tweaks to get this to work. Thanks a lot to previous commenters for clues.

reddit = praw.Reddit(
    client_id=client_id,
    client_secret=client_secret,
    user_agent=user_agent,
    username=username,
    password=password,
)
subreddit = reddit.subreddit("showerthoughts")

# go by timespan - 'hour', 'day', 'week', 'month', 'year', 'all'
# might need to go longer than an hour to get entries...
top_submissions = subreddit.top('hour')

n_sub = int(sys.argv[1]) if len(sys.argv) > 1 else 1

i = 0
while i < n_sub:
    top_submission = next(top_submissions)
    i += 1

top_post = top_submission.title

upvotes = []
downvotes = []
contents = []

for sub in top_submissions:
    try:
        ratio = sub.upvote_ratio
        ups = int(round((ratio*sub.score)/(2*ratio - 1))
                  if ratio != 0.5 else round(sub.score/2))
        upvotes.append(ups)
        downvotes.append(ups - sub.score)
        contents.append(sub.title)
    except Exception as e:
        continue

I should have mentioned that the limit arg works in top(), e.g.

top_submissions = subreddit.top('hour', limit=5)
top_submissions = subreddit.top('all', limit=20)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Anjum48 picture Anjum48  路  8Comments

cledoux picture cledoux  路  13Comments

cledoux picture cledoux  路  13Comments

fgypas picture fgypas  路  6Comments

SamLau95 picture SamLau95  路  22Comments