From ff87f8bd52a5c4a8f79e1cf723c124f657c85fee Mon Sep 17 00:00:00 2001 From: Wolfgang Maier Date: Thu, 5 Apr 2018 10:38:45 +0200 Subject: [PATCH] Use Random.choices in tempfile Using Random.choices instead of Random.choice in the tempfile._RandomNameSequence class makes the code more readable and faster. This commit also updates the docstring of the class. --- Lib/tempfile.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 71ecafa5305838..ff3561f65f8aff 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -132,7 +132,7 @@ def _sanitize_params(prefix, suffix, dir): class _RandomNameSequence: """An instance of _RandomNameSequence generates an endless sequence of unpredictable strings which can safely be incorporated - into file names. Each string is six characters long. Multiple + into file names. Each string is eight characters long. Multiple threads can safely use the same instance at the same time. _RandomNameSequence is an iterator.""" @@ -151,9 +151,7 @@ def __iter__(self): return self def __next__(self): - c = self.characters - choose = self.rng.choice - letters = [choose(c) for dummy in range(8)] + letters = self.rng.choices(self.characters, k=8) return ''.join(letters) def _candidate_tempdir_list():