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

#396 Trainer constructor cleanup

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/SG-266_clean_trainer_ctor
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  1. import inspect
  2. import functools
  3. from typing import Callable
  4. class _ExplicitParamsValidator:
  5. def __init__(self, function: Callable, validation_type: str = 'None'):
  6. """
  7. ExplicitParamsValidator
  8. :param function:
  9. :param validation_type:
  10. """
  11. self.func = function
  12. self.validation_type = validation_type
  13. def __get__(self, obj, type=None):
  14. return functools.partial(self, obj)
  15. def __call__(self, *args, **kwargs):
  16. """
  17. Caller to create the Wrapper for
  18. :param args:
  19. :param kwargs:
  20. :return:
  21. """
  22. if not hasattr(self, 'func'):
  23. self.func = args[0]
  24. return self
  25. return self.validate_explicit_params(*args, **kwargs)
  26. def validate_explicit_params(self, *args, **kwargs):
  27. """
  28. validate_explicit_params - Checks if each of the explicit parameters (The ones the user put in the func/method
  29. call) are not None or Empty - Depending on the instantiation
  30. :param args: The function arguments
  31. :param kwargs: The function Keyword arguments
  32. :return: wrapped function after input params None values validation
  33. """
  34. var_names = inspect.getfullargspec(self.func)[0]
  35. explicit_args_var_names = list(var_names[:len(args)])
  36. # FOR CLASS METHOD REMOVE THE EXPLICIT DEMAND FOR self PARAMETER
  37. for params_list in [explicit_args_var_names, list(kwargs.keys())]:
  38. if 'self' in params_list:
  39. params_list.remove('self')
  40. # FIRST OF ALL HANDLE ALL OF THE KEYWORD ARGUMENTS
  41. for kwarg, value in kwargs.items():
  42. self.__validate_input_param(kwarg, value)
  43. for i, input_param in enumerate(explicit_args_var_names):
  44. self.__validate_input_param(input_param, args[i])
  45. return self.func(*args, **kwargs)
  46. def __validate_input_param(self, input_param, value):
  47. """
  48. __validate_input_param - Validates the input param based on the validation type
  49. in the class constructor
  50. :param input_param:
  51. :param value:
  52. :return:
  53. """
  54. if self.validation_type == 'NoneOrEmpty':
  55. if not value:
  56. raise ValueError('Input param: ' + str(input_param) + ' is Empty')
  57. if value is None:
  58. raise ValueError('Input param: ' + str(input_param) + ' is None')
  59. # WRAPS THE RETRY DECORATOR CLASS TO ENABLE CALLING WITHOUT PARAMS
  60. def explicit_params_validation(function: Callable = None, validation_type: str = 'None'):
  61. if function is not None:
  62. return _ExplicitParamsValidator(function=function)
  63. else:
  64. def wrapper(function):
  65. return _ExplicitParamsValidator(function=function, validation_type=validation_type)
  66. return wrapper
Discard
Tip!

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