Skip to content

Commit 99dfb10

Browse files
committed
Python 3 preparation: update calls to dict methods.
Tool-assisted patch. Changes of iterkeys / itervalues / iteritems to keys / values / items are fully automated, but may make things less efficient in Python 2. Automated tools want to add list() around many calls to keys / values / items, but I thought most such list() additions were unnecessary because it seemed the result of keys / values / items was just iterated over while the set of dict keys remained unchanged, rather than used in a way requiring an actual list, or used while the set of keys in the dict could change. It's quite possible I missed some cases where list() was really needed, or left in some unnecessary list() calls. In cases where list() was only needed because the resulting list was then sorted in-place, I changed the code to use calls to sorted().
1 parent a6b85b4 commit 99dfb10

26 files changed

+100
-123
lines changed

detectors/newitemcopy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ def generateCreateNote(db, cl, nodeid):
1515

1616
# list the values
1717
m = []
18-
prop_items = props.items()
19-
prop_items.sort()
18+
prop_items = sorted(props.items())
2019
for propname, prop in prop_items:
2120
value = cl.get(nodeid, propname, None)
2221
# skip boring entries

frontends/ZRoundup/ZRoundup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def getvalue(self, key, default=None):
102102
def has_key(self, item):
103103
return item in self.__form
104104
def keys(self):
105-
return self.__form.keys()
105+
return list(self.__form.keys())
106106

107107
def __repr__(self):
108108
return '<ZRoundup.FormWrapper %r>'%self.__form

roundup/backends/back_anydbm.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,7 @@ def addclass(self, cl):
261261

262262
def getclasses(self):
263263
"""Return a list of the names of all existing classes."""
264-
l = self.classes.keys()
265-
l.sort()
266-
return l
264+
return sorted(self.classes.keys())
267265

268266
def getclass(self, classname):
269267
"""Get the Class object representing a particular class.
@@ -484,7 +482,7 @@ def serialise(self, classname, node):
484482
"""
485483
properties = self.getclass(classname).getprops()
486484
d = {}
487-
for k, v in node.iteritems():
485+
for k, v in node.items():
488486
if k == self.RETIRED_FLAG:
489487
d[k] = v
490488
continue
@@ -511,7 +509,7 @@ def unserialise(self, classname, node):
511509
"""
512510
properties = self.getclass(classname).getprops()
513511
d = {}
514-
for k, v in node.iteritems():
512+
for k, v in node.items():
515513
# if the property doesn't exist, or is the "retired" flag then
516514
# it won't be in the properties dict
517515
if k not in properties:
@@ -600,7 +598,7 @@ def setjournal(self, classname, nodeid, journal):
600598
def fix_journal(self, classname, journal):
601599
""" fix password entries to correct type """
602600
pwprops = {}
603-
for pn, prop in self.getclass(classname).properties.iteritems():
601+
for pn, prop in self.getclass(classname).properties.items():
604602
if isinstance(prop, hyperdb.Password):
605603
pwprops [pn] = 1
606604
if not pwprops:
@@ -724,7 +722,7 @@ def commit(self):
724722
reindex[method(*args)] = 1
725723
finally:
726724
# make sure we close all the database files
727-
for db in self.databases.itervalues():
725+
for db in self.databases.values():
728726
db.close()
729727
del self.databases
730728

@@ -916,7 +914,7 @@ def create_inner(self, **propvalues):
916914

917915
# validate propvalues
918916
num_re = re.compile('^\d+$')
919-
for key, value in propvalues.iteritems():
917+
for key, value in propvalues.items():
920918
if key == self.key:
921919
try:
922920
self.lookup(value)
@@ -1027,7 +1025,7 @@ def create_inner(self, **propvalues):
10271025
raise TypeError('new property "%s" not boolean'%key)
10281026

10291027
# make sure there's data where there needs to be
1030-
for key, prop in self.properties.iteritems():
1028+
for key, prop in self.properties.items():
10311029
if key in propvalues:
10321030
continue
10331031
if key == self.key:
@@ -1168,7 +1166,7 @@ class or a KeyError is raised.
11681166

11691167
self.fireAuditors('set', nodeid, propvalues)
11701168
oldvalues = copy.deepcopy(self.db.getnode(self.classname, nodeid))
1171-
for name, prop in self.getprops(protected=0).iteritems():
1169+
for name, prop in self.getprops(protected=0).items():
11721170
if name in oldvalues:
11731171
continue
11741172
if isinstance(prop, hyperdb.Multilink):
@@ -1551,7 +1549,7 @@ def find(self, **propspec):
15511549
db.issue.find(messages='1')
15521550
db.issue.find(messages={'1':1,'3':1}, files={'7':1})
15531551
"""
1554-
for propname, itemids in propspec.iteritems():
1552+
for propname, itemids in propspec.items():
15551553
# check the prop is OK
15561554
prop = self.properties[propname]
15571555
if not isinstance(prop, hyperdb.Link) and not isinstance(prop, hyperdb.Multilink):
@@ -1566,7 +1564,7 @@ def find(self, **propspec):
15661564
item = self.db.getnode(self.classname, id, db=cldb)
15671565
if self.db.RETIRED_FLAG in item:
15681566
continue
1569-
for propname, itemids in propspec.iteritems():
1567+
for propname, itemids in propspec.items():
15701568
if type(itemids) is not type({}):
15711569
itemids = {itemids:1}
15721570

@@ -1616,7 +1614,7 @@ def stringFind(self, **requirements):
16161614
node = self.db.getnode(self.classname, nodeid, cldb)
16171615
if self.db.RETIRED_FLAG in node:
16181616
continue
1619-
for key, value in requirements.iteritems():
1617+
for key, value in requirements.items():
16201618
if key not in node:
16211619
break
16221620
if node[key] is None or node[key].lower() != value:
@@ -1720,7 +1718,7 @@ def _filter(self, search_matches, filterspec, proptree,
17201718
INTERVAL = 'spec:interval'
17211719
OTHER = 'spec:other'
17221720

1723-
for k, v in filterspec.iteritems():
1721+
for k, v in filterspec.items():
17241722
propclass = props[k]
17251723
if isinstance(propclass, hyperdb.Link):
17261724
if type(v) is not type([]):
@@ -2001,7 +1999,7 @@ def addprop(self, **properties):
20011999
def index(self, nodeid):
20022000
""" Add (or refresh) the node to search indexes """
20032001
# find all the String properties that have indexme
2004-
for prop, propclass in self.getprops().iteritems():
2002+
for prop, propclass in self.getprops().items():
20052003
if isinstance(propclass, hyperdb.String) and propclass.indexme:
20062004
# index them under (classname, nodeid, property)
20072005
try:
@@ -2107,7 +2105,7 @@ def export_journals(self):
21072105
date = date.get_tuple()
21082106
if action == 'set':
21092107
export_data = {}
2110-
for propname, value in params.iteritems():
2108+
for propname, value in params.items():
21112109
if propname not in properties:
21122110
# property no longer in the schema
21132111
continue
@@ -2201,7 +2199,7 @@ def set(self, itemid, **propvalues):
22012199

22022200
# create the oldvalues dict - fill in any missing values
22032201
oldvalues = copy.deepcopy(self.db.getnode(self.classname, itemid))
2204-
for name, prop in self.getprops(protected=0).iteritems():
2202+
for name, prop in self.getprops(protected=0).items():
22052203
if name in oldvalues:
22062204
continue
22072205
if isinstance(prop, hyperdb.Multilink):
@@ -2238,7 +2236,7 @@ def index(self, nodeid):
22382236
Use the content-type property for the content property.
22392237
"""
22402238
# find all the String properties that have indexme
2241-
for prop, propclass in self.getprops().iteritems():
2239+
for prop, propclass in self.getprops().items():
22422240
if prop == 'content' and propclass.indexme:
22432241
mime_type = self.get(nodeid, 'type', self.default_mime_type)
22442242
self.db.indexer.add_text((self.classname, nodeid, 'content'),

roundup/backends/indexer_common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def search(self, search_terms, klass, ignore={}):
4040
return {}
4141

4242
designator_propname = {}
43-
for nm, propclass in klass.getprops().iteritems():
43+
for nm, propclass in klass.getprops().items():
4444
if _isLink(propclass):
4545
designator_propname.setdefault(propclass.classname,
4646
[]).append(nm)
@@ -49,7 +49,7 @@ def search(self, search_terms, klass, ignore={}):
4949
# and files
5050
nodeids = {} # this is the answer
5151
propspec = {} # used to do the klass.find
52-
for l in designator_propname.itervalues():
52+
for l in designator_propname.values():
5353
for propname in l:
5454
propspec[propname] = {} # used as a set (value doesn't matter)
5555

roundup/backends/indexer_dbm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def load_index(self, reload=0, wordlist=None):
204204
dbslice = marshal.loads(pickle_str)
205205
if dbslice.get('WORDS'):
206206
# if it has some words, add them
207-
for word, entry in dbslice['WORDS'].iteritems():
207+
for word, entry in dbslice['WORDS'].items():
208208
db['WORDS'][word] = entry
209209
if dbslice.get('FILES'):
210210
# if it has some files, add them
@@ -240,7 +240,7 @@ def save_index(self):
240240
segdicts = {} # Need batch of empty dicts
241241
for segment in letters:
242242
segdicts[segment] = {}
243-
for word, entry in self.words.iteritems(): # Split into segment dicts
243+
for word, entry in self.words.items(): # Split into segment dicts
244244
initchar = word[0].upper()
245245
segdicts[initchar][word] = entry
246246

@@ -269,7 +269,7 @@ def purge_entry(self, identifier):
269269
del self.fileids[file_index]
270270

271271
# The much harder part, cleanup the word index
272-
for key, occurs in self.words.iteritems():
272+
for key, occurs in self.words.items():
273273
if file_index in occurs:
274274
del occurs[file_index]
275275

0 commit comments

Comments
 (0)