-
Notifications
You must be signed in to change notification settings - Fork 3
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
…age that keeps only one row in memory! The latter is supplemented by CSV writer.
- Loading branch information
Showing
15 changed files
with
262 additions
and
27 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
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
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,30 @@ | ||
from arekit.common.data.input.providers.columns.base import BaseColumnsProvider | ||
from arekit.common.data.storages.base import BaseRowsStorage | ||
|
||
|
||
class RowCacheStorage(BaseRowsStorage): | ||
""" Row Caching storage kernel, based on python dictionary. | ||
""" | ||
|
||
def __init__(self): | ||
self.__f = None | ||
self.__row_cache = {} | ||
self.__columns = [] | ||
|
||
@property | ||
def RowCache(self): | ||
return self.__row_cache | ||
|
||
def init_empty(self, columns_provider): | ||
assert(isinstance(columns_provider, BaseColumnsProvider)) | ||
for col_name, _ in columns_provider.get_columns_list_with_types(): | ||
self.__columns.append(col_name) | ||
|
||
def iter_column_names(self): | ||
return iter(self.__columns) | ||
|
||
def _set_row_value(self, row_ind, column, value): | ||
self.__row_cache[column] = value | ||
|
||
def _begin_filling_row(self, row_ind): | ||
self.__row_cache.clear() |
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 |
---|---|---|
@@ -1,4 +1,22 @@ | ||
class BaseWriter(object): | ||
|
||
def write(self, storage, target): | ||
def open_target(self, target): | ||
pass | ||
|
||
def commit_line(self, storage): | ||
pass | ||
|
||
def close_target(self): | ||
pass | ||
|
||
def write_all(self, storage, target): | ||
""" Performs the writing process of the whole storage. | ||
The implementation and support of the related operation | ||
may vary and depends on the nature of the storage, which | ||
briefly might keep all the data in memory (available) | ||
or cache only temporary information (unavailable) | ||
storage: BaseRowsStorage | ||
target: str | ||
""" | ||
raise NotImplementedError() |
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,41 @@ | ||
import csv | ||
from arekit.common.data.storages.base import BaseRowsStorage | ||
from arekit.contrib.utils.data.storages.row_cache import RowCacheStorage | ||
from arekit.contrib.utils.data.writers.base import BaseWriter | ||
|
||
|
||
class NativeCsvWriter(BaseWriter): | ||
|
||
def __init__(self, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL): | ||
self.__target_f = None | ||
self.__writer = None | ||
self.__create_writer_func = lambda f: csv.writer( | ||
f, delimiter=delimiter, quotechar=quotechar, quoting=quoting) | ||
|
||
def open_target(self, target): | ||
self.__target_f = open(target, "w") | ||
self.__writer = self.__create_writer_func(self.__target_f) | ||
pass | ||
|
||
def close_target(self): | ||
self.__target_f.close() | ||
|
||
def commit_line(self, storage): | ||
assert(isinstance(storage, RowCacheStorage)) | ||
assert(self.__writer is not None) | ||
line_data = [storage.RowCache[col_name] for col_name in storage.iter_column_names() | ||
if col_name in storage.RowCache] | ||
self.__writer.writerow(line_data) | ||
|
||
def write_all(self, storage, target): | ||
""" Writes all the `storage` rows | ||
into the `target` filepath, formatted as CSV. | ||
""" | ||
assert(isinstance(storage, BaseRowsStorage)) | ||
|
||
with open(target, "w") as f: | ||
writer = self.__create_writer_func(f) | ||
for _, row in storage: | ||
#content = [row[col_name] for col_name in storage.iter_column_names()] | ||
content = [v for v in row] | ||
writer.writerow(content) |
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
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
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
Oops, something went wrong.