简体   繁体   English

在 Python 中组合多个函数

[英]combine several functions in Python

I do RSS parsing and get news from news headlines我做RSS解析并从新闻标题中获取新闻

def print_headlines_test(rss_dict):
    for key,url in rss_dict.items():
        feed = feedparser.parse(url)

    headlines = []

    allheadlines = []

    for newsitem in feed['items']:
        headlines.append(newsitem['title'])

    for key,url in rss_dict.items():
        allheadlines.extend(headlines)

Then i'm saving this to csv and read df:然后我将其保存到 csv 并读取 df:

def write_and_read():
    header = ['Tittle' , 'Desc'] 

    with open('news.csv', 'w', encoding='utf-8-sig') as csvfile: 

        writer.writerow(i for i in header) 

    for a  in zip(allheadlines):
        writer.writerow((a))


    df = pd.read_csv('news.csv')

Then i'm searching news by certain targets (t & t2):然后我按某些目标搜索新闻(t & t2):

t = 'word1|word2|word3'
t2 = 'word3|word4|word5'

And making dataframe of this:并制作此数据框:

def certain_words(t, t2):
    result = df.apply(lambda x: x.str.contains(t, na=False,
                                    flags = re.IGNORECASE, regex=True)).any(axis=1)
    result2 = df.apply(lambda x: x.str.contains(t2, na=False,
                                    flags = re.IGNORECASE, regex=True)).any(axis=1)
    df[result&result2]

So, my input values is rss_dict (dictionary of rss with format {'rss-name':'rss-link'} and two targets ( t,t2 )所以,我的输入值是rss_dict (格式为{'rss-name':'rss-link'}的 rss 字典和两个目标( t,t2

Now my question.现在我的问题。 How i should combine all of this functions to something ( function or maybe class ) in order to set these three values (rss_dict, t, t2) and so that my code runs immediately?我应该如何将所有这些函数组合到某个东西( functionclass )以设置这三个值(rss_dict, t, t2)并让我的代码立即运行?

You can use a class with all these functions included inside as follows:您可以使用包含所有这些功能的类,如下所示:

class News:
    def __init__(self,rss_dict,t, t2):
        rss_dict=self.rss_dict
        t=self.t
        t2=self.t2

    def print_headlines_test(self):
        for key,url in self.rss_dict.items():
            feed = feedparser.parse(url)

        headlines = []

        allheadlines = []

        for newsitem in feed['items']:
            headlines.append(newsitem['title'])

        for key,url in self.rss_dict.items():
            allheadlines.extend(headlines)
        self.allheadlines=allheadlines

    def write_and_read(self):
        header = ['Tittle' , 'Desc'] 

        with open('news.csv', 'w', encoding='utf-8-sig') as csvfile: 

            writer.writerow(i for i in header) 

        for a  in zip(self.allheadlines):
            writer.writerow((a))


        df = pd.read_csv('news.csv')

    def certain_words(self):
        result = df.apply(lambda x: x.str.contains(self.t, na=False,
                                        flags = re.IGNORECASE, regex=True)).any(axis=1)
        result2 = df.apply(lambda x: x.str.contains(self.t2, na=False,
                                        flags = re.IGNORECASE, regex=True)).any(axis=1)
        df[result&result2] 

You should pass the parameters by creating an object of the class and the functions has to be called using the created object for them to run您应该通过创建类的对象来传递参数,并且必须使用创建的对象来调用函数才能运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM