Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#378 Feature/sg 281 add kd notebook

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/SG-281-add_kd_notebook
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
  1. class SingletonMeta(type):
  2. """
  3. A Singleton meta class.
  4. A class that derives from this class will have only 1 instance of that type for the process.
  5. """
  6. _instances = {}
  7. def __call__(cls, *args, **kwargs):
  8. if cls not in cls._instances:
  9. cls._instances[cls] = super(SingletonMeta, cls).__call__(*args, **kwargs)
  10. return cls._instances[cls]
  11. class _SingletonWrapper:
  12. """
  13. A singleton wrapper class. Its instances would be created
  14. for each decorated class.
  15. """
  16. def __init__(self, cls):
  17. self.__wrapped__ = cls
  18. self._instance = None
  19. def __call__(self, *args, **kwargs):
  20. """Returns a single instance of decorated class"""
  21. if self._instance is None:
  22. self._instance = self.__wrapped__(*args, **kwargs)
  23. return self._instance
  24. def singleton(cls):
  25. """
  26. A singleton decorator. Returns a wrapper objects. A call on that object
  27. returns a single instance object of decorated class. Use the __wrapped__
  28. attribute to access decorated class directly in unit tests
  29. """
  30. return _SingletonWrapper(cls)
Discard
Tip!

Press p or to see the previous file or, n or to see the next file