]> granicus.if.org Git - python/commitdiff
Replaced .keys() with dictionary iterators
authorRaymond Hettinger <python@rcn.com>
Sun, 2 Jun 2002 18:55:56 +0000 (18:55 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 2 Jun 2002 18:55:56 +0000 (18:55 +0000)
Lib/copy.py
Lib/filecmp.py
Lib/inspect.py
Lib/mailcap.py
Lib/profile.py
Lib/pstats.py
Lib/pyclbr.py
Lib/rlcompleter.py
Lib/symtable.py

index 21be6bc3b4fee60792085b42b17fbc9ddc244c2e..01805549ab3b948b455652d35b4646c948af2ab2 100644 (file)
@@ -234,8 +234,8 @@ d[types.TupleType] = _deepcopy_tuple
 def _deepcopy_dict(x, memo):
     y = {}
     memo[id(x)] = y
-    for key in x.keys():
-        y[deepcopy(key, memo)] = deepcopy(x[key], memo)
+    for key, value in x.iteritems():
+        y[deepcopy(key, memo)] = deepcopy(value, memo)
     return y
 d[types.DictionaryType] = _deepcopy_dict
 if PyStringMap is not None:
@@ -335,8 +335,8 @@ def _test():
         def __getstate__(self):
             return {'a': self.a, 'arg': self.arg}
         def __setstate__(self, state):
-            for key in state.keys():
-                setattr(self, key, state[key])
+            for key, value in state.iteritems():
+                setattr(self, key, value)
         def __deepcopy__(self, memo = None):
             new = self.__class__(deepcopy(self.arg, memo))
             new.a = self.a
index 9aee1b389bfa56d5a3ffa517c2ef5fcb254598bc..03c2ea3f5b1bcddb377adb6216ae3224d101c504 100644 (file)
@@ -228,8 +228,8 @@ class dircmp:
 
     def phase4_closure(self): # Recursively call phase4() on subdirectories
         self.phase4()
-        for x in self.subdirs.keys():
-            self.subdirs[x].phase4_closure()
+        for sd in self.subdirs.itervalues():
+            sd.phase4_closure()
 
     def report(self): # Print a report on the differences between a and b
         # Output format is purposely lousy
@@ -258,15 +258,15 @@ class dircmp:
 
     def report_partial_closure(self): # Print reports on self and on subdirs
         self.report()
-        for x in self.subdirs.keys():
+        for sd in self.subdirs.itervalues():
             print
-            self.subdirs[x].report()
+            sd.report()
 
     def report_full_closure(self): # Report on self and subdirs recursively
         self.report()
-        for x in self.subdirs.keys():
+        for sd in self.subdirs.itervalues():
             print
-            self.subdirs[x].report_full_closure()
+            sd.report_full_closure()
 
 
 def cmpfiles(a, b, common, shallow=1, use_statcache=0):
index e40ec8df3b40075424d7171a6f67f46f87e463ac..fdbe16a827edd632155bac66da316335708fe8c5 100644 (file)
@@ -553,7 +553,7 @@ def getclasstree(classes, unique=0):
                 if unique and parent in classes: break
         elif c not in roots:
             roots.append(c)
-    for parent in children.keys():
+    for parent in children:
         if parent not in classes:
             roots.append(parent)
     return walktree(roots, children, None)
index b8ed7597f28b8c907a0751e2205422af688f03a2..b2ddacd046db4555b65ebd84455dc38a70e9b486 100644 (file)
@@ -24,11 +24,11 @@ def getcaps():
             continue
         morecaps = readmailcapfile(fp)
         fp.close()
-        for key in morecaps.keys():
+        for key, value in morecaps.iteritems():
             if not key in caps:
-                caps[key] = morecaps[key]
+                caps[key] = value
             else:
-                caps[key] = caps[key] + morecaps[key]
+                caps[key] = caps[key] + value
     return caps
 
 def listmailcapfiles():
index c1001f9005f10003def9a786dd003f7cc623ca72..c667db2c0eed9c143176d153cbc5a3110027a312 100755 (executable)
@@ -386,12 +386,11 @@ class Profile:
 
     def snapshot_stats(self):
         self.stats = {}
-        for func in self.timings.keys():
-            cc, ns, tt, ct, callers = self.timings[func]
+        for func, (cc, ns, tt, ct, callers) in self.timings.iteritems():
             callers = callers.copy()
             nc = 0
-            for func_caller in callers.keys():
-                nc = nc + callers[func_caller]
+            for callcnt in callers.itervalues():
+                nc += callcnt
             self.stats[func] = cc, nc, tt, ct, callers
 
 
index 384db5232c8f6310a7c5c8681e2d66db8fe3914d..d36dc9e922ddad5c275727f88038336efeb8bdcd 100644 (file)
@@ -142,7 +142,7 @@ class Stats:
         self.total_calls += other.total_calls
         self.prim_calls += other.prim_calls
         self.total_tt += other.total_tt
-        for func in other.top_level.keys():
+        for func in other.top_level:
             self.top_level[func] = None
 
         if self.max_name_len < other.max_name_len:
@@ -150,12 +150,12 @@ class Stats:
 
         self.fcn_list = None
 
-        for func in other.stats.keys():
+        for func, stat in other.stats.iteritems():
             if func in self.stats:
                 old_func_stat = self.stats[func]
             else:
                 old_func_stat = (0, 0, 0, 0, {},)
-            self.stats[func] = add_func_stats(old_func_stat, other.stats[func])
+            self.stats[func] = add_func_stats(old_func_stat, stat)
         return self
 
     # list the tuple indices and directions for sorting,
@@ -178,7 +178,7 @@ class Stats:
         if not self.sort_arg_dict:
             self.sort_arg_dict = dict = {}
             bad_list = {}
-            for word in self.sort_arg_dict_default.keys():
+            for word, tup in self.sort_arg_dict_default.iteritems():
                 fragment = word
                 while fragment:
                     if not fragment:
@@ -186,9 +186,9 @@ class Stats:
                     if fragment in dict:
                         bad_list[fragment] = 0
                         break
-                    dict[fragment] = self.sort_arg_dict_default[word]
+                    dict[fragment] = tup
                     fragment = fragment[:-1]
-            for word in bad_list.keys():
+            for word in bad_list:
                 del dict[word]
         return self.sort_arg_dict
 
@@ -213,8 +213,7 @@ class Stats:
             connector = ", "
 
         stats_list = []
-        for func in self.stats.keys():
-            cc, nc, tt, ct, callers = self.stats[func]
+        for func, (cc, nc, tt, ct, callers) in self.stats.iteritems():
             stats_list.append((cc, nc, tt, ct) + func +
                               (func_std_string(func), func))
 
@@ -234,14 +233,13 @@ class Stats:
         oldstats = self.stats
         self.stats = newstats = {}
         max_name_len = 0
-        for func in oldstats.keys():
-            cc, nc, tt, ct, callers = oldstats[func]
+        for func, (cc, nc, tt, ct, callers) in oldstats.iteritems():
             newfunc = func_strip_path(func)
             if len(func_std_string(newfunc)) > max_name_len:
                 max_name_len = len(func_std_string(newfunc))
             newcallers = {}
-            for func2 in callers.keys():
-                newcallers[func_strip_path(func2)] = callers[func2]
+            for func2, caller in callers.iteritems():
+                newcallers[func_strip_path(func2)] = caller
 
             if newfunc in newstats:
                 newstats[newfunc] = add_func_stats(
@@ -251,7 +249,7 @@ class Stats:
                 newstats[newfunc] = (cc, nc, tt, ct, newcallers)
         old_top = self.top_level
         self.top_level = new_top = {}
-        for func in old_top.keys():
+        for func in old_top:
             new_top[func_strip_path(func)] = None
 
         self.max_name_len = max_name_len
@@ -263,14 +261,13 @@ class Stats:
     def calc_callees(self):
         if self.all_callees: return
         self.all_callees = all_callees = {}
-        for func in self.stats.keys():
+        for func, (cc, nc, tt, ct, callers) in self.stats.iteritems():
             if not func in all_callees:
                 all_callees[func] = {}
-            cc, nc, tt, ct, callers = self.stats[func]
-            for func2 in callers.keys():
+            for func2, caller in callers.iteritems():
                 if not func2 in all_callees:
                     all_callees[func2] = {}
-                all_callees[func2][func]  = callers[func2]
+                all_callees[func2][func]  = caller
         return
 
     #******************************************************************
@@ -330,7 +327,7 @@ class Stats:
             print filename
         if self.files: print
         indent = ' ' * 8
-        for func in self.top_level.keys():
+        for func in self.top_level:
             print indent, func_get_function_name(func)
 
         print indent, self.total_calls, "function calls",
@@ -468,20 +465,20 @@ def add_func_stats(target, source):
 def add_callers(target, source):
     """Combine two caller lists in a single list."""
     new_callers = {}
-    for func in target.keys():
-        new_callers[func] = target[func]
-    for func in source.keys():
+    for func, caller in target.iteritems():
+        new_callers[func] = caller
+    for func, caller in source.iteritems():
         if func in new_callers:
-            new_callers[func] = source[func] + new_callers[func]
+            new_callers[func] = caller + new_callers[func]
         else:
-            new_callers[func] = source[func]
+            new_callers[func] = caller
     return new_callers
 
 def count_calls(callers):
     """Sum the caller statistics to get total number of calls received."""
     nc = 0
-    for func in callers.keys():
-        nc += callers[func]
+    for calls in callers.itervalues():
+        nc += calls
     return nc
 
 #**************************************************************************
@@ -595,19 +592,19 @@ if __name__ == '__main__':
             print "Reverse the sort order of the profiling report."
 
         def do_sort(self, line):
-            abbrevs = self.stats.get_sort_arg_defs().keys()
+            abbrevs = self.stats.get_sort_arg_defs()
             if line and not filter(lambda x,a=abbrevs: x not in a,line.split()):
                 apply(self.stats.sort_stats, line.split())
             else:
                 print "Valid sort keys (unique prefixes are accepted):"
-                for (key, value) in Stats.sort_arg_dict_default.items():
+                for (key, value) in Stats.sort_arg_dict_default.iteritems():
                     print "%s -- %s" % (key, value[1])
             return 0
         def help_sort(self):
             print "Sort profile data according to specified keys."
             print "(Typing `sort' without arguments lists valid keys.)"
         def complete_sort(self, text, *args):
-            return [a for a in Stats.sort_arg_dict_default.keys() if a.startswith(text)]
+            return [a for a in Stats.sort_arg_dict_default if a.startswith(text)]
 
         def do_stats(self, line):
             return self.generic('print_stats', line)
index 29b8354e4e09d59966a6ed6ff30ad432188291e0..fad46add86e37701b03ca9a75b26cd37f00d7f82 100644 (file)
@@ -324,7 +324,7 @@ def readmodule_ex(module, path=[], inpackage=0):
                     # Python does internally)
                     # also don't add names that
                     # start with _
-                    for n in d.keys():
+                    for n in d:
                         if n[0] != '_' and \
                            not n in dict:
                             dict[n] = d[n]
index d8208f3b7aa30eb4be08d192b3729e9ec80b08a1..3075a14e3dc72d585fa58a1d49c0033ed2dd75f9 100644 (file)
@@ -104,8 +104,8 @@ class Completer:
         matches = []
         n = len(text)
         for list in [keyword.kwlist,
-                     __builtin__.__dict__.keys(),
-                     self.namespace.keys()]:
+                     __builtin__.__dict__,
+                     self.namespace]:
             for word in list:
                 if word[:n] == text and word != "__builtins__":
                     matches.append(word)
index 44983358ca603aeab5e41d8e3e933cddcb4f4d18..38042ae96041e47fea51d54025cb9ab40f3a0b8a 100644 (file)
@@ -163,7 +163,7 @@ class Class(SymbolTable):
             d = {}
             for st in self._table.children:
                 d[st.name] = 1
-            self.__methods = tuple(d.keys())
+            self.__methods = tuple(d)
         return self.__methods
 
 class Symbol: