def encode_truncate(text, limit, encoding='utf8', return_encoded=True):
 assert ((text is None) or (type(text) == six.text_type))
 assert (type(limit) in six.integer_types)
 assert (limit >= 0)
 if (text is None):
  return
 s = text.encode(encoding)
 if (len(s) > limit):
  s = s[:limit]
  text = s.decode(encoding, 'ignore')
  if return_encoded:
   s = text.encode(encoding)
 if return_encoded:
  return s
 else:
  return text

def encode_truncate ( text , limit , encoding = 'utf8' , return_encoded = True ) : assert ( ( text is None ) or ( type ( text ) == six . text_type ) ) assert ( type ( limit ) in six . integer_types ) assert ( limit >= 0 ) if ( text is None ) : return s = text . encode ( encoding ) if ( len ( s ) > limit ) : s = s [ : limit ] text = s . decode ( encoding , 'ignore' ) if return_encoded : s = text . encode ( encoding ) if return_encoded : return s else : return text
given a string .