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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  1. from typing import Union, Mapping, Dict
  2. class AbstractFactory:
  3. """
  4. An abstract factory to generate an object from a string, a dictionary or a list
  5. """
  6. def get(self, conf: Union[str, dict, list]):
  7. """
  8. Get an instantiated object.
  9. :param conf: a configuration
  10. if string - assumed to be a type name (not the real name, but a name defined in the Factory)
  11. if dictionary - assumed to be {type_name(str): {parameters...}} (single item in dict)
  12. if list - assumed to be a list of the two options above
  13. If provided value is not one of the three above, the value will be returned as is
  14. """
  15. raise NotImplementedError
  16. class BaseFactory(AbstractFactory):
  17. """
  18. The basic factory fo a *single* object generation.
  19. """
  20. def __init__(self, type_dict: Dict[str, type]):
  21. """
  22. :param type_dict: a dictionary mapping a name to a type
  23. """
  24. self.type_dict = type_dict
  25. def get(self, conf: Union[str, dict]):
  26. """
  27. Get an instantiated object.
  28. :param conf: a configuration
  29. if string - assumed to be a type name (not the real name, but a name defined in the Factory)
  30. if dictionary - assumed to be {type_name(str): {parameters...}} (single item in dict)
  31. If provided value is not one of the three above, the value will be returned as is
  32. """
  33. if isinstance(conf, str):
  34. if conf in self.type_dict:
  35. return self.type_dict[conf]()
  36. else:
  37. raise RuntimeError(f"Unknown object type: {conf} in configuration. valid types are: {self.type_dict.keys()}")
  38. elif isinstance(conf, Mapping):
  39. if len(conf.keys()) > 1:
  40. raise RuntimeError("Malformed object definition in configuration. Expecting either a string of object type or a single entry dictionary"
  41. "{type_name(str): {parameters...}}."
  42. f"received: {conf}")
  43. _type = list(conf.keys())[0] # THE TYPE NAME
  44. _params = list(conf.values())[0] # A DICT CONTAINING THE PARAMETERS FOR INIT
  45. if _type in self.type_dict:
  46. return self.type_dict[_type](**_params)
  47. else:
  48. raise RuntimeError(f"Unknown object type: {_type} in configuration. valid types are: {self.type_dict.keys()}")
  49. else:
  50. return conf
Discard
Tip!

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