Python类:数据以某种方式丢失

时间:2016-11-04 08:45:41

标签: python class oop praw

我正在尝试定义一个Python类,它通过praw包分析subreddit数据。

我对C ++中的OOP很有经验,但对Python中的OOP没有多少经验。这是我到目前为止的代码:

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()

对于某些未知(对我而言)的原因,似乎调用后类'sr'中的数据丢失了:

sr.combine_titles()

当我尝试调用此方法时,类中的数据为空:

sr.display_titles()

事实上,我确实看到了类被销毁的消息:

SubRedditAnalyzer销毁

我做错了什么? 提前,谢谢你的关注。

2 个答案:

答案 0 :(得分:2)

self.submissions似乎可以是可迭代但不是集合(例如列表)。 The docs调用get_top_from_hour() 生成器方法(尽管他们还说明返回的是列表...)。如果它确实是generator method,则结果只能迭代一次。所有其他迭代尝试都将无声地失败(display_titles()中的循环不会执行任何操作)。

所以,解决方案是:

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

__init__()中将迭代转换为可以多次迭代的永久集合(列表)。

答案 1 :(得分:2)

根据PRAW文档,get_content及其相关方法(如get_top_from_hour)会返回生成器。生成器只能迭代一次,这可以在combine_titles中进行。在该迭代之后,生成器耗尽并且不能再次迭代。

您可以将提交内容转换为列表,当您将其提交到__init__时:

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