Skip to content

Commit 74a1978

Browse files
committed
Use faster generator for link IDs
1 parent 4d6d631 commit 74a1978

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

rich/style.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import sys
22
from functools import lru_cache
3+
from itertools import count
4+
from marshal import dumps, loads
35
from operator import attrgetter
46
from pickle import dumps, loads
5-
from random import randint
7+
from time import time
68
from typing import Any, Dict, Iterable, List, Optional, Type, Union, cast
79

810
from . import errors
@@ -18,6 +20,9 @@
1820
StyleType = Union[str, "Style"]
1921

2022

23+
_id_generator = count(int(time()) >> 4)
24+
25+
2126
class _Bit:
2227
"""A descriptor to get/set a style attribute bit."""
2328

@@ -195,7 +200,7 @@ def _make_color(color: Union[Color, str]) -> Color:
195200
self._link = link
196201
self._meta = None if meta is None else dumps(meta)
197202
self._link_id = (
198-
f"{randint(0, 999999)}{hash(self._meta)}" if (link or meta) else ""
203+
f"{next(_id_generator)}{hash(self._meta)}" if (link or meta) else ""
199204
)
200205
self._hash: Optional[int] = None
201206
self._null = not (self._set_attributes or color or bgcolor or link or meta)
@@ -245,7 +250,7 @@ def from_meta(cls, meta: Optional[Dict[str, Any]]) -> "Style":
245250
style._attributes = 0
246251
style._link = None
247252
style._meta = dumps(meta)
248-
style._link_id = f"{randint(0, 999999)}{hash(style._meta)}"
253+
style._link_id = f"{next(_id_generator)}{hash(style._meta)}"
249254
style._hash = None
250255
style._null = not (meta)
251256
return style
@@ -483,7 +488,7 @@ def without_color(self) -> "Style":
483488
style._attributes = self._attributes
484489
style._set_attributes = self._set_attributes
485490
style._link = self._link
486-
style._link_id = f"{randint(0, 999999)}" if self._link else ""
491+
style._link_id = f"{next(_id_generator)}" if self._link else ""
487492
style._null = False
488493
style._meta = None
489494
style._hash = None
@@ -635,7 +640,7 @@ def copy(self) -> "Style":
635640
style._attributes = self._attributes
636641
style._set_attributes = self._set_attributes
637642
style._link = self._link
638-
style._link_id = f"{randint(0, 999999)}" if self._link else ""
643+
style._link_id = f"{next(_id_generator)}" if self._link else ""
639644
style._hash = self._hash
640645
style._null = False
641646
style._meta = self._meta
@@ -681,7 +686,7 @@ def update_link(self, link: Optional[str] = None) -> "Style":
681686
style._attributes = self._attributes
682687
style._set_attributes = self._set_attributes
683688
style._link = link
684-
style._link_id = f"{randint(0, 999999)}" if link else ""
689+
style._link_id = f"{next(_id_generator)}" if link else ""
685690
style._hash = None
686691
style._null = False
687692
style._meta = self._meta

0 commit comments

Comments
 (0)