def do_truncate(s, length=255, killwords=False, end='...'):
 if (len(s) <= length):
  return s
 elif killwords:
  return (s[:length] + end)
 words = s.split(' ')
 result = []
 m = 0
 for word in words:
  m += (len(word) + 1)
  if (m > length):
   break
  result.append(word)
 result.append(end)
 return u' '.join(result)

def do_truncate ( s , length = 255 , killwords = False , end = '...' ) : if ( len ( s ) <= length ) : return s elif killwords : return ( s [ : length ] + end ) words = s . split ( ' ' ) result = [ ] m = 0 for word in words : m += ( len ( word ) + 1 ) if ( m > length ) : break result . append ( word ) result . append ( end ) return u' ' . join ( result )
return a truncated copy of the string .