Skip to content

Commit 1d28207

Browse files
committed
added line count method
1 parent d512834 commit 1d28207

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

randomAccessReader/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def __init__(self, filepath, endline_character='\n'):
2828
self._endline = endline_character
2929
self._lines = self._get_line_data()
3030

31+
@property
32+
def number_of_lines(self):
33+
return len(self._lines)
34+
3135
def _get_line_data(self):
3236
f = open(self._filepath)
3337
lines = []
@@ -36,15 +40,14 @@ def _get_line_data(self):
3640
current_line = 0
3741
while has_more:
3842
current = f.read(1)
39-
if current == '':
40-
has_more = False
41-
continue
4243

43-
if current == self._endline:
44+
if current == self._endline or current == '':
4445
# we've reached the end of the current line
4546
lines.append({"position": start_position, "length": current_line})
4647
start_position += current_line + 1
4748
current_line = 0
49+
if current == '':
50+
has_more = False
4851
continue
4952

5053
current_line += 1

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# Versions should comply with PEP440. For a discussion on single-sourcing
1616
# the version across setup.py and the project code, see
1717
# https://packaging.python.org/en/latest/single_source_version.html
18-
version='0.1.0',
18+
version='0.1.1',
1919

2020
description='A python random access file reader',
2121
long_description=long_description,

tests/tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ def test_text_file(self):
1111
line = reader.get_line(5)
1212
self.assertTrue('Learn Tons of Blogging Tips &Tricks' in line)
1313

14+
def test_line_count(self):
15+
path = os.path.dirname(os.path.abspath(__file__)) + "/test_file.csv"
16+
reader = RandomAccessReader(path)
17+
self.assertTrue(reader.number_of_lines == 34)
18+
1419

1520
class CsvReaderTest(unittest.TestCase):
1621

0 commit comments

Comments
 (0)