|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import abc
-
- class MyBaseLabel(abc.ABC):
- @abc.abstractmethod
- def _int2str_item(self, int_inp):
- pass
-
- @abc.abstractmethod
- def _str2int_item(self, str_inp):
- pass
-
- def int2str(self, _input):
- if isinstance(_input, list):
- return [self._int2str_item(item) for item in _input]
- return self._int2str_item(_input)
-
- def str2int(self, _input):
- if isinstance(_input, list):
- return [self._str2int_item(item) for item in _input]
- return self._str2int_item(_input)
-
- class MyDummyLabel(MyBaseLabel):
- def _int2str_item(self, int_inp):
- return int_inp
-
- def _str2int_item(self, str_inp):
- return str_inp
-
- class MyClassLabel(MyBaseLabel):
- def __init__(self, names):
- self.names = names
-
- def _int2str_item(self, int_inp):
- return self.names[int_inp]
-
- def _str2int_item(self, str_inp):
- if str_inp not in self.names:
- return None
- return self.names.index(str_inp)
-
- class MyRegresionLabel(MyBaseLabel):
- def _int2str_item(self, int_inp):
- return "%.1f" % round(int_inp, 1)
-
- def _str2int_item(self, str_inp):
- try:
- return float(str_inp)
- except ValueError as ex:
- return None
|