def unicode_truncate(s, length, encoding='utf-8'):
    encoded = s.encode(encoding)[:length]
return encoded.decode(encoding, 'ignore')
Truncating unicode so it fits a maximum size when encoded for wire transfer