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