-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add reader, csv reader, jsonlines reader
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from rltk.io.reader import Reader | ||
from rltk.io.csv_reader import CSVReader | ||
from rltk.io.jsonlines_reader import JsonLinesReader |
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,32 @@ | ||
import csv | ||
import codecs | ||
|
||
from rltk.io.reader import Reader | ||
|
||
|
||
class CSVReader(Reader): | ||
|
||
def __init__(self, filename, **kwargs): | ||
self._filename = filename | ||
self._kwargs = kwargs | ||
self._loop_count = 0 | ||
|
||
self._file_handler = codecs.open(filename) | ||
self._csv_reader = csv.DictReader(self._file_handler, **kwargs) | ||
|
||
def __next__(self): | ||
for obj in self._csv_reader: | ||
self._loop_count += 1 | ||
yield {t[0]: t[1] for t in obj.items()} | ||
|
||
def __del__(self): | ||
try: | ||
self._file_handler.close() | ||
except: | ||
pass | ||
|
||
def __copy__(self): | ||
new_reader = CSVReader(self._filename, **self._kwargs) | ||
while new_reader._loop_count < self._loop_count: | ||
new_reader.__next__() | ||
return new_reader |
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,36 @@ | ||
import json | ||
import codecs | ||
|
||
from rltk.io.reader import Reader | ||
|
||
|
||
class JsonLinesReader(Reader): | ||
|
||
def __init__(self, filename, ignore_blank_line=True): | ||
self._filename = filename | ||
self._loop_count = 0 | ||
|
||
self._file_handler = codecs.open(filename) | ||
self._ignore_blank_line = ignore_blank_line | ||
|
||
def __next__(self): | ||
for line in self._file_handler: | ||
if line.strip() == '': | ||
if self._ignore_blank_line: | ||
continue | ||
else: | ||
raise ValueError('Blank line detected') | ||
self._loop_count += 1 | ||
yield json.loads(line) | ||
|
||
def __del__(self): | ||
try: | ||
self._file_handler.close() | ||
except: | ||
pass | ||
|
||
def __copy__(self): | ||
new_reader = JsonLinesReader(self._filename, self._ignore_blank_line) | ||
while new_reader._loop_count < self._loop_count: | ||
new_reader.__next__() | ||
return new_reader |
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,21 @@ | ||
import itertools | ||
|
||
|
||
class Reader(object): | ||
def __init__(self): | ||
pass | ||
|
||
def __iter__(self): | ||
return self.__next__() | ||
|
||
def __next__(self): | ||
raise NotImplementedError | ||
|
||
def __del__(self): | ||
pass | ||
|
||
def __copy__(self): | ||
raise NotImplementedError | ||
|
||
def copy(self): | ||
return self.__copy__() |