show-newlines.py 831 B

12345678910111213141516171819202122232425262728
  1. import inflect
  2. from more_itertools import always_iterable
  3. import jaraco.text
  4. def report_newlines(filename):
  5. r"""
  6. Report the newlines in the indicated file.
  7. >>> tmp_path = getfixture('tmp_path')
  8. >>> filename = tmp_path / 'out.txt'
  9. >>> _ = filename.write_text('foo\nbar\n', newline='', encoding='utf-8')
  10. >>> report_newlines(filename)
  11. newline is '\n'
  12. >>> filename = tmp_path / 'out.txt'
  13. >>> _ = filename.write_text('foo\nbar\r\n', newline='', encoding='utf-8')
  14. >>> report_newlines(filename)
  15. newlines are ('\n', '\r\n')
  16. """
  17. newlines = jaraco.text.read_newlines(filename)
  18. count = len(tuple(always_iterable(newlines)))
  19. engine = inflect.engine()
  20. print(
  21. engine.plural_noun("newline", count),
  22. engine.plural_verb("is", count),
  23. repr(newlines),
  24. )