Use string.ascii_lowercase and string.ascii_uppercase only

"string.lowercase" and "string.uppercase" were removed in Python 3,
we can use only "string.ascii_lowercase" and "string.ascii_uppercase".

Change-Id: I97ddac01af7070129ad8baf5960c841482c346a2
Closes-Bug: #1405918
This commit is contained in:
li,chen 2014-12-29 13:11:49 +08:00
parent c6ea17fe79
commit 6d57a3be02
6 changed files with 37 additions and 5 deletions

View File

@ -70,7 +70,7 @@ class Scenario(object):
:param length: int, length of autogenerated part of result name
:returns: str, pseudo-random name
"""
rand_part = ''.join(random.choice(string.lowercase) for i
rand_part = ''.join(random.choice(string.ascii_lowercase) for i
in range(length or cls.RESOURCE_NAME_LENGTH))
return (prefix or cls.RESOURCE_NAME_PREFIX) + rand_part

View File

@ -28,4 +28,5 @@ Rally Specific Commandments
* [N333] - Ensure that ``urlparse`` is not used
* [N334] - Ensure that ``itertools.imap`` is not used
* [N335] - Ensure that ``xrange`` is not used
* [N336] - Ensure that ``string.lowercase`` and ``string.uppercase`` are not used
* [N340] - Ensure that we are importing always ``from rally import objects``

View File

@ -51,6 +51,8 @@ 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\(")
re_xrange_method = re.compile(r"(^|[\s=])xrange\(")
re_string_lower_upper_case_method = re.compile(
r"(^|[(,\s=])string\.(lower|upper)case([)\[,\s]|$)")
def _parse_assert_mock_str(line):
@ -306,6 +308,20 @@ def check_xrange_method(logical_line):
yield (0, "N335: Use six.moves.range rather than xrange.")
def check_string_lower_upper_case_method(logical_line):
"""Check if string.lowercase and string.uppercase are properly called
In Python 3, string.lowercase and string.uppercase are gone.
The correct form is "string.ascii_lowercase" and "string.ascii_uppercase".
N336
"""
res = re_string_lower_upper_case_method.search(logical_line)
if res:
yield (0, "N336: Use string.ascii_lowercase or string.ascii_uppercase "
"rather than string.lowercase or string.uppercase.")
def check_no_direct_rally_objects_import(logical_line, filename):
"""Check if rally.objects are properly imported.
@ -339,4 +355,5 @@ def factory(register):
register(check_urlparse_method)
register(check_itertools_imap_method)
register(check_xrange_method)
register(check_string_lower_upper_case_method)
register(check_no_direct_rally_objects_import)

View File

@ -36,7 +36,7 @@ def generate_uuid():
return str(uuid.uuid4())
def generate_name(prefix="", length=12, choices=string.lowercase):
def generate_name(prefix="", length=12, choices=string.ascii_lowercase):
"""Generate pseudo-random name.
:param prefix: str, custom prefix for genertated name

View File

@ -163,6 +163,19 @@ class HackingTestCase(test.TestCase):
self.assertEqual(len(list(checks.check_xrange_method(
"six.moves.range()"))), 0)
def test_check_string_lower_upper_case_method(self):
self.assertEqual(len(list(checks.check_string_lower_upper_case_method(
"string.lowercase[:16]"))), 1)
self.assertEqual(len(list(checks.check_string_lower_upper_case_method(
"string.uppercase[:16]"))), 1)
self.assertEqual(len(list(checks.check_string_lower_upper_case_method(
"string.ascii_lowercase[:16]"))), 0)
self.assertEqual(len(list(checks.check_string_lower_upper_case_method(
"string.ascii_uppercase[:16]"))), 0)
def test_assert_equal_none(self):
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(A, None)"))), 1)

View File

@ -418,12 +418,13 @@ class GenerateRandomTestCase(test.TestCase):
idx = iter(range(100))
mock_random.choice.side_effect = lambda choice: choice[idx.next()]
self.assertEqual(utils.generate_random_name(), string.lowercase[:16])
self.assertEqual(utils.generate_random_name(),
string.ascii_lowercase[:16])
idx = iter(range(100))
mock_random.choice.side_effect = lambda choice: choice[idx.next()]
self.assertEqual(utils.generate_random_name(length=10),
string.lowercase[:10])
string.ascii_lowercase[:10])
idx = iter(range(100))
mock_random.choice.side_effect = lambda choice: choice[idx.next()]
@ -439,7 +440,7 @@ class GenerateRandomTestCase(test.TestCase):
mock_random.choice.side_effect = lambda choice: choice[idx.next()]
self.assertEqual(
utils.generate_random_name(prefix="foo_", length=10),
"foo_" + string.lowercase[:10])
"foo_" + string.ascii_lowercase[:10])
idx = iter(range(100))
mock_random.choice.side_effect = lambda choice: choice[idx.next()]