truncate_1.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python
  2. """Truncation beautifier function
  3. This simple function attempts to intelligently truncate a given string
  4. """
  5. def trunc(s, min_pos=0, max_pos=75, ellipsis=True):
  6. """Return a nicely shortened string if over a set upper limit
  7. (default 75 characters)
  8. What is nicely shortened? Consider this line from Orwell's 1984...
  9. 0---------1---------2---------3---------4---------5---------6---------7---->
  10. When we are omnipotent we shall have no more need of science. There will be
  11. If the limit is set to 70, a hard truncation would result in...
  12. When we are omnipotent we shall have no more need of science. There wi...
  13. Truncating to the nearest space might be better...
  14. When we are omnipotent we shall have no more need of science. There...
  15. The best truncation would be...
  16. When we are omnipotent we shall have no more need of science...
  17. Therefore, the returned string will be, in priority...
  18. 1. If the string is less than the limit, just return the whole string
  19. 2. If the string has a period, return the string from zero to the first
  20. period from the right
  21. 3. If the string has no period, return the string from zero to the first
  22. space
  23. 4. If there is no space or period in the range return a hard truncation
  24. In all cases, the string returned will have ellipsis appended unless
  25. otherwise specified.
  26. Parameters:
  27. s = string to be truncated as a String
  28. min_pos = minimum character index to return as Integer (returned
  29. string will be at least this long - default 0)
  30. max_pos = maximum character index to return as Integer (returned
  31. string will be at most this long - default 75)
  32. ellipsis = returned string will have an ellipsis appended to it
  33. before it is returned if this is set as Boolean
  34. (default is True)
  35. Returns:
  36. Truncated String
  37. Throws:
  38. ValueError exception if min_pos > max_pos, indicating improper
  39. configuration
  40. Usage:
  41. short_string = trunc(some_long_string)
  42. or
  43. shorter_string = trunc(some_long_string,max_pos=15,ellipsis=False)
  44. """
  45. # Sentinel value -1 returned by String function rfind
  46. NOT_FOUND = -1
  47. # Error message for max smaller than min positional error
  48. ERR_MAXMIN = 'Minimum position cannot be greater than maximum position'
  49. # If the minimum position value is greater than max, throw an exception
  50. if max_pos < min_pos:
  51. raise ValueError(ERR_MAXMIN)
  52. # Change the ellipsis characters here if you want a true ellipsis
  53. if ellipsis:
  54. suffix = '...'
  55. else:
  56. suffix = ''
  57. # Case 1: Return string if it is shorter (or equal to) than the limit
  58. length = len(s)
  59. if length <= max_pos:
  60. return s + suffix
  61. else:
  62. # Case 2: Return it to nearest period if possible
  63. try:
  64. end = s.rindex('.', min_pos, max_pos)
  65. except ValueError:
  66. # Case 3: Return string to nearest space
  67. end = s.rfind(' ', min_pos, max_pos)
  68. if end == NOT_FOUND:
  69. end = max_pos
  70. return s[0:end] + suffix