def unicode_safe_truncate(s, max_length):
 if (not isinstance(s, unicode)):
  s = str(s).decode('utf-8', 'ignore')
 return s.rstrip()[:max_length]

def unicode_safe_truncate ( s , max_length ) : if ( not isinstance ( s , unicode ) ) : s = str ( s ) . decode ( 'utf-8' , 'ignore' ) return s . rstrip ( ) [ : max_length ]
implements unicode-safe truncation and trims whitespace for a given input string .