'Encodes an object to a string in some encoding, or to Unicode.'

def toEncoding(self, s, encoding=None):

    if isinstance(s, unicode):
        if encoding:
            s = s.encode(encoding)
    elif isinstance(s, str):
        if encoding:
            s = s.encode(encoding)
        else:
            s = unicode(s)
    elif encoding:
        s = self.toEncoding(str(s), encoding)
    else:
        s = unicode(s)
    return s
