Skip to content

Commit

Permalink
Merge pull request #62 from zopefoundation/code-cleaup-keys
Browse files Browse the repository at this point in the history
Code cleanup: do not call dict.keys
  • Loading branch information
dataflake authored Jan 12, 2019
2 parents 627fc10 + 0dd062b commit d135c1e
Show file tree
Hide file tree
Showing 19 changed files with 49 additions and 48 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Products.CMFCore Changelog
2.4.0b7 (unreleased)
--------------------

- Nothing changed yet.
- Code cleanup: avoid to call the ``keys`` method on dictionary
when not needed
[ale-rt]


2.4.0b6 (2018-12-14)
Expand Down
2 changes: 1 addition & 1 deletion Products/CMFCore/CMFCatalogAware.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def opaqueItems(self):
# Loop over all attributes and add those to 'items'
# implementing 'ICallableOpaqueItem'.
self_base = aq_base(self)
for name in self_base.__dict__.keys():
for name in self_base.__dict__:
obj = getattr(self, name)
if ICallableOpaqueItem.providedBy(obj):
items.append((obj.getId(), obj))
Expand Down
5 changes: 2 additions & 3 deletions Products/CMFCore/CatalogTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def allowedRolesAndUsers(self):
allowed['user:' + user] = 1
if 'Owner' in allowed:
del allowed['Owner']
return list(allowed.keys())
return list(allowed)

def cmf_uid(self):
"""
Expand Down Expand Up @@ -362,8 +362,7 @@ def _reindexObject(self, object, idxs=[], update_metadata=1, uid=None):
uid = self.__url(object)
if idxs != []:
# Filter out invalid indexes.
valid_indexes = self._catalog.indexes.keys()
idxs = [i for i in idxs if i in valid_indexes]
idxs = [i for i in idxs if i in self._catalog.indexes]
self.catalog_object(object, uid, idxs, update_metadata)


Expand Down
4 changes: 2 additions & 2 deletions Products/CMFCore/DirectoryView.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def prepareContents(self, registry):
# FS-based security
permissions = metadata.getSecurity()
if permissions is not None:
for name in permissions.keys():
for name in permissions:
acquire, roles = permissions[name]
try:
ob.manage_permission(name, roles, acquire)
Expand Down Expand Up @@ -347,7 +347,7 @@ def getDirectoryInfo(self, reg_key):
return self._directories.get(reg_key, None)

def listDirectories(self):
dirs = sorted(self._directories.keys())
dirs = sorted(self._directories)
return dirs


Expand Down
10 changes: 5 additions & 5 deletions Products/CMFCore/MemberDataTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def getMemberDataContents(self):
mtool = getUtility(IMembershipTool)
members = self._members
user_list = mtool.listMemberIds()
member_list = members.keys()
member_count = len(members)
member_list = list(members)
member_count = len(member_list)
orphan_count = 0

for member in member_list:
Expand All @@ -130,7 +130,7 @@ def searchMemberData(self, search_param, search_term, attributes=()):
if search_param == 'username':
search_param = 'id'

for user_id in self._members.keys():
for user_id in self._members:
u = mtool.getMemberById(user_id)

if u is not None:
Expand Down Expand Up @@ -160,7 +160,7 @@ def searchMemberDataContents(self, search_param, search_term):

mtool = getUtility(IMembershipTool)

for member_id in self._members.keys():
for member_id in self._members:

user_wrapper = mtool.getMemberById(member_id)

Expand All @@ -182,7 +182,7 @@ def pruneMemberDataContents(self):
members = self._members
user_list = mtool.listMemberIds()

for member_id in list(members.keys()):
for member_id in list(members):
if member_id not in user_list:
del members[member_id]

Expand Down
2 changes: 1 addition & 1 deletion Products/CMFCore/MembershipTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def wrapUser(self, u, wrap_anon=0):

# Apply any role mapping if we have it
if hasattr(self, 'role_map'):
for portal_role in self.role_map.keys():
for portal_role in self.role_map:
if (self.role_map.get(portal_role) in u.roles and
portal_role not in u.roles):
u.roles.append(portal_role)
Expand Down
4 changes: 2 additions & 2 deletions Products/CMFCore/SkinsTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def manage_skinLayers(self, chosen=(), add_skin=0, del_skin=0,
del sels[name]

if REQUEST is not None:
for key in sels.keys():
for key in sels:
fname = 'skinpath_%s' % key
val = REQUEST[fname]

Expand Down Expand Up @@ -291,7 +291,7 @@ def getSkinSelections(self):
""" Get the sorted list of available skin names.
"""
sels = self._getSelections()
rval = sorted(sels.keys())
rval = sorted(sels)
return rval

@security.protected(View)
Expand Down
6 changes: 3 additions & 3 deletions Products/CMFCore/TypesTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ def listContentTypes(self, container=None, by_metatype=0):
if name:
typenames[name] = None

result = sorted(typenames.keys())
result = sorted(typenames)
return result

@security.public
Expand Down Expand Up @@ -844,9 +844,9 @@ def listMethodAliasKeys(self):
_dict = {}
for ti in self.listTypeInfo():
aliases = ti.getMethodAliases()
for k in aliases.keys():
for k in aliases:
_dict[k] = 1
rval = sorted(_dict.keys())
rval = sorted(_dict)
return rval


Expand Down
2 changes: 1 addition & 1 deletion Products/CMFCore/WorkflowTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ def _reindexWorkflowVariables(self, ob):
if hasattr(aq_base(ob), 'reindexObject'):
# XXX We only need the keys here, no need to compute values.
mapping = self.getCatalogVariablesFor(ob) or {}
vars = mapping.keys()
vars = list(mapping)
ob.reindexObject(idxs=vars)

# Reindex security of subobjects.
Expand Down
22 changes: 11 additions & 11 deletions Products/CMFCore/tests/test_CachingPolicyManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def test_viewPredicateMiss(self):

def test_kwPredicate(self):
policy = self._makePolicy('kwPredicate',
predicate='python:"foo" in keywords.keys()')
predicate='python:"foo" in keywords')
context = self._makeContext(foo=1)
headers = policy.getHeaders(context)

Expand All @@ -181,7 +181,7 @@ def test_kwPredicate(self):

def test_kwPredicateMiss(self):
policy = self._makePolicy('kwPredicateMiss',
predicate='python:"foo" in keywords.keys()')
predicate='python:"foo" in keywords')
context = self._makeContext(bar=1)
headers = policy.getHeaders(context)

Expand Down Expand Up @@ -479,7 +479,7 @@ def test_reorder(self):

for policy_id in policy_ids:
mgr._addPolicy(policy_id,
'python:"%s" in keywords.keys()' % policy_id,
'python:"%s" in keywords' % policy_id,
None, 0, 0, 0, 0, '', '')

ids = tuple([x[0] for x in mgr.listPolicies()])
Expand All @@ -500,7 +500,7 @@ def _makeOneWithPolicies(self):

for policy_id, max_age_secs in policy_tuples:
mgr._addPolicy(policy_id,
'python:"%s" in keywords.keys()' % policy_id,
'python:"%s" in keywords' % policy_id,
None, max_age_secs, 0, 0, 0, '', '')

return mgr
Expand Down Expand Up @@ -894,7 +894,7 @@ def test_subtemplate_cpm_1(self):
portal.content()

# no headers should be added by the CPM if all is well
headers = [x.lower() for x in self.RESPONSE.headers.keys()]
headers = {x.lower() for x in self.RESPONSE.headers}
self.assertFalse('x-cache-headers-set-by' in headers)
self.assertFalse('vary' in headers)

Expand Down Expand Up @@ -935,7 +935,7 @@ def test_subtemplate_cpm_2(self):
portal.content()

# no headers should be added by the CPM if all is well
headers = [x.lower() for x in self.RESPONSE.headers.keys()]
headers = {x.lower() for x in self.RESPONSE.headers}
self.assertFalse('x-cache-headers-set-by' in headers)
self.assertFalse('vary' in headers)

Expand Down Expand Up @@ -980,7 +980,7 @@ def test_subtemplate_cpm_3(self):
portal.doc1()

# no headers should be added by the CPM if all is well
headers = [x.lower() for x in self.RESPONSE.headers.keys()]
headers = {x.lower() for x in self.RESPONSE.headers}
self.assertFalse('x-cache-headers-set-by' in headers)
self.assertFalse('vary' in headers)

Expand Down Expand Up @@ -1025,7 +1025,7 @@ def test_mixed_subtemplate_cpm(self):
portal.doc1()

# no headers should be added by the CPM if all is well
headers = [x.lower() for x in self.RESPONSE.headers.keys()]
headers = {x.lower() for x in self.RESPONSE.headers}
self.assertFalse('x-cache-headers-set-by' in headers)
self.assertFalse('vary' in headers)

Expand Down Expand Up @@ -1087,7 +1087,7 @@ def test_fireForSubtemplates(self):
self.portal.doc1.dummy_view()

# no headers should be added by the CPM if all is well
headers = [x.lower() for x in self.RESPONSE.headers.keys()]
headers = {x.lower() for x in self.RESPONSE.headers}
self.assertFalse('x-cache-headers-set-by' in headers)
self.assertFalse('vary' in headers)

Expand Down Expand Up @@ -1238,7 +1238,7 @@ def test_no_association(self):
self.portal.doc1()

# no headers should be added by the CPM if all is well
headers = [x.lower() for x in self.RESPONSE.headers.keys()]
headers = {x.lower() for x in self.RESPONSE.headers}
self.assertFalse('x-cache-headers-set-by' in headers)
self.assertFalse('vary' in headers)

Expand All @@ -1252,7 +1252,7 @@ def test_unsuitable_association(self):
doc2()

# no headers should be added by the CPM if all is well
headers = [x.lower() for x in self.RESPONSE.headers.keys()]
headers = {x.lower() for x in self.RESPONSE.headers}
self.assertFalse('x-cache-headers-set-by' in headers)
self.assertFalse('vary' in headers)

Expand Down
2 changes: 1 addition & 1 deletion Products/CMFCore/tests/test_DirectoryView.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def tearDown(self):
# This is nasty, but there is no way to unregister anything
# right now...
metatype_registry = DirectoryView._dirreg._meta_types
if 'FOLDER' in metatype_registry.keys():
if 'FOLDER' in metatype_registry:
del metatype_registry['FOLDER']
FSDVTest.tearDown(self)

Expand Down
4 changes: 2 additions & 2 deletions Products/CMFCore/tests/test_FSDTMLMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def test_caching(self):
obj = obj.__of__(self.app)
obj(self.app, self.REQUEST, self.RESPONSE)
self.assertTrue(len(self.RESPONSE.headers) >= original_len + 2)
self.assertTrue('foo' in self.RESPONSE.headers.keys())
self.assertTrue('bar' in self.RESPONSE.headers.keys())
self.assertTrue('foo' in self.RESPONSE.headers)
self.assertTrue('bar' in self.RESPONSE.headers)

def test_ownership(self):
script = self._makeOne('testDTML', 'testDTML.dtml')
Expand Down
4 changes: 2 additions & 2 deletions Products/CMFCore/tests/test_FSFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ def test_caching(self):
obj.index_html(self.REQUEST, self.RESPONSE)
headers = self.RESPONSE.headers
self.assertTrue(len(headers) >= original_len + 3)
self.assertTrue('foo' in headers.keys())
self.assertTrue('bar' in headers.keys())
self.assertTrue('foo' in headers)
self.assertTrue('bar' in headers)
self.assertEqual(headers['test_path'], '/test_file')

def test_forced_content_type(self):
Expand Down
8 changes: 4 additions & 4 deletions Products/CMFCore/tests/test_FSImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ def test_caching(self):
obj.index_html(self.REQUEST, self.RESPONSE)
headers = self.RESPONSE.headers
self.assertTrue(len(headers) >= original_len + 3)
self.assertTrue('foo' in headers.keys())
self.assertTrue('bar' in headers.keys())
self.assertTrue('foo' in headers)
self.assertTrue('bar' in headers)
self.assertEqual(headers['test_path'], '/test_image')

def test_index_html_with_304_and_caching(self):
Expand All @@ -190,8 +190,8 @@ def test_index_html_with_304_and_caching(self):

headers = self.RESPONSE.headers
self.assertTrue(len(headers) >= original_len + 3)
self.assertTrue('foo' in headers.keys())
self.assertTrue('bar' in headers.keys())
self.assertTrue('foo' in headers)
self.assertTrue('bar' in headers)
self.assertEqual(headers['test_path'], '/test_image')

def test_tag_with_acquired_clashing_attrs(self):
Expand Down
4 changes: 2 additions & 2 deletions Products/CMFCore/tests/test_FSPageTemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def test_caching(self):
obj = obj.__of__(self.app)
obj()
self.assertTrue(len(self.RESPONSE.headers) >= original_len + 2)
self.assertTrue('foo' in self.RESPONSE.headers.keys())
self.assertTrue('bar' in self.RESPONSE.headers.keys())
self.assertTrue('foo' in self.RESPONSE.headers)
self.assertTrue('bar' in self.RESPONSE.headers)

def test_pt_properties(self):
script = self._makeOne('testPT', 'testPT.pt')
Expand Down
4 changes: 2 additions & 2 deletions Products/CMFCore/tests/test_FSReSTMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def test_caching(self):
obj = obj.__of__(self.app)
obj(self.REQUEST, self.RESPONSE)
self.assertTrue(len(self.RESPONSE.headers) >= original_len + 2)
self.assertTrue('foo' in self.RESPONSE.headers.keys())
self.assertTrue('bar' in self.RESPONSE.headers.keys())
self.assertTrue('foo' in self.RESPONSE.headers)
self.assertTrue('bar' in self.RESPONSE.headers)

def test_ownership(self):
script = self._makeOne('testReST', 'testReST.rst')
Expand Down
4 changes: 2 additions & 2 deletions Products/CMFCore/tests/test_FSSTXMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ def test_caching(self):
obj = obj.__of__(self.app)
obj(self.REQUEST, self.RESPONSE)
self.assertTrue(len(self.RESPONSE.headers) >= original_len + 2)
self.assertTrue('foo' in self.RESPONSE.headers.keys())
self.assertTrue('bar' in self.RESPONSE.headers.keys())
self.assertTrue('foo' in self.RESPONSE.headers)
self.assertTrue('bar' in self.RESPONSE.headers)

def test_ownership(self):
script = self._makeOne('testSTX', 'testSTX.stx')
Expand Down
4 changes: 2 additions & 2 deletions Products/CMFCore/tests/test_WorkflowTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def test_content_own_chain(self):

vars = tool.getCatalogVariablesFor(dummy)
self.assertEqual(len(vars), 1)
self.assertTrue('dummy' in vars.keys())
self.assertTrue('dummy' in vars)
self.assertTrue('a: dummy' in vars.values())

def test_setChainForPortalTypes(self):
Expand Down Expand Up @@ -308,7 +308,7 @@ def test_getCatalogVariablesFor(self):

vars = tool.getCatalogVariablesFor(dummy)
self.assertEqual(len(vars), 1)
self.assertTrue('dummy' in vars.keys())
self.assertTrue('dummy' in vars)
self.assertTrue('a: dummy' in vars.values())

def test_getInfoFor(self):
Expand Down
2 changes: 1 addition & 1 deletion Products/CMFCore/zcml.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def registerDirectory(_context, name, directory=None, recursive=False,
def cleanUp():
global _directory_regs
for reg_key in _directory_regs:
for key in list(_dirreg._directories.keys()):
for key in list(_dirreg._directories):
if key.startswith(reg_key):
del _dirreg._directories[key]
_directory_regs = []
Expand Down

0 comments on commit d135c1e

Please sign in to comment.