Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_dunder_iter(self):
class ImplementedIter(DatasetMixin):
def __iter__(self):
return super(ImplementedIter, self).__iter__()
def get_example(self): ...
def __len__(self): ...
d = ImplementedIter()
self.assertListEqual([], list(d))
def test_dunder_len(self):
class ImplementedLen(DatasetMixin):
def __iter__(self): ...
def get_example(self, i): ...
def __len__(self):
return super(ImplementedLen, self).__len__()
d = ImplementedLen()
self.assertEqual(len(d), 0)
def test_get_example(self):
class ImplementedGetExample(DatasetMixin):
def __iter__(self): ...
def get_example(self, i):
return super(ImplementedGetExample, self).get_example(i)
def __len__(self):
return 1
d = ImplementedGetExample()
with self.assertRaises(IndexError):
d[0]
def test_acts_list(self):
self.assertIsInstance([], DatasetMixin)
def test_dunder_subclasshook(self):
class Dummy(DatasetMixin):
def __iter__(self): ...
def get_example(self, i): ...
def __len__(self): ...
self.assertEqual(Dummy.__subclasshook__(Dummy),
NotImplemented)
def test_acts_tuple(self):
self.assertIsInstance((), DatasetMixin)
def __len__(self) -> int:
return 0
@classmethod
def __subclasshook__(cls, C):
if cls is DatasetMixin:
return _check_methods(C, '__iter__', 'get_example', '__len__')
return NotImplemented
DatasetMixin.register(Sequence)
DatasetMixin.register(easyfile.TextFile)
DatasetMixin.register(easyfile.CsvFile)
class Dataset(DatasetMixin):
"""Dataset wrapping ``DatasetMixin`` object.
Args:
dataset (DatasetMixin): ``Sequence``, ``easyfile.TextFile``, or ``easyfile.CsvFile`` object.
"""
def __init__(self,
dataset: DatasetMixin) -> None:
assert isinstance(dataset, DatasetMixin)
self._dataset = dataset
self._length = None
def __iter__(self) -> Iterator[Any]:
yield from self._dataset
raise IndexError
@abstractmethod
def __len__(self) -> int:
return 0
@classmethod
def __subclasshook__(cls, C):
if cls is DatasetMixin:
return _check_methods(C, '__iter__', 'get_example', '__len__')
return NotImplemented
DatasetMixin.register(Sequence)
DatasetMixin.register(easyfile.TextFile)
DatasetMixin.register(easyfile.CsvFile)
class Dataset(DatasetMixin):
"""Dataset wrapping ``DatasetMixin`` object.
Args:
dataset (DatasetMixin): ``Sequence``, ``easyfile.TextFile``, or ``easyfile.CsvFile`` object.
"""
def __init__(self,
dataset: DatasetMixin) -> None:
assert isinstance(dataset, DatasetMixin)
self._dataset = dataset
self._length = None
def __subclasshook__(cls, C):
if cls is DatasetMixin:
return _check_methods(C, '__iter__', 'get_example', '__len__')
return NotImplemented
def __init__(self, *datasets: List[DatasetMixin]) -> None:
assert all(isinstance(d, DatasetMixin) for d in datasets)
self._datasets = datasets
self._length = None