Replaced list comprehensions with generator expressions.
authorRahul Goswami <redacted>
Thu, 1 Mar 2018 16:23:26 +0000 (21:53 +0530)
committerGitHub <redacted>
Thu, 1 Mar 2018 16:23:26 +0000 (21:53 +0530)
Previously, a list had to be created which was not necessary and hindered performance.
Generator expression handles this in the correct way.
Also now the all() can be shortcircuited as soon as it finds first False value and not goes through the complete list.

tests/test_xkcdpass.py

index dd3ecccd591ad3a6eb5e9deeceebc288f216c2fc..88579fde86adcd4c38c22e94e8c44d8ca53ecde0 100644 (file)
@@ -76,15 +76,14 @@ class XkcdPasswordTests(unittest.TestCase):
         results["alternating"] = xkcd_password.set_case(words, method="alternating")
         results["random"] = xkcd_password.set_case(words, method="random", testing=True)
 
-        words_after = set([word.lower() for group in list(results.values()) for word in group])
+        words_after = set(word.lower() for group in list(results.values()) for word in group)
 
         # Test that no words have been fundamentally mutated by any of the methods
         self.assertTrue(words_before == words_after)
 
         # Test that the words have been uppered or lowered respectively.
-        self.assertTrue(all([word.islower() for word in results["lower"]]))
-        self.assertTrue(all([word.isupper() for word in results["upper"]]))
-
+        self.assertTrue(all(word.islower() for word in results["lower"]))
+        self.assertTrue(all(word.isupper() for word in results["upper"]))
         # Test that the words have been correctly uppered randomly.
         expected_random_result_1 = ['THIS', 'IS', 'ONLY', 'a', 'test']
         expected_random_result_2 = ['THIS', 'IS', 'a', 'test', 'ALSO']
git clone https://git.99rst.org/PROJECT