nshtrainer 0.1.0__py3-none-any.whl → 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,89 +0,0 @@
1
- from logging import getLogger
2
- from typing import Any
3
-
4
- from typing_extensions import Self, TypeVar, override
5
-
6
- log = getLogger(__name__)
7
-
8
-
9
- class Singleton:
10
- singleton_key = "_singleton_instance"
11
-
12
- @classmethod
13
- def get(cls) -> Self | None:
14
- return getattr(cls, cls.singleton_key, None)
15
-
16
- @classmethod
17
- def set(cls, instance: Self) -> None:
18
- if cls.get() is not None:
19
- log.warning(f"{cls.__qualname__} instance is already set")
20
-
21
- setattr(cls, cls.singleton_key, instance)
22
-
23
- @classmethod
24
- def reset(cls) -> None:
25
- if cls.get() is not None:
26
- delattr(cls, cls.singleton_key)
27
-
28
- @classmethod
29
- def register(cls, instance: Self) -> None:
30
- cls.set(instance)
31
-
32
- def register_self(self):
33
- self.register(self)
34
-
35
- @classmethod
36
- def instance(cls) -> Self:
37
- instance = cls.get()
38
- if instance is None:
39
- raise RuntimeError(f"{cls.__qualname__} instance is not set")
40
-
41
- return instance
42
-
43
- @override
44
- def __init_subclass__(cls, *args, **kwargs) -> None:
45
- super().__init_subclass__(*args, **kwargs)
46
-
47
- cls.reset()
48
-
49
-
50
- T = TypeVar("T", infer_variance=True)
51
-
52
-
53
- class Registry:
54
- _registry: dict[type, Any] = {}
55
-
56
- @staticmethod
57
- def register(cls_: type[T], instance: T):
58
- if not isinstance(instance, cls_):
59
- raise ValueError(f"{instance} is not an instance of {cls_.__qualname__}")
60
-
61
- if cls_ in Registry._registry:
62
- raise ValueError(f"{cls_.__qualname__} is already registered")
63
-
64
- Registry._registry[cls_] = instance
65
-
66
- @staticmethod
67
- def try_get(cls_: type[T]) -> T | None:
68
- return Registry._registry.get(cls_)
69
-
70
- @staticmethod
71
- def get(cls_: type[T]) -> T:
72
- instance = Registry.try_get(cls_)
73
- if instance is None:
74
- raise ValueError(f"{cls_.__qualname__} is not registered")
75
-
76
- return instance
77
-
78
- @staticmethod
79
- def instance(cls_: type[T]) -> T:
80
- return Registry.get(cls_)
81
-
82
- @staticmethod
83
- def reset(cls_: type[T]):
84
- if cls_ in Registry._registry:
85
- del Registry._registry[cls_]
86
-
87
- @staticmethod
88
- def reset_all():
89
- Registry._registry.clear()