[Python-checkins] distutils2: Save up a bit of memory thanks to dict.iter* and __iter__

tarek.ziade python-checkins at python.org
Sun Jan 23 15:48:24 CET 2011


tarek.ziade pushed a4e3aa638aa8 to distutils2:

http://hg.python.org/distutils2/rev/a4e3aa638aa8
changeset:   890:a4e3aa638aa8
user:        ?ric Araujo <merwok at netwok.org>
date:        Fri Dec 17 18:18:59 2010 +0100
summary:
  Save up a bit of memory thanks to dict.iter* and __iter__

files:
  distutils2/_backport/pkgutil.py
  distutils2/_backport/shutil.py
  distutils2/_backport/sysconfig.py
  distutils2/_backport/tests/test_pkgutil.py
  distutils2/command/__init__.py
  distutils2/command/build_py.py
  distutils2/command/check.py
  distutils2/command/install_dist.py
  distutils2/command/register.py
  distutils2/command/sdist.py
  distutils2/command/upload.py
  distutils2/compiler/__init__.py
  distutils2/compiler/ccompiler.py
  distutils2/compiler/msvc9compiler.py
  distutils2/compiler/msvccompiler.py
  distutils2/config.py
  distutils2/dist.py
  distutils2/fancy_getopt.py
  distutils2/index/dist.py
  distutils2/index/wrapper.py
  distutils2/install.py
  distutils2/mkcfg.py
  distutils2/tests/test_command_bdist.py
  distutils2/tests/test_index_simple.py
  distutils2/tests/test_install.py
  runtests.py

diff --git a/distutils2/_backport/pkgutil.py b/distutils2/_backport/pkgutil.py
--- a/distutils2/_backport/pkgutil.py
+++ b/distutils2/_backport/pkgutil.py
@@ -343,8 +343,7 @@
     from zipimport import zipimporter
 
     def iter_zipimport_modules(importer, prefix=''):
-        dirlist = zipimport._zip_directory_cache[importer.archive].keys()
-        dirlist.sort()
+        dirlist = sorted(zipimport._zip_directory_cache[importer.archive])
         _prefix = importer.prefix
         plen = len(_prefix)
         yielded = {}
diff --git a/distutils2/_backport/shutil.py b/distutils2/_backport/shutil.py
--- a/distutils2/_backport/shutil.py
+++ b/distutils2/_backport/shutil.py
@@ -349,7 +349,7 @@
     compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'compress': '.Z'}
 
     # flags for compression program, each element of list will be an argument
-    if compress is not None and compress not in compress_ext.keys():
+    if compress is not None and compress not in compress_ext:
         raise ValueError, \
               ("bad value for 'compress': must be None, 'gzip', 'bzip2' "
                "or 'compress'")
@@ -487,7 +487,7 @@
     Each element of the returned sequence is a tuple (name, description)
     """
     formats = [(name, registry[2]) for name, registry in
-               _ARCHIVE_FORMATS.items()]
+               _ARCHIVE_FORMATS.iteritems()]
     formats.sort()
     return formats
 
diff --git a/distutils2/_backport/sysconfig.py b/distutils2/_backport/sysconfig.py
--- a/distutils2/_backport/sysconfig.py
+++ b/distutils2/_backport/sysconfig.py
@@ -102,9 +102,8 @@
 
 
 def _extend_dict(target_dict, other_dict):
-    target_keys = target_dict.keys()
-    for key, value in other_dict.items():
-        if key in target_keys:
+    for key, value in other_dict.iteritems():
+        if key in target_dict:
             continue
         target_dict[key] = value
 
@@ -713,7 +712,7 @@
 
 
 def _print_dict(title, data):
-    for index, (key, value) in enumerate(sorted(data.items())):
+    for index, (key, value) in enumerate(sorted(data.iteritems())):
         if index == 0:
             print '%s: ' % (title)
         print '\t%s = "%s"' % (key, value)
diff --git a/distutils2/_backport/tests/test_pkgutil.py b/distutils2/_backport/tests/test_pkgutil.py
--- a/distutils2/_backport/tests/test_pkgutil.py
+++ b/distutils2/_backport/tests/test_pkgutil.py
@@ -249,7 +249,7 @@
             dist = Distribution(distinfo_dir)
             for path, md5_, size in dist.get_installed_files():
                 record_data = self.records[dist.path]
-                self.assertTrue(path in record_data.keys())
+                self.assertIn(path, record_data)
                 self.assertEqual(md5_, record_data[path][0])
                 self.assertEqual(size, record_data[path][1])
 
@@ -318,7 +318,7 @@
         self.assertEqual(sorted(found), sorted(distinfo_record_paths))
         # Test for the iteration of local absolute paths
         distinfo_record_paths = [os.path.join(sys.prefix, path)
-            for path in self.records[distinfo_dir].keys()]
+            for path in self.records[distinfo_dir]]
         found = [path for path in dist.get_distinfo_files(local=True)]
         self.assertEqual(sorted(found), sorted(distinfo_record_paths))
 
@@ -382,7 +382,7 @@
             if not isinstance(dist, Distribution):
                 self.fail("item received was not a Distribution instance: "
                     "%s" % type(dist))
-            if dist.name in dict(fake_dists).keys() and \
+            if dist.name in dict(fake_dists) and \
                dist.path.startswith(self.fake_dists_path):
                 found_dists.append((dist.name, dist.metadata['version'],))
             else:
@@ -405,7 +405,7 @@
                     isinstance(dist, EggInfoDistribution)):
                 self.fail("item received was not a Distribution or "
                           "EggInfoDistribution instance: %s" % type(dist))
-            if dist.name in dict(fake_dists).keys() and \
+            if dist.name in dict(fake_dists) and \
                dist.path.startswith(self.fake_dists_path):
                 found_dists.append((dist.name, dist.metadata['version']))
             else:
diff --git a/distutils2/command/__init__.py b/distutils2/command/__init__.py
--- a/distutils2/command/__init__.py
+++ b/distutils2/command/__init__.py
@@ -31,8 +31,8 @@
 
 
 def get_command_names():
-    return sorted(_COMMANDS.keys())
     """Return registered commands"""
+    return sorted(_COMMANDS)
 
 
 def set_command(location):
diff --git a/distutils2/command/build_py.py b/distutils2/command/build_py.py
--- a/distutils2/command/build_py.py
+++ b/distutils2/command/build_py.py
@@ -68,7 +68,7 @@
         self.package_data = self.distribution.package_data
         self.package_dir = {}
         if self.distribution.package_dir:
-            for name, path in self.distribution.package_dir.items():
+            for name, path in self.distribution.package_dir.iteritems():
                 self.package_dir[name] = convert_path(path)
         self.data_files = self.get_data_files()
 
diff --git a/distutils2/command/check.py b/distutils2/command/check.py
--- a/distutils2/command/check.py
+++ b/distutils2/command/check.py
@@ -76,11 +76,11 @@
             raise DistutilsSetupError('The docutils package is needed.')
 
     def check_hooks_resolvable(self):
-        for options in self.distribution.command_options.values():
+        for options in self.distribution.command_options.itervalues():
             for hook_kind in ("pre_hook", "post_hook"):
                 if hook_kind not in options:
                     break
-                for hook_name in options[hook_kind][1].values():
+                for hook_name in options[hook_kind][1].itervalues():
                     try:
                         resolve_name(hook_name)
                     except ImportError:
diff --git a/distutils2/command/install_dist.py b/distutils2/command/install_dist.py
--- a/distutils2/command/install_dist.py
+++ b/distutils2/command/install_dist.py
@@ -424,7 +424,7 @@
         """Set the install directories by applying the install schemes."""
         # it's the caller's problem if they supply a bad name!
         scheme = get_paths(name, expand=False)
-        for key, value in scheme.items():
+        for key, value in scheme.iteritems():
             if key == 'platinclude':
                 key = 'headers'
                 value = os.path.join(value, self.distribution.metadata['Name'])
diff --git a/distutils2/command/register.py b/distutils2/command/register.py
--- a/distutils2/command/register.py
+++ b/distutils2/command/register.py
@@ -235,7 +235,7 @@
         sep_boundary = '\n--' + boundary
         end_boundary = sep_boundary + '--'
         body = StringIO.StringIO()
-        for key, value in data.items():
+        for key, value in data.iteritems():
             # handle multiple entries for the same name
             if not isinstance(value, (tuple, list)):
                 value = [value]
diff --git a/distutils2/command/sdist.py b/distutils2/command/sdist.py
--- a/distutils2/command/sdist.py
+++ b/distutils2/command/sdist.py
@@ -371,8 +371,7 @@
         need_dir = {}
         for file in files:
             need_dir[os.path.join(base_dir, os.path.dirname(file))] = 1
-        need_dirs = need_dir.keys()
-        need_dirs.sort()
+        need_dirs = sorted(need_dir)
 
         # Now create them
         for dir in need_dirs:
diff --git a/distutils2/command/upload.py b/distutils2/command/upload.py
--- a/distutils2/command/upload.py
+++ b/distutils2/command/upload.py
@@ -140,7 +140,7 @@
         body = StringIO()
         file_fields = ('content', 'gpg_signature')
 
-        for key, values in data.items():
+        for key, values in data.iteritems():
             # handle multiple entries for the same name
             if not isinstance(values, (tuple, list)):
                 values = [values]
diff --git a/distutils2/compiler/__init__.py b/distutils2/compiler/__init__.py
--- a/distutils2/compiler/__init__.py
+++ b/distutils2/compiler/__init__.py
@@ -127,7 +127,7 @@
     from distutils2.fancy_getopt import FancyGetopt
     compilers = []
 
-    for name, cls in _COMPILERS.items():
+    for name, cls in _COMPILERS.iteritems():
         if isinstance(cls, str):
             cls = resolve_name(cls)
             _COMPILERS[name] = cls
diff --git a/distutils2/compiler/ccompiler.py b/distutils2/compiler/ccompiler.py
--- a/distutils2/compiler/ccompiler.py
+++ b/distutils2/compiler/ccompiler.py
@@ -116,8 +116,8 @@
         # named library files) to include on any link
         self.objects = []
 
-        for key in self.executables.keys():
-            self.set_executable(key, self.executables[key])
+        for key, value in self.executables.iteritems():
+            self.set_executable(key, value)
 
     def set_executables(self, **args):
         """Define the executables (and options for them) that will be run
@@ -145,12 +145,12 @@
         # discovered at run-time, since there are many different ways to do
         # basically the same things with Unix C compilers.
 
-        for key in args.keys():
+        for key, value in args.iteritems():
             if key not in self.executables:
                 raise ValueError, \
                       "unknown executable '%s' for class %s" % \
                       (key, self.__class__.__name__)
-            self.set_executable(key, args[key])
+            self.set_executable(key, value)
 
     def set_executable(self, key, value):
         if isinstance(value, str):
diff --git a/distutils2/compiler/msvc9compiler.py b/distutils2/compiler/msvc9compiler.py
--- a/distutils2/compiler/msvc9compiler.py
+++ b/distutils2/compiler/msvc9compiler.py
@@ -153,7 +153,7 @@
                 self.macros["$(FrameworkVersion)"] = d["version"]
 
     def sub(self, s):
-        for k, v in self.macros.items():
+        for k, v in self.macros.iteritems():
             s = s.replace(k, v)
         return s
 
@@ -271,7 +271,7 @@
             result[key] = removeDuplicates(value)
 
     if len(result) != len(interesting):
-        raise ValueError(str(list(result.keys())))
+        raise ValueError(str(list(result)))
 
     return result
 
diff --git a/distutils2/compiler/msvccompiler.py b/distutils2/compiler/msvccompiler.py
--- a/distutils2/compiler/msvccompiler.py
+++ b/distutils2/compiler/msvccompiler.py
@@ -146,7 +146,7 @@
             self.macros["$(FrameworkVersion)"] = d["version"]
 
     def sub(self, s):
-        for k, v in self.macros.items():
+        for k, v in self.macros.iteritems():
             s = string.replace(s, k, v)
         return s
 
diff --git a/distutils2/config.py b/distutils2/config.py
--- a/distutils2/config.py
+++ b/distutils2/config.py
@@ -98,7 +98,7 @@
 
         # setting the metadata values
         if 'metadata' in content:
-            for key, value in content['metadata'].items():
+            for key, value in content['metadata'].iteritems():
                 key = key.replace('_', '-')
                 value = self._multiline(value)
                 if key == 'project-url':
@@ -124,7 +124,7 @@
 
         if 'files' in content:
             files = dict([(key, self._multiline(value))
-                          for key, value in content['files'].items()])
+                          for key, value in content['files'].iteritems()])
             self.dist.packages = []
             self.dist.package_dir = {}
 
@@ -223,7 +223,7 @@
         # If there was a "global" section in the config file, use it
         # to set Distribution options.
         if 'global' in self.dist.command_options:
-            for (opt, (src, val)) in self.dist.command_options['global'].items():
+            for (opt, (src, val)) in self.dist.command_options['global'].iteritems():
                 alias = self.dist.negative_opt.get(opt)
                 try:
                     if alias:
diff --git a/distutils2/dist.py b/distutils2/dist.py
--- a/distutils2/dist.py
+++ b/distutils2/dist.py
@@ -228,14 +228,14 @@
             options = attrs.get('options')
             if options is not None:
                 del attrs['options']
-                for (command, cmd_options) in options.items():
+                for (command, cmd_options) in options.iteritems():
                     opt_dict = self.get_option_dict(command)
-                    for (opt, val) in cmd_options.items():
+                    for (opt, val) in cmd_options.iteritems():
                         opt_dict[opt] = ("setup script", val)
 
             # Now work on the rest of the attributes.  Any attribute that's
             # not already defined is invalid!
-            for key, val in attrs.items():
+            for key, val in attrs.iteritems():
                 if self.metadata.is_metadata_field(key):
                     self.metadata[key] = val
                 elif hasattr(self, key):
@@ -280,8 +280,7 @@
         from pprint import pformat
 
         if commands is None:             # dump all command option dicts
-            commands = self.command_options.keys()
-            commands.sort()
+            commands = sorted(self.command_options)
 
         if header is not None:
             self.announce(indent + header)
@@ -478,7 +477,7 @@
         # Put the options from the command line into their official
         # holding pen, the 'command_options' dictionary.
         opt_dict = self.get_option_dict(command)
-        for (name, value) in vars(opts).items():
+        for (name, value) in vars(opts).iteritems():
             opt_dict[name] = ("command line", value)
 
         return args
@@ -680,7 +679,7 @@
 
         logger.debug("  setting options for '%s' command:" % command_name)
 
-        for (option, (source, value)) in option_dict.items():
+        for (option, (source, value)) in option_dict.iteritems():
             logger.debug("    %s = %s (from %s)" % (option, value, source))
             try:
                 bool_opts = [x.replace('-', '_')
diff --git a/distutils2/fancy_getopt.py b/distutils2/fancy_getopt.py
--- a/distutils2/fancy_getopt.py
+++ b/distutils2/fancy_getopt.py
@@ -107,7 +107,7 @@
 
     def _check_alias_dict (self, aliases, what):
         assert isinstance(aliases, dict)
-        for (alias, opt) in aliases.items():
+        for (alias, opt) in aliases.iteritems():
             if alias not in self.option_index:
                 raise DistutilsGetoptError, \
                       ("invalid %s '%s': "
diff --git a/distutils2/index/dist.py b/distutils2/index/dist.py
--- a/distutils2/index/dist.py
+++ b/distutils2/index/dist.py
@@ -408,7 +408,7 @@
             if not version in self.get_versions():
                 # append only if not already exists
                 self.releases.append(release)
-            for dist in release.dists.values():
+            for dist in release.dists.itervalues():
                 for url in dist.urls:
                     self.add_release(version, dist.dist_type, **url)
         else:
diff --git a/distutils2/index/wrapper.py b/distutils2/index/wrapper.py
--- a/distutils2/index/wrapper.py
+++ b/distutils2/index/wrapper.py
@@ -57,7 +57,7 @@
 
         # instantiate the classes and set their _project attribute to the one
         # of the wrapper.
-        for name, cls in index_classes.items():
+        for name, cls in index_classes.iteritems():
             obj = self._indexes.setdefault(name, cls())
             obj._projects = self._projects
             obj._index = self
diff --git a/distutils2/install.py b/distutils2/install.py
--- a/distutils2/install.py
+++ b/distutils2/install.py
@@ -123,13 +123,13 @@
     try:
         if install:
             installed_files = install_dists(install, install_path)  # install to tmp first
-        for files in temp_files.values():
+        for files in temp_files.itervalues():
             for old, new in files:
                 os.remove(new)
 
     except Exception,e:
         # if an error occurs, put back the files in the good place.
-        for files in temp_files.values():
+        for files in temp_files.itervalues():
             for old, new in files:
                 shutil.move(new, old)
 
@@ -183,7 +183,7 @@
     infos = {'install': [], 'remove': [], 'conflict': []}
 
     # Get what the missing deps are
-    for dists in depgraph.missing.values():
+    for dists in depgraph.missing.itervalues():
         if dists:
             logging.info("missing dependencies found, installing them")
             # we have missing deps
@@ -203,7 +203,7 @@
     """extends the lists contained in the `info` dict with those contained
     in the `new_info` one
     """
-    for key, value in infos.items():
+    for key, value in infos.iteritems():
         if key in new_infos:
             infos[key].extend(new_infos[key])
 
diff --git a/distutils2/mkcfg.py b/distutils2/mkcfg.py
--- a/distutils2/mkcfg.py
+++ b/distutils2/mkcfg.py
@@ -369,7 +369,7 @@
         if not trove:
             return
 
-        for key in sorted(trove.keys()):
+        for key in sorted(trove):
             if len(trove[key]) == 0:
                 if ask_yn('Add "%s"' % desc[4:] + ' :: ' + key, 'n') == 'y':
                     classifiers[desc[4:] + ' :: ' + key] = 1
@@ -457,7 +457,7 @@
                            "number.")
 
     def _dotted_packages(self, data):
-        packages = sorted(data.keys())
+        packages = sorted(data)
         modified_pkgs = []
         for pkg in packages:
             pkg = pkg.lstrip('./')
diff --git a/distutils2/tests/test_command_bdist.py b/distutils2/tests/test_command_bdist.py
--- a/distutils2/tests/test_command_bdist.py
+++ b/distutils2/tests/test_command_bdist.py
@@ -22,10 +22,9 @@
         # XXX an explicit list in bdist is
         # not the best way to  bdist_* commands
         # we should add a registry
-        formats = ['zip', 'gztar', 'bztar', 'ztar', 'tar', 'wininst', 'msi']
-        formats.sort()
-        found = cmd.format_command.keys()
-        found.sort()
+        formats = sorted(('zip', 'gztar', 'bztar', 'ztar',
+                          'tar', 'wininst', 'msi'))
+        found = sorted(cmd.format_command)
         self.assertEqual(found, formats)
 
 def test_suite():
diff --git a/distutils2/tests/test_index_simple.py b/distutils2/tests/test_index_simple.py
--- a/distutils2/tests/test_index_simple.py
+++ b/distutils2/tests/test_index_simple.py
@@ -293,8 +293,8 @@
 <a href="../download" rel="download">link2</a>
 <a href="../simpleurl">link2</a>
         """
-        found_links = dict(crawler._default_link_matcher(content,
-                                                         base_url)).keys()
+        found_links = set(dict(crawler._default_link_matcher(content,
+                                                             base_url)))
         self.assertIn('http://example.org/some/homepage', found_links)
         self.assertIn('http://example.org/some/simpleurl', found_links)
         self.assertIn('http://example.org/some/download', found_links)
diff --git a/distutils2/tests/test_install.py b/distutils2/tests/test_install.py
--- a/distutils2/tests/test_install.py
+++ b/distutils2/tests/test_install.py
@@ -214,7 +214,7 @@
 
         for dict1, dict2, expect in tests:
             install._update_infos(dict1, dict2)
-            for key in expect.keys():
+            for key in expect:
                 self.assertEqual(expect[key], dict1[key])
 
     def test_install_dists_rollback(self):
diff --git a/runtests.py b/runtests.py
--- a/runtests.py
+++ b/runtests.py
@@ -64,7 +64,7 @@
         # running coverage 2.x
         cov.cache = COVERAGE_FILE
         cov.restore()
-        morfs = [m for m in cov.cexecuted.keys() if "distutils2" in m]
+        morfs = [m for m in cov.cexecuted if "distutils2" in m]
 
     prefixes = ["runtests", "distutils2/tests", "distutils2/_backport"]
     prefixes += ignore_prefixes(unittest)

--
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list