Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Trickybrain Creating the LineReader file #14

Merged
merged 25 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
50c908b
make some changes to edwin-pr
Trickybrain Feb 22, 2024
041af02
Resolved merge conflicts
Trickybrain Feb 22, 2024
f4dfcce
Trickybrain Adding LineReader
Trickybrain Feb 22, 2024
3d675eb
Merge branch 'main' into Trickybrain-LineReader
Trickybrain Feb 22, 2024
9fa6352
Make changes to LineReader, fixing the type issue
Trickybrain Feb 22, 2024
5805966
Merge branch 'Trickybrain-LineReader' of https://github.com/Trickybra…
Trickybrain Feb 22, 2024
2005fdb
Make changes to assigned in python
Trickybrain Feb 22, 2024
888f072
Line Reader Translation
Trickybrain Feb 27, 2024
0759e42
Merge branch 'main' into Trickybrain-LineReader
Trickybrain Feb 27, 2024
74a22fb
importing LineReader at init
Trickybrain Feb 27, 2024
15546f1
Merge branch 'Trickybrain-LineReader' of https://github.com/Trickybra…
Trickybrain Feb 27, 2024
4881e0f
Make changes to LineReader
Trickybrain Feb 27, 2024
2427d97
Make changes to LineReader
Trickybrain Feb 27, 2024
2ac95ec
Merge branch 'main' into Trickybrain-LineReader
Trickybrain Feb 27, 2024
419a64b
Make changes to linereader test
Trickybrain Feb 27, 2024
fe9adc7
Merge branch 'Trickybrain-LineReader' of https://github.com/Trickybra…
Trickybrain Feb 27, 2024
ef49c27
Make changes to LineReader
Trickybrain Feb 27, 2024
013a094
Make changes to the errors
Trickybrain Feb 27, 2024
2d06b69
Merge branch 'main' into Trickybrain-LineReader
Trickybrain Feb 27, 2024
c78c424
Make change LineReader
Trickybrain Feb 27, 2024
5ddbbd3
Merge branch 'Trickybrain-LineReader' of https://github.com/Trickybra…
Trickybrain Feb 27, 2024
4596ba9
Make changes to LineReader and rename LineReader_test
Trickybrain Feb 27, 2024
66aa871
Make detect_newline_type private
Trickybrain Feb 28, 2024
aab9d49
Make changes to detect new line and add get line number
Trickybrain Feb 28, 2024
ab1fe76
We want things to be as private as possible. private is two underscor…
nedtwigg Feb 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions python/selfie-lib/selfie_lib/LineReader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import io


class LineReader:
def __init__(self, content: bytes):
self.__buffer = io.BytesIO(content)
self.__uses_unix_newlines = self.__detect_newline_type()
self.__line_count = 0 # Initialize line count

@classmethod
def for_binary(cls, content: bytes):
return cls(content)

@classmethod
def for_string(cls, content: str):
return cls(content.encode("utf-8"))

def __detect_newline_type(self) -> bool:
first_line = self.__buffer.readline()
self.__buffer.seek(0) # Reset buffer for actual reading
return b"\r\n" not in first_line

def unix_newlines(self) -> bool:
return self.__uses_unix_newlines

def read_line(self) -> str:
line_bytes = self.__buffer.readline()
if line_bytes:
self.__line_count += 1 # Increment line count for each line read
line = line_bytes.decode("utf-8")
return line.rstrip("\r\n" if not self.__uses_unix_newlines else "\n")

# Method to get the current line number
def get_line_number(self) -> int:
return self.__line_count
2 changes: 2 additions & 0 deletions python/selfie-lib/selfie_lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .LineReader import LineReader as LineReader
from .Slice import Slice as Slice

42 changes: 42 additions & 0 deletions python/selfie-lib/tests/LineReader_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from selfie_lib import LineReader

def test_should_find_unix_separator_from_binary():
reader = LineReader.for_binary(b"This is a new line\n")
assert reader.unix_newlines() is True
assert reader.read_line() == "This is a new line"

def test_should_find_windows_separator_from_binary():
reader = LineReader.for_binary(b"This is a new line\r\n")
assert reader.unix_newlines() is False
assert reader.read_line() == "This is a new line"

def test_should_find_unix_separator_from_string():
reader = LineReader.for_string("This is a new line\n")
assert reader.unix_newlines() is True
assert reader.read_line() == "This is a new line"

def test_should_find_windows_separator_from_string():
reader = LineReader.for_string("This is a new line\r\n")
assert reader.unix_newlines() is False
assert reader.read_line() == "This is a new line"

def test_should_get_unix_line_separator_when_there_is_none():
reader = LineReader.for_binary(b"This is a new line")
assert reader.unix_newlines() is True
assert reader.read_line() == "This is a new line"

def test_should_read_next_line_without_problem():
reader = LineReader.for_binary(b"First\r\nSecond\r\n")
assert reader.unix_newlines() is False
assert reader.read_line() == "First"
assert reader.unix_newlines() is False
assert reader.read_line() == "Second"
assert reader.unix_newlines() is False

def test_should_use_first_line_separator_and_ignore_next():
reader = LineReader.for_binary(b"First\r\nAnother separator\n")
assert reader.unix_newlines() is False
assert reader.read_line() == "First"
assert reader.unix_newlines() is False
assert reader.read_line() == "Another separator"
assert reader.unix_newlines() is False