如何在以下python代码中包含此附加用例

时间:2014-12-29 04:24:44

标签: python

我有以下文字。

我需要编写一个python代码来查找" link"然后得到之后的所有值,之后我会按照上面的例子得到。 然后我需要从括号中取出整数值,并检查它是否大于零,然后返回结果dict,告诉我应用了哪些优化。 现在另外一个用例是文本还包含一些额外的关键字。

理想情况下,我希望它可以应用多个正则表达式并将应用/非应用放在我的结果字典中。怎么做。

1 个答案:

答案 0 :(得分:0)

您不必编辑process_feo_debug_output,只需向CHECKS添加一些代码即可。试试这个:

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, 'Not Applied') 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] = ('Not Applied','Applied')[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