|
1 | 1 | import sys |
2 | 2 | from functools import lru_cache |
| 3 | +from itertools import count |
3 | 4 | from marshal import dumps, loads |
4 | | -from random import randint |
| 5 | +from time import time |
5 | 6 | from typing import Any, Dict, Iterable, List, Optional, Type, Union, cast |
6 | 7 |
|
7 | 8 | from . import errors |
|
13 | 14 | StyleType = Union[str, "Style"] |
14 | 15 |
|
15 | 16 |
|
| 17 | +_id_generator = count(int(time()) >> 4) |
| 18 | + |
| 19 | + |
16 | 20 | class _Bit: |
17 | 21 | """A descriptor to get/set a style attribute bit.""" |
18 | 22 |
|
@@ -190,7 +194,7 @@ def _make_color(color: Union[Color, str]) -> Color: |
190 | 194 | self._link = link |
191 | 195 | self._meta = None if meta is None else dumps(meta) |
192 | 196 | 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 "" |
194 | 198 | ) |
195 | 199 | self._hash: Optional[int] = None |
196 | 200 | 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": |
240 | 244 | style._attributes = 0 |
241 | 245 | style._link = None |
242 | 246 | 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)}" |
244 | 248 | style._hash = None |
245 | 249 | style._null = not (meta) |
246 | 250 | return style |
@@ -487,7 +491,7 @@ def without_color(self) -> "Style": |
487 | 491 | style._attributes = self._attributes |
488 | 492 | style._set_attributes = self._set_attributes |
489 | 493 | 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 "" |
491 | 495 | style._null = False |
492 | 496 | style._meta = None |
493 | 497 | style._hash = None |
@@ -639,7 +643,7 @@ def copy(self) -> "Style": |
639 | 643 | style._attributes = self._attributes |
640 | 644 | style._set_attributes = self._set_attributes |
641 | 645 | 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 "" |
643 | 647 | style._hash = self._hash |
644 | 648 | style._null = False |
645 | 649 | style._meta = self._meta |
@@ -685,7 +689,7 @@ def update_link(self, link: Optional[str] = None) -> "Style": |
685 | 689 | style._attributes = self._attributes |
686 | 690 | style._set_attributes = self._set_attributes |
687 | 691 | 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 "" |
689 | 693 | style._hash = None |
690 | 694 | style._null = False |
691 | 695 | style._meta = self._meta |
|
0 commit comments