我在Python中有以下类。
class FEOProcessor(object):
def __init__(self):
self.parser = Extractor()
self.result = {'Standard JavaScript Inlining Optimization' :
'Not Applied',
'HTML5 Advanced Cache' : 'Not Applied',
'Cookieless Resource Domain' : 'Not Applied',
'Minificatiopn of JS' : 'Not Applied',
'File Versioning' : 'Not Applied',
'Small Image Embedding' : 'Not Applied',
'Responsive Image Loading' : 'Not Applied',
'Asynchronous JS and CSS Loading' : 'Not Applied',
'JS Pre-Execution' : 'Not Applied'
}
def check_js_inlining(self, result):
if 'EMBED_JAVASCRIPT' in result:
return result['EMBED_JAVASCRIPT'] > 0
def check_html5_advanced_cache(self, result):
if 'JAVASCRIPT_HTML5_CACHE' in result:
return result['JAVASCRIPT_HTML5_CACHE'] > 0 and result['CSS_HTML5_CACHE'] > 0
def check_cookieless_resource_domain(self,result):
if 'RENAME_JAVASCRIPT' in result and 'RENAME_CSS' in result:
return result['RENAME_JAVASCRIPT'] > 0 and result['RENAME_CSS'] > 0
def check_js_minifaction(self, result):
if 'MINIFY_JAVASCRIPT' in result:
return result['MINIFY_JAVASCRIPT'] > 0
def check_file_versioning(self, result):
if 'RENAME_JAVASCRIPT' in result and 'RENAME_IMAGE' in result and 'RENAME_CSS' in result:
return result['RENAME_JAVASCRIPT'] > 0 and result['RENAME_IMAGE'] > 0 and result['RENAME_CSS'] > 0
def check_small_image_embedding(self, result):
if 'EMBED_IMAGE' in result:
return result['EMBED_IMAGE'] > 0
def check_responsive_image_loading(self, result):
if 'RESPONSIVE_IMAGES' in result:
return result['RESPONSIVE_IMAGES'] > 0
def check_async_js_and_css_loading(self, result):
if 'ASYNC_JAVASCRIPT' in result:
return result['ASYNC_JAVASCRIPT'] > 0
def check_js_pre_execution(self, result):
if 'PRE_EXECUTE_JAVASCRIPT' in result:
return result['PRE_EXECUTE_JAVASCRIPT'] > 0
def process_feo_debug_output(self, analysis_id, url):
feed = self.parser.start_parser(analysis_id, url, True)
result = self.get_feo_tags(feed)
if self.check_js_inlining(result):
self.result['Standard JavaScript Inlining Optimization'] = 'Applied'
if self.check_html5_advanced_cache(result):
self.result['HTML5 Advanced Cache'] = 'Applied'
if self.check_cookieless_resource_domain(result):
self.result['Cookieless Resource Domain'] = 'Applied'
if self.check_js_minifaction(result):
self.result['Minificatiopn of JS'] = 'Applied'
if self.check_file_versioning(result):
self.result['File Versioning'] = 'Applied'
if self.check_small_image_embedding(result):
self.result['Small Image Embedding'] = 'Applied'
if self.check_responsive_image_loading(result):
self.result['Responsive Image Loading'] = 'Applied'
if self.check_async_js_and_css_loading(result):
self.result['Asynchronous JS and CSS Loading'] = 'Applied'
if self.check_js_pre_execution(result):
self.result['JS Pre-Execution'] = 'Applied'
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
processor = FEOProcessor()
print processor.process_feo_debug_output('123d','http://in.strawberrynet.com/main.aspx')
最初结果都没有应用dict。现在根据我获得的结果更新dict。这可以用更好的方式编写。
答案 0 :(得分:0)
因此,您可以采取以下措施来避免使用那么多if
语句,创建一个字典,其中所有函数名称为key
,而self.result
字典keys
作为values
:
class FEOProcessor(object):
def __init__(self):
self.parser = Extractor()
self.result = {
'Standard JavaScript Inlining Optimization': 'Not Applied',
'HTML5 Advanced Cache' : 'Not Applied',
'Cookieless Resource Domain' : 'Not Applied',
'Minificatiopn of JS' : 'Not Applied',
'File Versioning' : 'Not Applied',
'Small Image Embedding' : 'Not Applied',
'Responsive Image Loading' : 'Not Applied',
'Asynchronous JS and CSS Loading' : 'Not Applied',
'JS Pre-Execution' : 'Not Applied'
}
# All your functions...
def process_feo_debug_output(self, analysis_id, url):
feed = self.parser.start_parser(analysis_id, url, True)
result = self.get_feo_tags(feed)
func_dict = {
self.check_js_inlining: 'Standard JavaScript Inlining Optimization',
self.check_html5_advanced_cache: 'HTML5 Advanced Cache',
self.check_cookieless_resource_domain: 'Cookieless Resource Domain',
self.check_js_minifaction: 'Minificatiopn of JS',
self.check_file_versioning: 'File Versioning',
self.check_small_image_embedding: 'Small Image Embedding',
self.check_responsive_image_loading: 'Responsive Image Loading',
self.check_async_js_and_css_loading: 'Asynchronous JS and CSS Loading',
self.check_js_pre_execution: 'JS Pre-Execution'
}
for key, value in func_dict.iteritems():
if key(result):
self.result[value] = 'Applied'
答案 1 :(得分:0)
摆脱所有if
,你可以使用一个结构来定义你的支票:
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'),
]
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