Revision: 6656 Author: nicdumz Date: 2009-04-22 02:15:13 +0000 (Wed, 22 Apr 2009)
Log Message: ----------- Similarly as r6652, use iterators where possible
Modified Paths: -------------- branches/rewrite/pywikibot/config2.py branches/rewrite/pywikibot/date.py branches/rewrite/pywikibot/page.py branches/rewrite/pywikibot/site.py branches/rewrite/scripts/category_redirect.py branches/rewrite/tests/api_tests.py branches/rewrite/tests/site_tests.py
Modified: branches/rewrite/pywikibot/config2.py =================================================================== --- branches/rewrite/pywikibot/config2.py 2009-04-22 01:53:58 UTC (rev 6655) +++ branches/rewrite/pywikibot/config2.py 2009-04-22 02:15:13 UTC (rev 6656) @@ -482,7 +482,7 @@ % {'fn' :_filename}
# Test for obsoleted and/or unknown variables. -for _key in globals().keys(): +for _key in globals().iterkeys(): if _key[0]=='_': pass elif _key in _gl: @@ -577,7 +577,7 @@
# cleanup all locally-defined variables
-for __var in globals().keys(): +for __var in globals().iterkeys(): if __var.startswith("_") and not __var.startswith("__"): del __sys.modules[__name__].__dict__[__var]
Modified: branches/rewrite/pywikibot/date.py =================================================================== --- branches/rewrite/pywikibot/date.py 2009-04-22 01:53:58 UTC (rev 6655) +++ branches/rewrite/pywikibot/date.py 2009-04-22 02:15:13 UTC (rev 6656) @@ -278,7 +278,7 @@ }
# Allows to search for '(%%)|(%d)|(%R)|...", and allows one digit 1-9 too set the size of zero-padding for numbers -_reParameters = re.compile(u'|'.join([ u'(%%[1-9]?%s)' % s for s in _digitDecoders.keys() ])) +_reParameters = re.compile(u'|'.join([ u'(%%[1-9]?%s)' % s for s in _digitDecoders.iterkeys() ]))
# A map of sitecode+pattern to (re matching object and corresponding decoders) _escPtrnCache2 = {} @@ -1567,7 +1567,7 @@ >>> date.test()
""" - for formatName in formats.keys(): + for formatName in formats.iterkeys():
if quick: testMapEntry( formatName, showAll, formatLimits[formatName][1] ) # Only test the first value in the test range
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2009-04-22 01:53:58 UTC (rev 6655) +++ branches/rewrite/pywikibot/page.py 2009-04-22 02:15:13 UTC (rev 6656) @@ -412,7 +412,7 @@ def previousRevision(self): """Return the revision id for the previous revision of this Page.""" vh = self.getVersionHistory(revCount=2) - revkey = sorted(self._revisions.keys(), reverse=True)[1] + revkey = sorted(self._revisions.iterkeys(), reverse=True)[1] return revkey
def exists(self): @@ -848,7 +848,7 @@ args = template[1] positional = [] named = {} - for key in sorted(args.keys()): + for key in sorted(args.iterkeys()): try: int(key) except ValueError: @@ -912,7 +912,7 @@ self._revisions[rev].timestamp, self._revisions[rev].user, self._revisions[rev].comment - ) for rev in sorted(self._revisions.keys(), + ) for rev in sorted(self._revisions.iterkeys(), reverse=not reverseOrder)[ : revCount] ]
@@ -1451,7 +1451,7 @@ @type recurse: int or bool
""" - namespaces = [x for x in self.site().namespaces().keys() + namespaces = [x for x in self.site().namespaces().iterkeys() if x>=0 and x!=14] for member in self.site().categorymembers(self, namespaces=namespaces): @@ -1945,7 +1945,6 @@ return cmp(self.namespace, other.namespace) return cmp(self.title, other.title)
- # Utility functions for parsing page titles
def html2unicode(text, ignore = []):
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2009-04-22 01:53:58 UTC (rev 6655) +++ branches/rewrite/pywikibot/site.py 2009-04-22 02:15:13 UTC (rev 6656) @@ -219,7 +219,7 @@ def validLanguageLinks(self): """Return list of language codes that can be used in interwiki links."""
- nsnames = sum(self.namespaces().values(), []) + nsnames = sum(self.namespaces().itervalues(), []) return [l for l in self.languages() if l[:1].upper() + l[1:] not in self.namespaces()]
@@ -997,7 +997,7 @@ if "pages" not in result['query']: # no "pages" element indicates a circular redirect raise pywikibot.CircularRedirect(redirmap[title]) - for pagedata in result['query']['pages'].values(): + for pagedata in result['query']['pages'].itervalues(): # there should be only one value in 'pages', and it is the target if pagedata['title'] not in redirmap.values(): raise RuntimeError(
Modified: branches/rewrite/scripts/category_redirect.py =================================================================== --- branches/rewrite/scripts/category_redirect.py 2009-04-22 01:53:58 UTC (rev 6655) +++ branches/rewrite/scripts/category_redirect.py 2009-04-22 02:15:13 UTC (rev 6656) @@ -326,8 +326,8 @@ if len(log_items) < LOG_SIZE: return log_text # sort by keys and keep the first (LOG_SIZE-1) values - keep = [text for (key, text) - in sorted(log_items.items(), reverse=True)[ : LOG_SIZE-1]] + keep = [text for (_, text) in + sorted(log_items.iteritems(), reverse=True)[:LOG_SIZE-1]] log_text = "\n".join("\n".join(line for line in text) for text in keep) # get permalink to older logs history = self.log_page.getVersionHistory(revCount=LOG_SIZE) @@ -489,7 +489,7 @@ ## % cat.title(asLink=True, textlink=True))
# delete record entries for non-existent categories - for cat_name in list(record.keys()): + for cat_name in record.iterkeys(): if pywikibot.Category( pywikibot.Link(self.catprefix+cat_name, self.site) ) not in catmap: @@ -497,7 +497,7 @@
pywikibot.output(u"") pywikibot.output(u"Checking %s destination categories" % len(destmap)) - for dest in pagegenerators.PreloadingGenerator(destmap.keys()): + for dest in pagegenerators.PreloadingGenerator(destmap.iterkeys()): if not dest.exists(): for d in destmap[dest]: problems.append("# %s redirects to %s"
Modified: branches/rewrite/tests/api_tests.py =================================================================== --- branches/rewrite/tests/api_tests.py 2009-04-22 01:53:58 UTC (rev 6655) +++ branches/rewrite/tests/api_tests.py 2009-04-22 02:15:13 UTC (rev 6656) @@ -30,7 +30,7 @@ # req.keys() should contain "foo", "bar", "format", "maxlag", "one" self.assertEqual(len(req.keys()), 5) self.assert_("test" in req.values()) - self.assert_(all(len(item) == 2 for item in req.items())) + self.assert_(all(len(item) == 2 for item in req.iteritems()))
class TestPageGenerator(unittest.TestCase):
Modified: branches/rewrite/tests/site_tests.py =================================================================== --- branches/rewrite/tests/site_tests.py 2009-04-22 01:53:58 UTC (rev 6655) +++ branches/rewrite/tests/site_tests.py 2009-04-22 02:15:13 UTC (rev 6656) @@ -98,9 +98,9 @@ self.assertTrue(all(isinstance(key, int) for key in ns)) self.assertTrue(all(isinstance(val, list) - for val in ns.values())) + for val in ns.itervalues())) self.assertTrue(all(isinstance(name, basestring) - for val in ns.values() + for val in ns.itervalues() for name in val)) self.assertTrue(all(isinstance(mysite.namespace(key), basestring) for key in ns))