-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…t-lazy #62: Fix CSV loader not lazy
- Loading branch information
Showing
3 changed files
with
43 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class LazyFileIterable: | ||
"""LazyFileIterable is an iterable that loads data from a data source lazily.""" | ||
|
||
def __init__(self, loader): | ||
self.__loader = loader | ||
self.__data = None | ||
|
||
def __iter__(self): | ||
self.__load_data() | ||
return iter(self.__data) | ||
|
||
def __getitem__(self, index): | ||
self.__load_data() | ||
return self.__data[index] | ||
|
||
def __len__(self): | ||
self.__load_data() | ||
return len(self.__data) | ||
|
||
def __load_data(self): | ||
"""Loads the data from the data source if it has not been loaded yet.""" | ||
if self.__data is None: | ||
self.__data = self.__loader() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters