truncate_1_1.py 3.0 KB

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