Skip to content

Commit

Permalink
add clone for file iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatYYX committed Apr 19, 2017
1 parent 3bd1769 commit 727dbdc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 7 additions & 0 deletions examples/file_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@
type='csv', id_column='id', value_columns=['content']) # field_names=['xx', 'yy']
for id, value in iter:
print id, value

iter2 = iter.copy()
iter3 = iter.copy()
for id, value in iter2:
print id, value
for id, value in iter3:
print id, value
14 changes: 12 additions & 2 deletions rltk/file_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

class FileIterator(object):

_file_path = None
_type = None
_kwargs = {}
_file_handler = None
_count = 0
_type = None

def __init__(self, file_path, type='text', **kwargs):
self._file_handler = open(file_path, 'r')
self._file_path = file_path
self._type = type
self._kwargs = kwargs
self._file_handler = open(file_path, 'r')

if type == 'json_line':
# pre-compile json path, raise exception if not exists
Expand All @@ -30,6 +34,12 @@ def __init__(self, file_path, type='text', **kwargs):
else: # text
self._id_prefix = hashlib.md5(file_path).hexdigest()[:6]

def __copy__(self):
return FileIterator(self._file_path, self._type, **self._kwargs)

def copy(self):
return self.__copy__()

def next(self):
"""
Returns:
Expand Down

0 comments on commit 727dbdc

Please sign in to comment.