Repository: activeloopai/Hub
File: deeplake/enterprise/dataloader.py (Python)

Code before:
                self._dataloader = self.__create_dummy_dataloader(
                    dataset,
                    tensors=tensors,
                    raw_tensors=raw_tensors,
                    pil_compressed_tensors=pil_compressed_tensors,
                )
            else:
                if not hasattr(self, "_indra_dataset"):
                    indra_dataset = dataset_to_libdeeplake(dataset)
                else:
                    indra_dataset = self._indra_dataset

                self._dataloader = self.__get_indra_dataloader(
                    indra_dataset,
                    tensors=tensors,
                    raw_tensors=raw_tensors,
                    pil_compressed_tensors=pil_compressed_tensors,
                    json_tensors=json_tensors,
                    list_tensors=list_tensors,
                    htype_dict=htype_dict,
                    ndim_dict=ndim_dict,
                    tensor_info_dict=tensor_info_dict,
                )

        dataset_read(self._orig_dataset)

        if self._internal_iterator is not None:
            self._internal_iterator = iter(self._internal_iterator)

        return self

    def __next__(self):
        if self._dataloader is None:
            self.__iter__()
        if self._internal_iterator is None:
            self._internal_iterator = iter(self._dataloader)
        return next(self._internal_iterator)

    def __del__(self):
        self.close()


def dataloader(
    dataset, ignore_errors: bool = False, verbose: bool = False
) -> DeepLakeDataLoader:
    """Returns a :class:`~deeplake.enterprise.dataloader.DeepLakeDataLoader` object which can be transformed to either pytorch dataloader or numpy.


    Args:
        dataset: :class:`~deeplake.core.dataset.Dataset` object on which dataloader needs to be built
        ignore_errors (bool): If ``True``, the data loader will ignore errors appeared during data iteration otherwise it will collect the statistics and report appeared errors. Default value is ``False``

Review comment (suggestion):
Minor Comment : Whenever the dataloader is getting intialized better to make the _iterator also initialize in that call. Will there be any situation where dataloader got re-initialize and self._iterator is pointing to some value. Then the check will stop self._iterator to get re-initialize again.

Code after:
                    tensors=tensors,
                    raw_tensors=raw_tensors,
                    pil_compressed_tensors=pil_compressed_tensors,
                )
            else:
                if not hasattr(self, "_indra_dataset"):
                    indra_dataset = dataset_to_libdeeplake(dataset)
                else:
                    indra_dataset = self._indra_dataset

                self._dataloader = self.__get_indra_dataloader(
                    indra_dataset,
                    tensors=tensors,
                    raw_tensors=raw_tensors,
                    pil_compressed_tensors=pil_compressed_tensors,
                    json_tensors=json_tensors,
                    list_tensors=list_tensors,
                    htype_dict=htype_dict,
                    ndim_dict=ndim_dict,
                    tensor_info_dict=tensor_info_dict,
                )

        dataset_read(self._orig_dataset)

        if self._iterator is not None:
            self._iterator = iter(self._iterator)

        return self

    def __setattr__(self, attr, val):
        if (
            attr == "_iterator"
            and val is None
            and hasattr(self, "_iterator")
            and self._iterator is not None
        ):
            self._iterator.close()
        else:
            super().__setattr__(attr, val)

    def __next__(self):
        if self._dataloader is None:
            self.__iter__()
        if self._iterator is None:
            self._iterator = iter(self._dataloader)
        return next(self._iterator)

    def __del__(self):
        self.close()