Skip to content

Commit d72af90

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

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,8 +1,9 @@
11
import sys
22
from functools import lru_cache
3+
from itertools import count
34
from operator import attrgetter
45
from pickle import dumps, loads
5-
from random import randint
6+
from time import time
67
from typing import Any, Dict, Iterable, List, Optional, Type, Union, cast
78

89
from . import errors
@@ -18,6 +19,9 @@
1819
StyleType = Union[str, "Style"]
1920

2021

22+
_id_generator = count(int(time()) >> 4)
23+
24+
2125
class _Bit:
2226
"""A descriptor to get/set a style attribute bit."""
2327

@@ -195,7 +199,7 @@ def _make_color(color: Union[Color, str]) -> Color:
195199
self._link = link
196200
self._meta = None if meta is None else dumps(meta)
197201
self._link_id = (
198-
f"{randint(0, 999999)}{hash(self._meta)}" if (link or meta) else ""
202+
f"{next(_id_generator)}{hash(self._meta)}" if (link or meta) else ""
199203
)
200204
self._hash: Optional[int] = None
201205
self._null = not (self._set_attributes or color or bgcolor or link or meta)
@@ -245,7 +249,7 @@ def from_meta(cls, meta: Optional[Dict[str, Any]]) -> "Style":
245249
style._attributes = 0
246250
style._link = None
247251
style._meta = dumps(meta)
248-
style._link_id = f"{randint(0, 999999)}{hash(style._meta)}"
252+
style._link_id = f"{next(_id_generator)}{hash(style._meta)}"
249253
style._hash = None
250254
style._null = not (meta)
251255
return style
@@ -483,7 +487,7 @@ def without_color(self) -> "Style":
483487
style._attributes = self._attributes
484488
style._set_attributes = self._set_attributes
485489
style._link = self._link
486-
style._link_id = f"{randint(0, 999999)}" if self._link else ""
490+
style._link_id = f"{next(_id_generator)}" if self._link else ""
487491
style._null = False
488492
style._meta = None
489493
style._hash = None
@@ -635,7 +639,7 @@ def copy(self) -> "Style":
635639
style._attributes = self._attributes
636640
style._set_attributes = self._set_attributes
637641
style._link = self._link
638-
style._link_id = f"{randint(0, 999999)}" if self._link else ""
642+
style._link_id = f"{next(_id_generator)}" if self._link else ""
639643
style._hash = self._hash
640644
style._null = False
641645
style._meta = self._meta
@@ -681,7 +685,7 @@ def update_link(self, link: Optional[str] = None) -> "Style":
681685
style._attributes = self._attributes
682686
style._set_attributes = self._set_attributes
683687
style._link = link
684-
style._link_id = f"{randint(0, 999999)}" if link else ""
688+
style._link_id = f"{next(_id_generator)}" if link else ""
685689
style._hash = None
686690
style._null = False
687691
style._meta = self._meta

0 commit comments

Comments
 (0)