Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e0d4972

Browse files
committed
Replaced .keys() with dictionary iterators
1 parent 1fab9ee commit e0d4972

9 files changed

Lines changed: 46 additions & 50 deletions

File tree

Lib/copy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ def _deepcopy_tuple(x, memo):
234234
def _deepcopy_dict(x, memo):
235235
y = {}
236236
memo[id(x)] = y
237-
for key in x.keys():
238-
y[deepcopy(key, memo)] = deepcopy(x[key], memo)
237+
for key, value in x.iteritems():
238+
y[deepcopy(key, memo)] = deepcopy(value, memo)
239239
return y
240240
d[types.DictionaryType] = _deepcopy_dict
241241
if PyStringMap is not None:
@@ -335,8 +335,8 @@ def __init__(self, arg=None):
335335
def __getstate__(self):
336336
return {'a': self.a, 'arg': self.arg}
337337
def __setstate__(self, state):
338-
for key in state.keys():
339-
setattr(self, key, state[key])
338+
for key, value in state.iteritems():
339+
setattr(self, key, value)
340340
def __deepcopy__(self, memo = None):
341341
new = self.__class__(deepcopy(self.arg, memo))
342342
new.a = self.a

Lib/filecmp.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ def phase4(self): # Find out differences between common subdirectories
228228

229229
def phase4_closure(self): # Recursively call phase4() on subdirectories
230230
self.phase4()
231-
for x in self.subdirs.keys():
232-
self.subdirs[x].phase4_closure()
231+
for sd in self.subdirs.itervalues():
232+
sd.phase4_closure()
233233

234234
def report(self): # Print a report on the differences between a and b
235235
# Output format is purposely lousy
@@ -258,15 +258,15 @@ def report(self): # Print a report on the differences between a and b
258258

259259
def report_partial_closure(self): # Print reports on self and on subdirs
260260
self.report()
261-
for x in self.subdirs.keys():
261+
for sd in self.subdirs.itervalues():
262262
print
263-
self.subdirs[x].report()
263+
sd.report()
264264

265265
def report_full_closure(self): # Report on self and subdirs recursively
266266
self.report()
267-
for x in self.subdirs.keys():
267+
for sd in self.subdirs.itervalues():
268268
print
269-
self.subdirs[x].report_full_closure()
269+
sd.report_full_closure()
270270

271271

272272
def cmpfiles(a, b, common, shallow=1, use_statcache=0):

Lib/inspect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def getclasstree(classes, unique=0):
553553
if unique and parent in classes: break
554554
elif c not in roots:
555555
roots.append(c)
556-
for parent in children.keys():
556+
for parent in children:
557557
if parent not in classes:
558558
roots.append(parent)
559559
return walktree(roots, children, None)

Lib/mailcap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ def getcaps():
2424
continue
2525
morecaps = readmailcapfile(fp)
2626
fp.close()
27-
for key in morecaps.keys():
27+
for key, value in morecaps.iteritems():
2828
if not key in caps:
29-
caps[key] = morecaps[key]
29+
caps[key] = value
3030
else:
31-
caps[key] = caps[key] + morecaps[key]
31+
caps[key] = caps[key] + value
3232
return caps
3333

3434
def listmailcapfiles():

Lib/profile.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,11 @@ def create_stats(self):
386386

387387
def snapshot_stats(self):
388388
self.stats = {}
389-
for func in self.timings.keys():
390-
cc, ns, tt, ct, callers = self.timings[func]
389+
for func, (cc, ns, tt, ct, callers) in self.timings.iteritems():
391390
callers = callers.copy()
392391
nc = 0
393-
for func_caller in callers.keys():
394-
nc = nc + callers[func_caller]
392+
for callcnt in callers.itervalues():
393+
nc += callcnt
395394
self.stats[func] = cc, nc, tt, ct, callers
396395

397396

Lib/pstats.py

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,20 @@ def add(self, *arg_list):
142142
self.total_calls += other.total_calls
143143
self.prim_calls += other.prim_calls
144144
self.total_tt += other.total_tt
145-
for func in other.top_level.keys():
145+
for func in other.top_level:
146146
self.top_level[func] = None
147147

148148
if self.max_name_len < other.max_name_len:
149149
self.max_name_len = other.max_name_len
150150

151151
self.fcn_list = None
152152

153-
for func in other.stats.keys():
153+
for func, stat in other.stats.iteritems():
154154
if func in self.stats:
155155
old_func_stat = self.stats[func]
156156
else:
157157
old_func_stat = (0, 0, 0, 0, {},)
158-
self.stats[func] = add_func_stats(old_func_stat, other.stats[func])
158+
self.stats[func] = add_func_stats(old_func_stat, stat)
159159
return self
160160

161161
# list the tuple indices and directions for sorting,
@@ -178,17 +178,17 @@ def get_sort_arg_defs(self):
178178
if not self.sort_arg_dict:
179179
self.sort_arg_dict = dict = {}
180180
bad_list = {}
181-
for word in self.sort_arg_dict_default.keys():
181+
for word, tup in self.sort_arg_dict_default.iteritems():
182182
fragment = word
183183
while fragment:
184184
if not fragment:
185185
break
186186
if fragment in dict:
187187
bad_list[fragment] = 0
188188
break
189-
dict[fragment] = self.sort_arg_dict_default[word]
189+
dict[fragment] = tup
190190
fragment = fragment[:-1]
191-
for word in bad_list.keys():
191+
for word in bad_list:
192192
del dict[word]
193193
return self.sort_arg_dict
194194

@@ -213,8 +213,7 @@ def sort_stats(self, *field):
213213
connector = ", "
214214

215215
stats_list = []
216-
for func in self.stats.keys():
217-
cc, nc, tt, ct, callers = self.stats[func]
216+
for func, (cc, nc, tt, ct, callers) in self.stats.iteritems():
218217
stats_list.append((cc, nc, tt, ct) + func +
219218
(func_std_string(func), func))
220219

@@ -234,14 +233,13 @@ def strip_dirs(self):
234233
oldstats = self.stats
235234
self.stats = newstats = {}
236235
max_name_len = 0
237-
for func in oldstats.keys():
238-
cc, nc, tt, ct, callers = oldstats[func]
236+
for func, (cc, nc, tt, ct, callers) in oldstats.iteritems():
239237
newfunc = func_strip_path(func)
240238
if len(func_std_string(newfunc)) > max_name_len:
241239
max_name_len = len(func_std_string(newfunc))
242240
newcallers = {}
243-
for func2 in callers.keys():
244-
newcallers[func_strip_path(func2)] = callers[func2]
241+
for func2, caller in callers.iteritems():
242+
newcallers[func_strip_path(func2)] = caller
245243

246244
if newfunc in newstats:
247245
newstats[newfunc] = add_func_stats(
@@ -251,7 +249,7 @@ def strip_dirs(self):
251249
newstats[newfunc] = (cc, nc, tt, ct, newcallers)
252250
old_top = self.top_level
253251
self.top_level = new_top = {}
254-
for func in old_top.keys():
252+
for func in old_top:
255253
new_top[func_strip_path(func)] = None
256254

257255
self.max_name_len = max_name_len
@@ -263,14 +261,13 @@ def strip_dirs(self):
263261
def calc_callees(self):
264262
if self.all_callees: return
265263
self.all_callees = all_callees = {}
266-
for func in self.stats.keys():
264+
for func, (cc, nc, tt, ct, callers) in self.stats.iteritems():
267265
if not func in all_callees:
268266
all_callees[func] = {}
269-
cc, nc, tt, ct, callers = self.stats[func]
270-
for func2 in callers.keys():
267+
for func2, caller in callers.iteritems():
271268
if not func2 in all_callees:
272269
all_callees[func2] = {}
273-
all_callees[func2][func] = callers[func2]
270+
all_callees[func2][func] = caller
274271
return
275272

276273
#******************************************************************
@@ -330,7 +327,7 @@ def print_stats(self, *amount):
330327
print filename
331328
if self.files: print
332329
indent = ' ' * 8
333-
for func in self.top_level.keys():
330+
for func in self.top_level:
334331
print indent, func_get_function_name(func)
335332

336333
print indent, self.total_calls, "function calls",
@@ -468,20 +465,20 @@ def add_func_stats(target, source):
468465
def add_callers(target, source):
469466
"""Combine two caller lists in a single list."""
470467
new_callers = {}
471-
for func in target.keys():
472-
new_callers[func] = target[func]
473-
for func in source.keys():
468+
for func, caller in target.iteritems():
469+
new_callers[func] = caller
470+
for func, caller in source.iteritems():
474471
if func in new_callers:
475-
new_callers[func] = source[func] + new_callers[func]
472+
new_callers[func] = caller + new_callers[func]
476473
else:
477-
new_callers[func] = source[func]
474+
new_callers[func] = caller
478475
return new_callers
479476

480477
def count_calls(callers):
481478
"""Sum the caller statistics to get total number of calls received."""
482479
nc = 0
483-
for func in callers.keys():
484-
nc += callers[func]
480+
for calls in callers.itervalues():
481+
nc += calls
485482
return nc
486483

487484
#**************************************************************************
@@ -595,19 +592,19 @@ def help_reverse(self):
595592
print "Reverse the sort order of the profiling report."
596593

597594
def do_sort(self, line):
598-
abbrevs = self.stats.get_sort_arg_defs().keys()
595+
abbrevs = self.stats.get_sort_arg_defs()
599596
if line and not filter(lambda x,a=abbrevs: x not in a,line.split()):
600597
apply(self.stats.sort_stats, line.split())
601598
else:
602599
print "Valid sort keys (unique prefixes are accepted):"
603-
for (key, value) in Stats.sort_arg_dict_default.items():
600+
for (key, value) in Stats.sort_arg_dict_default.iteritems():
604601
print "%s -- %s" % (key, value[1])
605602
return 0
606603
def help_sort(self):
607604
print "Sort profile data according to specified keys."
608605
print "(Typing `sort' without arguments lists valid keys.)"
609606
def complete_sort(self, text, *args):
610-
return [a for a in Stats.sort_arg_dict_default.keys() if a.startswith(text)]
607+
return [a for a in Stats.sort_arg_dict_default if a.startswith(text)]
611608

612609
def do_stats(self, line):
613610
return self.generic('print_stats', line)

Lib/pyclbr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def readmodule_ex(module, path=[], inpackage=0):
324324
# Python does internally)
325325
# also don't add names that
326326
# start with _
327-
for n in d.keys():
327+
for n in d:
328328
if n[0] != '_' and \
329329
not n in dict:
330330
dict[n] = d[n]

Lib/rlcompleter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def global_matches(self, text):
104104
matches = []
105105
n = len(text)
106106
for list in [keyword.kwlist,
107-
__builtin__.__dict__.keys(),
108-
self.namespace.keys()]:
107+
__builtin__.__dict__,
108+
self.namespace]:
109109
for word in list:
110110
if word[:n] == text and word != "__builtins__":
111111
matches.append(word)

Lib/symtable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def get_methods(self):
163163
d = {}
164164
for st in self._table.children:
165165
d[st.name] = 1
166-
self.__methods = tuple(d.keys())
166+
self.__methods = tuple(d)
167167
return self.__methods
168168

169169
class Symbol:

0 commit comments

Comments
 (0)