我应该如何重新思考这里的对象/对象方向?

发布于 2024-12-02 03:38:34 字数 1678 浏览 1 评论 0原文

我(主要是想学习 python 和 json,但也)尝试定期从 Twitter 上提取和格式化热门主题列表。我浏览了很多不同的教程,将其拼凑在一起。它满足了我的目的——将我需要的 HTML 打印到标准输出,但我想知道我是否可以以不同的方式思考对象或更好地构建它。协助?

class trend:
        #these are the fields that Twitter provides, so they make up one trend.
        def __init__(self, query, name, promoted_content, events, url):
                self.query = query
                self.name = name
                self.promoted_content = promoted_content
                self.events = events
                self.url = url 

        def listitem(self):
                print "\t <li><a href=\"%s\">%s</a></li>\n" %(self.url, self.name)

class trending:
        def __init__(self,api_url,title):
                self.api_url = api_url
                self.title = title

        def get_trending(self):
                import simplejson as json
                import urllib2

                trends_all = json.loads(urllib2.urlopen(self.api_url).read())
                # test print
                # print trends_all[0]['trends']
                print "<p>%s</p> \n <ol>" % self.title

                #I'm initializing an array, though I don't actually use it. That's next.
                trends = []
                for x in trends_all[0]['trends']:
                    thistrend = trend(x['query'], x['name'], x['promoted_content'], x['events'], x['url'])
                    thistrend.listitem()
                    trends.append(thistrend) 
                print "</ol>\n"
                return trends

usa = trending("http://api.twitter.com/1/trends/23424977.json","Trending nationally")
usa.get_trending()

反馈?

I'm (mostly trying to learn python and json, but also) trying to periodically pull and format a list of trending topics off of twitter. I cobbled this together skimming a lot of different tutorials. It serves my purposes -- prints the HTML I need to stdout, but I'm wondering if I could have thought about objects differently or structured it better. Assist?

class trend:
        #these are the fields that Twitter provides, so they make up one trend.
        def __init__(self, query, name, promoted_content, events, url):
                self.query = query
                self.name = name
                self.promoted_content = promoted_content
                self.events = events
                self.url = url 

        def listitem(self):
                print "\t <li><a href=\"%s\">%s</a></li>\n" %(self.url, self.name)

class trending:
        def __init__(self,api_url,title):
                self.api_url = api_url
                self.title = title

        def get_trending(self):
                import simplejson as json
                import urllib2

                trends_all = json.loads(urllib2.urlopen(self.api_url).read())
                # test print
                # print trends_all[0]['trends']
                print "<p>%s</p> \n <ol>" % self.title

                #I'm initializing an array, though I don't actually use it. That's next.
                trends = []
                for x in trends_all[0]['trends']:
                    thistrend = trend(x['query'], x['name'], x['promoted_content'], x['events'], x['url'])
                    thistrend.listitem()
                    trends.append(thistrend) 
                print "</ol>\n"
                return trends

usa = trending("http://api.twitter.com/1/trends/23424977.json","Trending nationally")
usa.get_trending()

feedback?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁年轻赶紧闹 2024-12-09 03:38:34

在你的例子中,我不明白为什么趋势需要成为一个类,因为它只有一个功能。这可以使用 get_trending 作为独立函数编写,该函数将 api_url 和标题作为参数。

In your example, I don't see why trending needs to be a clas at all, since it only has one function. This could be written with get_trending as a standalone function, which takes the api_url and title as arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文