使用一个功能的输出作为另一个功能的输入

问题描述:

所以我想检查一个网站来更新我,只要有新的项目发布。他们不经常更新,所以我相当确定他们什么时候更新它将成为感兴趣的项目。我想通过选择一个“起始数字”并计算页面上的链接数量,然后每10分钟将该数字与链接数量进行比较,直到链接数量大于起始数字为止。使用一个功能的输出作为另一个功能的输入

首先,我跑这来获得链接的“起始编号”:

links=[] 
for link in soup.findAll('a'): 
    links.append(link.get('href')) 
start_num = len(links) 

那么现在这个数字比较链接的数量和每5秒:

notify=True 
while notify: 
    try: 
     page = urllib.request.urlopen('web/site/url') 
     soup = bs(page, "lxml") 

     links=[] 
     for link in soup.findAll('a'): 
      links.append(link.get('href')) 

     if len(links) > start_num: 
      message = client.messages.create(to="", from_="",body="") 
      print('notified') 
      notify=False 
     else: 
      print('keep going') 
      time.sleep(60*5) 

    except: 
     print("Going to sleep") 
     time.sleep(60*10) 

哪有我把所有这一切合并为一个函数,我运行的时候可以存储链接的起始数量,而不用每次检查链接数量时都覆盖它。

+0

如果你想保留一个函数的状态,你应该考虑使用类。 –

你可以做到这一点至少在两个方面:装饰和发电机

装饰:

def hang_on(func): 

    # soup should be in a visible scope 
    def count_links(): 
     # refresh page? 
     return len(soup.findAll('a')) 

    start_num = count_links() 

    def wrapper(*args, **kwargs): 
     while True: 
      try: 
       new_links = count_links() 
       if new_links > start_num: 
        start_num = new_links 
        return fund(*args, **kwargs) 
       print('keep going') 
       time.sleep(60*5)    
      except: 
       print("Going to sleep") 
       time.sleep(60*10)   

    return wrapper 

@hang_on  
def notify(): 
    message = client.messages.create(to="", from_="",body="") 
    print('notified') 

# somewhere in your code, simply: 
notify() 

发电机:

def gen_example(soup): 

    # initialize soup (perhaps from url) 

    # soup should be in a visible scope 
    def count_links(): 
     # refresh page? 
     return len(soup.findAll('a')) 

    start_num = count_links() 

    while True: 
     try: 
      new_links = count_links() 
      if new_links > start_num: 
       start_num = new_links 
       message = client.messages.create(to="", from_="",body="") 
       print('notified') 
       yield True # this is what makes this func a generator 

      print('keep going') 
      time.sleep(60*5)    
     except: 
      print("Going to sleep") 
      time.sleep(60*10)  

# somewhere in your code: 
gen = gen_example(soup) # initialize 

gen.next() # will wait and notify 

# coming soon 

我会实现它作为一类,因为这个代码是可读性强,易于支持。享受:

class Notifier: 
    url = 'web/site/url' 
    timeout = 60 * 10 

    def __links_count(self): 
     page = urllib.request.urlopen(self.url) 
     soup = bs(page, "lxml") 

     links=[] 
     for link in soup.findAll('a'): 
      links.append(link.get('href')) 

     return len(links) 

    def __notify(self): 
     client.messages.create(to="", from_="", body="") 
     print('notified') 

    def run(self): 
     current_count = self.__links_count() 

     while True: 
      try: 
       new_count = self.__links_count() 

       if new_count > current_count: 
        self.__notify() 
        break 

       sleep(self.timeout) 

      except: 
       print('Keep going') 
       sleep(self.timeout) 

notifier = Norifier() 
notifier.run() 
+0

所以自我在函数中使用的参数(self.url,self.timeout)总是引用未在类中定义的任何方法中定义的变量? – e1v1s