Search code examples
pythonclassooppraw

Python class: The data is somehow lost


I am trying to define a Python class which analyzes subreddit data via the praw package.

I am fairly experienced with OOP in C++, but have not had much experience with OOP in Python. Here is the code I have so far:

import praw
class SubRedditAnalyzer:

    def __init__(self, reddit_session, name='dataisbeautiful'):
        self.name = name # subreddit name
        self.reddit_session = reddit_session # assign the reddit session
        self.subreddit = self.reddit_session.get_subreddit(self.name) # create the subreddit object
        self.timeframe = 'day'
        self.max_post_count = 10
        self.submissions = self.subreddit.get_top_from_hour(limit=10)

    def __del__(self):
        class_name = self.__class__.__name__
        print class_name, "destroyed"

    def get_top_submissions(self, max_post_count):

        timeframe = self.timeframe

        if (timeframe == 'hour'):
            self.submissions = self.subreddit.get_top_from_hour(limit= max_post_count)
        elif (timeframe == 'day'):
            self.submissions = self.subreddit.get_top_from_day(limit= max_post_count)
        elif (timeframe == 'week'):
            self.submissions = self.subreddit.get_top_from_week(limit= max_post_count)
        elif (timeframe == 'month'):
            self.submissions = self.subreddit.get_top_from_month(limit= max_post_count)
        elif (timeframe == 'year'):
            self.submissions = self.subreddit.get_top_from_year(limit= max_post_count)
        elif (timeframe == 'all'):
            self.submissions = self.subreddit.get_top_from_all(limit= max_post_count)

    def combine_titles(self):
        titles = ""
        for submission in self.submissions:
            titles += submission.title
        self.titles = titles 

    def display_titles(self):
        counter = 1
        ya = self.submissions
        for sub in self.submissions:
            sc = sub.score
            ti = sub.title
            print('T%d- [%d] %s \n' %(counter,sc,ti))
            counter += 1

def main():
   r = praw.Reddit('Request to fetch data by user')
   sr = SubRedditAnalyzer(r, 'dataisbeautiful')
   sr.get_top_submissions(15) # top 15 from reddit
   sr.combine_titles()        # combine the top titles    
   sr.display_titles()        # display all the titles 

main()

For some unknown (to me) reason, it seems that the data in class 'sr' is lost after calling:

sr.combine_titles()

When I try to call this method, the data in class is empty:

sr.display_titles()

In fact, I do see the message that the class is destroyed:

SubRedditAnalyzer destroyed

What is it that I am doing wrong? In advance, thanks for your attention.


Solution

  • It seems that self.submissions may be an iterable but not a collection (e.g. a list). The docs call get_top_from_hour() a generator method (although they state also that what is returned is a list...). If it is indeed a generator method, the result can be iterated over only once. All other attempts at iteration will fail silently (the loop in display_titles() executes nothing).

    So, the solution would be:

    self.submissions = list(self.subreddit.get_top_from_hour(limit=10))
    

    in __init__() to convert an iterable into a permanent collection (list) that can be iterated over multiple times.