<pre><code>for type_key, type in TYPES.iteritems():
for sub_type_key, sub_type in type.iteritems():
for sig in sub_type['signatures']:
</code></pre>
<p>应该是:</p>
<pre><code>for type_key, type in TYPES.iteritems():
for sig in type['signatures']:
</code></pre>
<p>但在这种情况下,“type”是一个糟糕的名字选择。。。你不想给一个建筑蒙上阴影。</p>
<p>从本质上说,“type_key”具有名称(hotmail或gmail),而“type”具有与该键关联的值dictionary。所以你想要的就是输入“签名”。</p>
<p>此外,您可能不需要在嵌套字典中包含“gmail”;只需返回“type_key”,而不是<code>type['type']</code>。</p>
<p>把这一切结合起来,也许会更好:(警告:未经测试)</p>
<pre><code>providers = {
'hotmail':{
'type':'hotmail',
'lookup':'mixed',
'dkim': 'no',
'signatures':[
'|S|Return-Path: postmaster@hotmail.com',
'|R|^Return-Path:\s*[^@]+@(?:hot|msn)',
'^Received: from .*hotmail.com$']
},
'gmail':{
'type':'gmail',
'lookup':'mixed',
'dkim': 'yes',
'signatures':['|S|Subject: unsubscribe','','','']
}
}
for provider, provider_info in providers.iteritems():
for sig in provicer_info['signatures']:
if ("|S|" in sig):
#String based matching
clean_sig = sig[3:len(sig)]
if (clean_sig in file_contents):
sig_match += 1
elif ("|R|" in sig):
clean_sig = sig[3:len(sig)]
#REGMATCH later
if (sig_match == sig.count):
return provider
return None
</code></pre>