-
Notifications
You must be signed in to change notification settings - Fork 1
/
identifiable.py
51 lines (41 loc) · 1.1 KB
/
identifiable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from abc import ABC
from typing import ClassVar
class Identifiable(ABC):
_default_id: ClassVar[int]
_id: ClassVar[int]
_all: ClassVar[dict]
id: int
def __init_subclass__(cls, /, default_id, **kwargs):
super().__init_subclass__(**kwargs)
cls._default_id = default_id
cls._id = cls._default_id
cls._all = dict()
def __new__(cls, *args, **kwargs):
instance = super(Identifiable, cls).__new__(cls)
instance.id = cls._id
cls._all[cls._id] = instance
cls._id += 1
return instance
@classmethod
def min_id(cls):
"""
Returns the minimum number for the lab IDs (always 200)
"""
return cls._default_id
@classmethod
def max_id(cls):
"""
Returns the maximum number for the lab IDs
"""
return cls._id - 1
@classmethod
def get(cls, id):
"""
Given an ID of a room, return the instance
"""
if id:
return cls._all[id]
else:
return None
def __repr__(self):
return str(self.id)