2015-01-07 37 views
0

我有以下Python類。將另一個用例添加到python代碼

from body_parser import Extractor 
import re 

class FEOProcessor(object): 

    CHECKS = [ 
     ('Standard JavaScript Inlining Optimization', ('EMBED_JAVASCRIPT',), 'check_js_inlining'), 
     ('HTML5 Advanced Cache', ('JAVASCRIPT_HTML5_CACHE', 'CSS_HTML5_CACHE'), 'check_html5_advanced_cache'), 
     ('Cookieless Resource Domain', ('RENAME_JAVASCRIPT', 'RENAME_CSS'), 'check_cookieless_resource_domain'), 
     ('Minificatiopn of JS', ('MINIFY_JAVASCRIPT',), 'check_js_minifaction'), 
     ('File Versioning', ('RENAME_JAVASCRIPT', 'RENAME_IMAGE', 'RENAME_CSS'), 'check_file_versioning'), 
     ('Small Image Embedding', ('EMBED_IMAGE',), 'check_small_image_embedding'), 
     ('Responsive Image Loading', ('RESPONSIVE_IMAGES',), 'check_responsive_image_loading'), 
     ('Asynchronous JS and CSS Loading', ('ASYNC_JAVASCRIPT',), 'check_async_js_and_css_loading'), 
     ('JS Pre-Execution', ('PRE_EXECUTE_JAVASCRIPT',), 'check_js_pre_execution'), 
     ('EDGESTART', ('EDGESTART',), 'check_edgestart'), 
     ('Invoke Click OnTouch', ('BlzFastClick',), 'check_click'), 
     ('Cellular Connection Keep-Alive', ('blzEnableMobileHeartbeat',), 'check_cell'), 
    ] 

    def __init__(self): 
     self.parser = Extractor() 
     self.result = dict((k, False) for k,_,_ in self.CHECKS) 

    for _, keys, name in CHECKS: 
     locals()[name] = lambda self, result, _keys=keys: all(result.get(k, 0)>0 for k in _keys) 


    def process_feo_debug_output(self, analysis_id, url): 
     feed = self.parser.start_parser(analysis_id, url, True) 
     result = self.get_feo_tags(feed) 
     for name, _, func in self.CHECKS: 
      self.result[name] = (False, True)[getattr(self,func)(result)] 
     return self.result 

    def get_feo_tags(self, feed): 
     result = {} 
     tag_list = re.findall(r'(?:TextTransApplied):\s*((?:(?:[A-Z]+(?:_[A-Z\d]+)+)?\(\d+\)\s*(?:,\s*|;))*)', str(feed)) 
     for tag in tag_list: 
      for element in tag.split(","): 
       index = element.index('(') 
       if element[:index].strip(): 
        result[element[:index].strip()] = (element.split("(")[1].rstrip(");")) 
     return result 

    def check_edgestart(self, result): 
     return 1 if 'EDGESTART' in result.keys() else 0 

    def check_click(self, result): 
     return 1 if 'BlzFastClick' in result.keys() else 0 

    def check_cell(self, result): 
     return 1 if 'blzEnableMobileHeartbeat' in result.keys() else 0 

返回我的基礎上,檢查TrueFalse值的列表。我想要根據輸入到get_feo_tags方法的feed進行額外的檢查。

的另一個檢查需要被合併爲

for img in feed.find_all('img', attrs={'data-blzsrc': True, 'src': lambda x: 'data' not in x}): 
    #append to the result dict {On Demand Image Loading: True} 

如何在當前的設置做到這一點。

+0

這是什麼都與一個字典呢? – skzryzg

+0

抱歉編輯問題 – station

+0

什麼是湯?你想用IMG做什麼? –

回答

0

如果soup相同feed,你可以試試這個:

#!/usr/bin/env python 
#-*- coding:utf-8 -*- 

from body_parser import Extractor 
import re 

class FEOProcessor(object): 

    CHECKS = [ 
     ('Standard JavaScript Inlining Optimization', ('EMBED_JAVASCRIPT',), 'check_js_inlining'), 
     ('HTML5 Advanced Cache', ('JAVASCRIPT_HTML5_CACHE', 'CSS_HTML5_CACHE'), 'check_html5_advanced_cache'), 
     ('Cookieless Resource Domain', ('RENAME_JAVASCRIPT', 'RENAME_CSS'), 'check_cookieless_resource_domain'), 
     ('Minificatiopn of JS', ('MINIFY_JAVASCRIPT',), 'check_js_minifaction'), 
     ('File Versioning', ('RENAME_JAVASCRIPT', 'RENAME_IMAGE', 'RENAME_CSS'), 'check_file_versioning'), 
     ('Small Image Embedding', ('EMBED_IMAGE',), 'check_small_image_embedding'), 
     ('Responsive Image Loading', ('RESPONSIVE_IMAGES',), 'check_responsive_image_loading'), 
     ('Asynchronous JS and CSS Loading', ('ASYNC_JAVASCRIPT',), 'check_async_js_and_css_loading'), 
     ('JS Pre-Execution', ('PRE_EXECUTE_JAVASCRIPT',), 'check_js_pre_execution'), 
     ('EDGESTART', ('EDGESTART',), 'check_edgestart'), 
     ('Invoke Click OnTouch', ('BlzFastClick',), 'check_click'), 
     ('Cellular Connection Keep-Alive', ('blzEnableMobileHeartbeat',), 'check_cell'), 
     ('On Demand Image Loading', ('img',), 'check_img'), 
    ] 

    def __init__(self): 
     self.parser = Extractor() 
     self.result = dict((k, False) for k,_,_ in self.CHECKS) 

    for _, keys, name in CHECKS: 
     locals()[name] = lambda self, result, _keys=keys: all(result.get(k, 0)>0 for k in _keys) 


    def process_feo_debug_output(self, analysis_id, url): 
     feed = self.parser.start_parser(analysis_id, url, True) 
     # this works only if feed is the same as soup as you said 
     result = self.get_feo_tags(feed) 
     for name, _, func in self.CHECKS: 
      if name == 'On Demand Image Loading': 
       self.result[name] = (False, True)[getattr(self,func)(feed)] 
      else: 
       self.result[name] = (False, True)[getattr(self,func)(result)] 
     return self.result 

    def get_feo_tags(self, feed): 
     result = {} 
     tag_list = re.findall(r'(?:TextTransApplied):\s*((?:(?:[A-Z]+(?:_[A-Z\d]+)+)?\(\d+\)\s*(?:,\s*|;))*)', str(feed)) 
     for tag in tag_list: 
      for element in tag.split(","): 
       index = element.index('(') 
       if element[:index].strip(): 
        result[element[:index].strip()] = (element.split("(")[1].rstrip(");")) 
     return result 

    def check_edgestart(self, result): 
     return 1 if 'EDGESTART' in result.keys() else 0 

    def check_click(self, result): 
     return 1 if 'BlzFastClick' in result.keys() else 0 

    def check_cell(self, result): 
     return 1 if 'blzEnableMobileHeartbeat' in result.keys() else 0 

    def check_img(self, feed): 
     return 1 if feed.find_all('img', attrs={'data-blzsrc': True, 'src': lambda x: 'data' not in x}) else 0 
+0

這會拋出一個錯誤,提示''dict'對象沒有屬性'find_all'' – station

+0

@ user567797嗨,爲什麼取消接受答案? –