Guest User

Untitled

a guest
Jun 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. def smart_str(s, encoding='utf-8', errors='strict', from_encoding='utf-8'):
  2. if type(s) in (int, long, float, types.NoneType):
  3. return str(s)
  4. elif type(s) is str:
  5. if encoding != from_encoding:
  6. return s.decode(from_encoding, errors).encode(encoding, errors)
  7. else:
  8. return s
  9. elif type(s) is unicode:
  10. return s.encode(encoding, errors)
  11. elif hasattr(s, '__str__'):
  12. return smart_str(str(s), encoding, errors, from_encoding)
  13. elif hasattr(s, '__unicode__'):
  14. return smart_str(unicode(s), encoding, errors, from_encoding)
  15. else:
  16. return smart_str(str(s), encoding, errors, from_encoding)
Add Comment
Please, Sign In to add comment