string.py 436 B

12345678910111213141516171819
  1. from typing import (
  2. Any,
  3. )
  4. def abbr(value: Any, limit: int = 79) -> str:
  5. """
  6. Converts a value into its string representation and abbreviates that
  7. representation based on the given length `limit` if necessary.
  8. """
  9. rep = repr(value)
  10. if len(rep) > limit:
  11. if limit < 3:
  12. raise ValueError("Abbreviation limit may not be less than 3")
  13. rep = rep[: limit - 3] + "..."
  14. return rep