Skip to content

Commit 75a019e

Browse files
committed
Use faster generator for link IDs
1 parent ea9d4db commit 75a019e

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

rich/style.py

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

78
from . import errors
@@ -13,6 +14,9 @@
1314
StyleType = Union[str, "Style"]
1415

1516

17+
_id_generator = count(int(time()) >> 4)
18+
19+
1620
class _Bit:
1721
"""A descriptor to get/set a style attribute bit."""
1822

@@ -190,7 +194,7 @@ def _make_color(color: Union[Color, str]) -> Color:
190194
self._link = link
191195
self._meta = None if meta is None else dumps(meta)
192196
self._link_id = (
193-
f"{randint(0, 999999)}{hash(self._meta)}" if (link or meta) else ""
197+
f"{next(_id_generator)}{hash(self._meta)}" if (link or meta) else ""
194198
)
195199
self._hash: Optional[int] = None
196200
self._null = not (self._set_attributes or color or bgcolor or link or meta)
@@ -240,7 +244,7 @@ def from_meta(cls, meta: Optional[Dict[str, Any]]) -> "Style":
240244
style._attributes = 0
241245
style._link = None
242246
style._meta = dumps(meta)
243-
style._link_id = f"{randint(0, 999999)}{hash(style._meta)}"
247+
style._link_id = f"{next(_id_generator)}{hash(style._meta)}"
244248
style._hash = None
245249
style._null = not (meta)
246250
return style
@@ -487,7 +491,7 @@ def without_color(self) -> "Style":
487491
style._attributes = self._attributes
488492
style._set_attributes = self._set_attributes
489493
style._link = self._link
490-
style._link_id = f"{randint(0, 999999)}" if self._link else ""
494+
style._link_id = f"{next(_id_generator)}" if self._link else ""
491495
style._null = False
492496
style._meta = None
493497
style._hash = None
@@ -639,7 +643,7 @@ def copy(self) -> "Style":
639643
style._attributes = self._attributes
640644
style._set_attributes = self._set_attributes
641645
style._link = self._link
642-
style._link_id = f"{randint(0, 999999)}" if self._link else ""
646+
style._link_id = f"{next(_id_generator)}" if self._link else ""
643647
style._hash = self._hash
644648
style._null = False
645649
style._meta = self._meta
@@ -685,7 +689,7 @@ def update_link(self, link: Optional[str] = None) -> "Style":
685689
style._attributes = self._attributes
686690
style._set_attributes = self._set_attributes
687691
style._link = link
688-
style._link_id = f"{randint(0, 999999)}" if link else ""
692+
style._link_id = f"{next(_id_generator)}" if link else ""
689693
style._hash = None
690694
style._null = False
691695
style._meta = self._meta

0 commit comments

Comments
 (0)