From 1e2b4e3922072a6be47f645aa67423c00a22d0ca Mon Sep 17 00:00:00 2001 From: Grigory Bukovsky Date: Thu, 13 Mar 2025 19:41:57 +0300 Subject: [PATCH 1/5] Improve perfomance of UUID.hex and UUID.__str__ by using bytes.hex() --- Lib/uuid.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Lib/uuid.py b/Lib/uuid.py index 7a12b48cb008f1..e7dd08101e9327 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -321,9 +321,8 @@ def __setattr__(self, name, value): raise TypeError('UUID objects are immutable') def __str__(self): - hex = '%032x' % self.int - return '%s-%s-%s-%s-%s' % ( - hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:]) + hex = self.hex + return f'{hex[:8]}-{hex[8:12]}-{hex[12:16]}-{hex[16:20]}-{hex[20:]}' @property def bytes(self): @@ -387,7 +386,7 @@ def node(self): @property def hex(self): - return '%032x' % self.int + return self.bytes.hex() @property def urn(self): From 32e69e636d79340b253fe13486fd9765b4e8b566 Mon Sep 17 00:00:00 2001 From: Grigory Bukovsky Date: Thu, 13 Mar 2025 19:54:25 +0300 Subject: [PATCH 2/5] Update News --- .../next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst diff --git a/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst b/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst new file mode 100644 index 00000000000000..7123b309a634b3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst @@ -0,0 +1 @@ +Improve perfomance of UUID.hex and UUID.__str__ by using bytes.hex(). From 6ed5b817eba9a8be6a8c2e751e8e6b011b549530 Mon Sep 17 00:00:00 2001 From: Grigory Bukovsky <32143244+booqoffsky@users.noreply.github.com> Date: Thu, 13 Mar 2025 22:29:35 +0300 Subject: [PATCH 3/5] Update News MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst b/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst index 7123b309a634b3..45e37580b97420 100644 --- a/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst +++ b/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst @@ -1 +1 @@ -Improve perfomance of UUID.hex and UUID.__str__ by using bytes.hex(). +Improve perfomance of :meth:`uuid.UUID.hex` and :meth:`uuid.UUID.__str__`. From d7ec5d9ed2a43d36f1f8d7411586a89d416f85d3 Mon Sep 17 00:00:00 2001 From: Grigory Bukovsky Date: Thu, 13 Mar 2025 22:33:06 +0300 Subject: [PATCH 4/5] Fix __str__ method --- Lib/uuid.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/uuid.py b/Lib/uuid.py index e7dd08101e9327..6f7a7a3f42ac11 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -321,8 +321,8 @@ def __setattr__(self, name, value): raise TypeError('UUID objects are immutable') def __str__(self): - hex = self.hex - return f'{hex[:8]}-{hex[8:12]}-{hex[12:16]}-{hex[16:20]}-{hex[20:]}' + x = self.hex + return f'{x[:8]}-{x[8:12]}-{x[12:16]}-{x[16:20]}-{x[20:]}' @property def bytes(self): From fa09e182b3e50a72cb1cb1fc1f217bd16e7f36b9 Mon Sep 17 00:00:00 2001 From: Grigory Bukovsky <32143244+booqoffsky@users.noreply.github.com> Date: Fri, 14 Mar 2025 00:13:08 +0300 Subject: [PATCH 5/5] Update NEWS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst b/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst index 45e37580b97420..72c10fe57e8dad 100644 --- a/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst +++ b/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst @@ -1 +1 @@ -Improve perfomance of :meth:`uuid.UUID.hex` and :meth:`uuid.UUID.__str__`. +Improve perfomance of :attr:`uuid.UUID.hex` and :meth:`uuid.UUID.__str__ `.