Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions src/tally/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ def summary(text):
word_label = "word" if words == 1 else "words"
character_label = "character" if characters == 1 else "characters"
return f"{words} {word_label}, {characters} {character_label}"


def line_count(text):
"""Return how many lines *text* holds."""
return len(text.split("\n"))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"".split("\n") gives [''], so empty text counts as one line. splitlines() handles that, and Windows line endings too:

Suggested change
return len(text.split("\n"))
if not text:
return 0
return len(text.splitlines())

5 changes: 5 additions & 0 deletions tests/test_tally.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ def test_summary_uses_singulars(self):
self.assertEqual(tally.summary("a"), "1 word, 1 character")


class LineCountTests(unittest.TestCase):
def test_counts_lines(self):
self.assertEqual(tally.line_count("one\ntwo"), 2)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for empty text as well?



if __name__ == "__main__":
unittest.main()
Loading