pincer.utils.snowflake.Snowflake.process_id()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nop 1
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. an uint64),
16
    they are always returned as strings in the HTTP API
17
    to prevent integer overflows in some languages.
18
    """
19
20
    _MAX_VALUE: int = 9223372036854775807
21
    _MIN_VALUE: int = 0
22
23
    def __init__(self, _):
24
        super().__init__()
25
26
        if self < self._MIN_VALUE:
27
            raise ValueError(
28
                "snowflake value should be greater than or equal to 0."
29
            )
30
31
        if self > self._MAX_VALUE:
32
            raise ValueError(
33
                "snowflake value should be less than"
34
                " or equal to 9223372036854775807."
35
            )
36
37
    @classmethod
38
    def __factory__(cls, string: str) -> Snowflake:
39
        # TODO: fix docs
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
40
        """
41
42
        Parameters
43
        ----------
44
        string
45
46
        Returns
47
        -------
48
49
        """
50
        return cls.from_string(string)
51
52
    @classmethod
53
    def from_string(cls, string: str):
54
        """Initialize a new Snowflake from a string.
55
56
        Parameters
57
        ----------
58
        string: :class:`str`
59
            The snowflake as a string.
60
        """
61
        return Snowflake(int(string))
62
63
    @property
64
    def timestamp(self) -> int:
65
        """:class:`int`: Milliseconds since Discord Epoch,
66
        the first second of 2015 or 14200704000000
67
        """
68
        return self >> 22
69
70
    @property
71
    def worker_id(self) -> int:
72
        """:class:`int`: Internal worker ID"""
73
        return (self >> 17) % 16
74
75
    @property
76
    def process_id(self) -> int:
77
        """:class:`int`: Internal process ID"""
78
        return (self >> 12) % 16
79
80
    @property
81
    def increment(self) -> int:
82
        """:class:`int`: For every ID that is generated on that process,
83
        this number is incremented.
84
        """
85
        return self % 2048
86
87
    @property
88
    def unix(self) -> int:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
89
        return self.timestamp + 1420070400000
90