Replace six.iteritems() with .items()
1.As mentioned in [1], we should avoid using six.iteritems to achieve iterators. We can use dict.items instead, as it will return iterators in PY3 as well. And dict.items/keys will more readable. 2.In py2, the performance about list should be negligible, see the link [2]. [1] https://wiki.openstack.org/wiki/Python3 [2] http://lists.openstack.org/pipermail/openstack-dev/2015-June/066391.html Change-Id: I5340fa3d71b6fba76e8fcf75f9f30432329023d2
This commit is contained in:
parent
ba5735c801
commit
693f57faae
|
@ -19,7 +19,6 @@ import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import six
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from bandit.core import extension_loader
|
from bandit.core import extension_loader
|
||||||
|
@ -158,8 +157,8 @@ def main():
|
||||||
test_list = [tpl.format(t.plugin._test_id, t.name)
|
test_list = [tpl.format(t.plugin._test_id, t.name)
|
||||||
for t in extension_loader.MANAGER.plugins]
|
for t in extension_loader.MANAGER.plugins]
|
||||||
|
|
||||||
others = [tpl.format(k, v['name']) for k, v in six.iteritems(
|
others = [tpl.format(k, v['name']) for k, v in (
|
||||||
extension_loader.MANAGER.blacklist_by_id)]
|
extension_loader.MANAGER.blacklist_by_id.items())]
|
||||||
test_list.extend(others)
|
test_list.extend(others)
|
||||||
test_list.sort()
|
test_list.sort()
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import six
|
|
||||||
|
|
||||||
import bandit
|
import bandit
|
||||||
from bandit.core import config as b_config
|
from bandit.core import config as b_config
|
||||||
|
@ -247,9 +246,9 @@ def main():
|
||||||
parser.set_defaults(ignore_nosec=False)
|
parser.set_defaults(ignore_nosec=False)
|
||||||
|
|
||||||
plugin_info = ["%s\t%s" % (a[0], a[1].name) for a in
|
plugin_info = ["%s\t%s" % (a[0], a[1].name) for a in
|
||||||
six.iteritems(extension_mgr.plugins_by_id)]
|
extension_mgr.plugins_by_id.items()]
|
||||||
blacklist_info = []
|
blacklist_info = []
|
||||||
for a in six.iteritems(extension_mgr.blacklist):
|
for a in extension_mgr.blacklist.items():
|
||||||
for b in a[1]:
|
for b in a[1]:
|
||||||
blacklist_info.append('%s\t%s' % (b['id'], b['name']))
|
blacklist_info.append('%s\t%s' % (b['id'], b['name']))
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import six
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from bandit.core import constants
|
from bandit.core import constants
|
||||||
|
@ -132,7 +131,7 @@ class BanditConfig(object):
|
||||||
extman = extension_loader.MANAGER
|
extman = extension_loader.MANAGER
|
||||||
|
|
||||||
updated_profiles = {}
|
updated_profiles = {}
|
||||||
for name, profile in six.iteritems(self.get_option('profiles') or {}):
|
for name, profile in (self.get_option('profiles') or {}).items():
|
||||||
# NOTE(tkelsey): can't use default of get() because value is
|
# NOTE(tkelsey): can't use default of get() because value is
|
||||||
# sometimes explicity 'None', for example when the list if given in
|
# sometimes explicity 'None', for example when the list if given in
|
||||||
# yaml but not populated with any values.
|
# yaml but not populated with any values.
|
||||||
|
@ -151,7 +150,7 @@ class BanditConfig(object):
|
||||||
bad_calls = self.get_option('blacklist_calls') or {}
|
bad_calls = self.get_option('blacklist_calls') or {}
|
||||||
bad_calls = bad_calls.get('bad_name_sets', {})
|
bad_calls = bad_calls.get('bad_name_sets', {})
|
||||||
for item in bad_calls:
|
for item in bad_calls:
|
||||||
for key, val in six.iteritems(item):
|
for key, val in item.items():
|
||||||
val['name'] = key
|
val['name'] = key
|
||||||
val['message'] = val['message'].replace('{func}', '{name}')
|
val['message'] = val['message'].replace('{func}', '{name}')
|
||||||
bad_calls_list.append(val)
|
bad_calls_list.append(val)
|
||||||
|
@ -159,7 +158,7 @@ class BanditConfig(object):
|
||||||
bad_imports = self.get_option('blacklist_imports') or {}
|
bad_imports = self.get_option('blacklist_imports') or {}
|
||||||
bad_imports = bad_imports.get('bad_import_sets', {})
|
bad_imports = bad_imports.get('bad_import_sets', {})
|
||||||
for item in bad_imports:
|
for item in bad_imports:
|
||||||
for key, val in six.iteritems(item):
|
for key, val in item.items():
|
||||||
val['name'] = key
|
val['name'] = key
|
||||||
val['message'] = val['message'].replace('{module}', '{name}')
|
val['message'] = val['message'].replace('{module}', '{name}')
|
||||||
val['qualnames'] = val['imports']
|
val['qualnames'] = val['imports']
|
||||||
|
@ -179,7 +178,7 @@ class BanditConfig(object):
|
||||||
data.remove(name)
|
data.remove(name)
|
||||||
data.add('B001')
|
data.add('B001')
|
||||||
|
|
||||||
for name, profile in six.iteritems(profiles):
|
for name, profile in profiles.items():
|
||||||
blacklist = {}
|
blacklist = {}
|
||||||
include = profile['include']
|
include = profile['include']
|
||||||
exclude = profile['exclude']
|
exclude = profile['exclude']
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
import importlib
|
import importlib
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import six
|
|
||||||
|
|
||||||
from bandit.core import blacklisting
|
from bandit.core import blacklisting
|
||||||
from bandit.core import extension_loader
|
from bandit.core import extension_loader
|
||||||
|
@ -46,7 +45,7 @@ class BanditTestSet(object):
|
||||||
exc = set(profile.get('exclude', []))
|
exc = set(profile.get('exclude', []))
|
||||||
|
|
||||||
all_blacklist_tests = set()
|
all_blacklist_tests = set()
|
||||||
for _node, tests in six.iteritems(extman.blacklist):
|
for _node, tests in extman.blacklist.items():
|
||||||
all_blacklist_tests.update(t['id'] for t in tests)
|
all_blacklist_tests.update(t['id'] for t in tests)
|
||||||
|
|
||||||
# this block is purely for backwards compatibility, the rules are as
|
# this block is purely for backwards compatibility, the rules are as
|
||||||
|
@ -83,7 +82,7 @@ class BanditTestSet(object):
|
||||||
blacklist = profile.get('blacklist')
|
blacklist = profile.get('blacklist')
|
||||||
if not blacklist: # not overridden by legacy data
|
if not blacklist: # not overridden by legacy data
|
||||||
blacklist = {}
|
blacklist = {}
|
||||||
for node, tests in six.iteritems(extman.blacklist):
|
for node, tests in extman.blacklist.items():
|
||||||
values = [t for t in tests if t['id'] in filtering]
|
values = [t for t in tests if t['id'] in filtering]
|
||||||
if values:
|
if values:
|
||||||
blacklist[node] = values
|
blacklist[node] = values
|
||||||
|
|
|
@ -16,7 +16,6 @@ import collections
|
||||||
import tempfile
|
import tempfile
|
||||||
from xml.etree import cElementTree as ET
|
from xml.etree import cElementTree as ET
|
||||||
|
|
||||||
import six
|
|
||||||
import testtools
|
import testtools
|
||||||
|
|
||||||
import bandit
|
import bandit
|
||||||
|
@ -54,12 +53,12 @@ class XmlFormatterTests(testtools.TestCase):
|
||||||
if children:
|
if children:
|
||||||
dd = collections.defaultdict(list)
|
dd = collections.defaultdict(list)
|
||||||
for dc in map(self._xml_to_dict, children):
|
for dc in map(self._xml_to_dict, children):
|
||||||
for k, v in six.iteritems(dc):
|
for k, v in dc.items():
|
||||||
dd[k].append(v)
|
dd[k].append(v)
|
||||||
d = {t.tag: {k: v[0] if len(v) == 1 else v
|
d = {t.tag: {k: v[0] if len(v) == 1 else v
|
||||||
for k, v in six.iteritems(dd)}}
|
for k, v in dd.items()}}
|
||||||
if t.attrib:
|
if t.attrib:
|
||||||
d[t.tag].update(('@' + k, v) for k, v in six.iteritems(t.attrib))
|
d[t.tag].update(('@' + k, v) for k, v in t.attrib.items())
|
||||||
if t.text:
|
if t.text:
|
||||||
text = t.text.strip()
|
text = t.text.strip()
|
||||||
if children or t.attrib:
|
if children or t.attrib:
|
||||||
|
|
Loading…
Reference in New Issue