Passed
Push — main ( 13c337...ec726f )
by Yohann
01:49 queued 12s
created

pincer.utils.snowflake.Snowflake.__init__()   A

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nop 2
1
# Copyright Pincer 2021-Present
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# Full MIT License can be found in `LICENSE` at the project root.
3
4
from __future__ import annotations
5
6
7
class Snowflake(int):
8
    """Discord utilizes Twitter's snowflake format for uniquely
9
    identifiable descriptors (IDs).
10
11
    These IDs are guaranteed to be unique across all of Discord,
12
    except in some unique scenarios in which child objects
13
    share their parent's ID.
14
15
    Because Snowflake IDs are up to 64 bits in size (e.g. a uint64),
16
    they are always returned as strings in the HTTP API
17
    to prevent integer overflows in some languages.
18
    """
19
    _MAX_VALUE: int = 9223372036854775807
20
    _MIN_VALUE: int = 0
21
22
    def __init__(self, _):
23
        super().__init__()
24
25
        if self < self._MIN_VALUE:
26
            raise ValueError(
27
                "snowflake value should be greater than or equal to 0."
28
            )
29
30
        if self > self._MAX_VALUE:
31
            raise ValueError(
32
                "snowflake value should be less than"
33
                " or equal to 9223372036854775807."
34
            )
35
36
    @classmethod
37
    def __factory__(cls, string: str) -> Snowflake:
38
        return cls.from_string(string)
39
40
    @classmethod
41
    def from_string(cls, string: str):
42
        """Initialize a new Snowflake from a string.
43
44
        Parameters
45
        ----------
46
        string: :class:`str`
47
            The snowflake as a string.
48
        """
49
        return Snowflake(int(string))
50
51
    @property
52
    def timestamp(self) -> int:
53
        """:class:`int`: Milliseconds since Discord Epoch,
54
        the first second of 2015 or 14200704000000
55
        """
56
        return self >> 22
57
58
    @property
59
    def worker_id(self) -> int:
60
        """:class:`int`: Internal worker ID"""
61
        return (self >> 17) % 16
62
63
    @property
64
    def process_id(self) -> int:
65
        """:class:`int`: Internal process ID"""
66
        return (self >> 12) % 16
67
68
    @property
69
    def increment(self) -> int:
70
        """:class:`int`: For every ID that is generated on that process,
71
        this number is incremented.
72
        """
73
        return self % 2048
74
75
    @property
76
    def unix(self) -> int:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
77
        return self.timestamp + 1420070400000
78