Use six.moves.map instead of itertools.imap

In Python 3, module "itertools" doesn't contain "imap" object.
To support both Python 2 and Python 3, "six.moves.map"
should be used instead.

Change-Id: I5b82020d0721a91aa6d6090a42073a3285b80ff9
Closes-Bug: #1403434
This commit is contained in:
li,chen 2014-12-26 21:28:09 +08:00
parent f5e1917bbf
commit b8a416a71d
4 changed files with 23 additions and 2 deletions

View File

@ -16,7 +16,6 @@
import functools
import imp
import inspect
import itertools
import multiprocessing
import os
import random
@ -52,7 +51,7 @@ class ImmutableMixin(object):
class EnumMixin(object):
def __iter__(self):
for k, v in itertools.imap(lambda x: (x, getattr(self, x)), dir(self)):
for k, v in moves.map(lambda x: (x, getattr(self, x)), dir(self)):
if not k.startswith('_'):
yield v

View File

@ -26,4 +26,5 @@ Rally Specific Commandments
* [N331] - Ensure that ``basestring`` is not used
* [N332] - Ensure that ``StringIO.StringIO`` is not used
* [N333] - Ensure that ``urlparse`` is not used
* [N334] - Ensure that ``itertools.imap`` is not used
* [N340] - Ensure that we are importing always ``from rally import objects``

View File

@ -49,6 +49,7 @@ re_iteritems_method = re.compile(r"\.iteritems\(\)")
re_basestring_method = re.compile(r"(^|[\s,(\[=])basestring([\s,)\]]|$)")
re_StringIO_method = re.compile(r"StringIO\.StringIO\(")
re_urlparse_method = re.compile(r"(^|[\s=])urlparse\.")
re_itertools_imap_method = re.compile(r"(^|[\s=])itertools\.imap\(")
def _parse_assert_mock_str(line):
@ -280,6 +281,18 @@ def check_urlparse_method(logical_line):
yield (0, "N333: Use six.moves.urllib.parse rather than urlparse.")
def check_itertools_imap_method(logical_line):
"""Check if itertools.imap is properly called for compatibility with Python 3
The correct form is six.moves.map instead of itertools.imap.
N334
"""
res = re_itertools_imap_method.search(logical_line)
if res:
yield (0, "N334: Use six.moves.map rather than itertools.imap.")
def check_no_direct_rally_objects_import(logical_line, filename):
"""Check if rally.objects are properly imported.
@ -311,4 +324,5 @@ def factory(register):
register(check_basestring_method)
register(check_StringIO_method)
register(check_urlparse_method)
register(check_itertools_imap_method)
register(check_no_direct_rally_objects_import)

View File

@ -149,6 +149,13 @@ class HackingTestCase(test.TestCase):
self.assertEqual(len(list(checks.check_urlparse_method(
"six.moves.urllib.parse.urlparse(url)"))), 0)
def test_check_itertools_imap_method(self):
self.assertEqual(len(list(checks.check_itertools_imap_method(
"itertools.imap()"))), 1)
self.assertEqual(len(list(checks.check_itertools_imap_method(
"six.moves.map()"))), 0)
def test_assert_equal_none(self):
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(A, None)"))), 1)