|
1 | 1 | import sys |
2 | 2 | from functools import lru_cache |
| 3 | +from itertools import count |
3 | 4 | from operator import attrgetter |
4 | 5 | from pickle import dumps, loads |
5 | | -from random import randint |
| 6 | +from time import time |
6 | 7 | from typing import Any, Dict, Iterable, List, Optional, Type, Union, cast |
7 | 8 |
|
8 | 9 | from . import errors |
|
18 | 19 | StyleType = Union[str, "Style"] |
19 | 20 |
|
20 | 21 |
|
| 22 | +_id_generator = count(int(time()) >> 4) |
| 23 | + |
| 24 | + |
21 | 25 | class _Bit: |
22 | 26 | """A descriptor to get/set a style attribute bit.""" |
23 | 27 |
|
@@ -195,7 +199,7 @@ def _make_color(color: Union[Color, str]) -> Color: |
195 | 199 | self._link = link |
196 | 200 | self._meta = None if meta is None else dumps(meta) |
197 | 201 | 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 "" |
199 | 203 | ) |
200 | 204 | self._hash: Optional[int] = None |
201 | 205 | 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": |
245 | 249 | style._attributes = 0 |
246 | 250 | style._link = None |
247 | 251 | 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)}" |
249 | 253 | style._hash = None |
250 | 254 | style._null = not (meta) |
251 | 255 | return style |
@@ -483,7 +487,7 @@ def without_color(self) -> "Style": |
483 | 487 | style._attributes = self._attributes |
484 | 488 | style._set_attributes = self._set_attributes |
485 | 489 | 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 "" |
487 | 491 | style._null = False |
488 | 492 | style._meta = None |
489 | 493 | style._hash = None |
@@ -635,7 +639,7 @@ def copy(self) -> "Style": |
635 | 639 | style._attributes = self._attributes |
636 | 640 | style._set_attributes = self._set_attributes |
637 | 641 | 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 "" |
639 | 643 | style._hash = self._hash |
640 | 644 | style._null = False |
641 | 645 | style._meta = self._meta |
@@ -681,7 +685,7 @@ def update_link(self, link: Optional[str] = None) -> "Style": |
681 | 685 | style._attributes = self._attributes |
682 | 686 | style._set_attributes = self._set_attributes |
683 | 687 | 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 "" |
685 | 689 | style._hash = None |
686 | 690 | style._null = False |
687 | 691 | style._meta = self._meta |
|
0 commit comments