def get_unicode_writer(stream=sys.stdout, encoding=None, errors=u'replace'):
 encoding = (encoding or get_preferred_output_encoding())
 if ((sys.version_info < (3,)) or (not hasattr(stream, u'buffer'))):
  return (lambda s: stream.write(s.encode(encoding, errors)))
 else:
  return (lambda s: stream.buffer.write(s.encode(encoding, errors)))

def get_unicode_writer ( stream = sys . stdout , encoding = None , errors = u'replace' ) : encoding = ( encoding or get_preferred_output_encoding ( ) ) if ( ( sys . version_info < ( 3 , ) ) or ( not hasattr ( stream , u'buffer' ) ) ) : return ( lambda s : stream . write ( s . encode ( encoding , errors ) ) ) else : return ( lambda s : stream . buffer . write ( s . encode ( encoding , errors ) ) )
get function which will write unicode string to the given stream writing is done using encoding returned by :py:func:get_preferred_output_encoding .