def tostr(object, encoding=None):
if isinstance(object, basestring):<EOL><INDENT>if encoding is None:<EOL><INDENT>return object<EOL><DEDENT>else:<EOL><INDENT>return object.encode(encoding)<EOL><DEDENT><DEDENT>if isinstance(object, tuple):<EOL><INDENT>s = ['<STR_LIT:(>']<EOL>for item in object:<EOL><INDENT>if isinstance(item, basestring):<EOL><INDENT>s.append(item)<EOL><DEDENT>else:<EOL><INDENT>s.append(tostr(item))<EOL><DEDENT>s.append('<STR_LIT:U+002CU+0020>')<EOL><DEDENT>s.append('<STR_LIT:)>')<EOL>return '<STR_LIT>'.join(s)<EOL><DEDENT>if isinstance(object, list):<EOL><INDENT>s = ['<STR_LIT:[>']<EOL>for item in object:<EOL><INDENT>if isinstance(item, basestring):<EOL><INDENT>s.append(item)<EOL><DEDENT>else:<EOL><INDENT>s.append(tostr(item))<EOL><DEDENT>s.append('<STR_LIT:U+002CU+0020>')<EOL><DEDENT>s.append('<STR_LIT:]>')<EOL>return '<STR_LIT>'.join(s)<EOL><DEDENT>if isinstance(object, dict):<EOL><INDENT>s = ['<STR_LIT:{>']<EOL>for item in object.items():<EOL><INDENT>if isinstance(item[<NUM_LIT:0>], basestring):<EOL><INDENT>s.append(item[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>s.append(tostr(item[<NUM_LIT:0>]))<EOL><DEDENT>s.append('<STR_LIT>')<EOL>if isinstance(item[<NUM_LIT:1>], basestring):<EOL><INDENT>s.append(item[<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>s.append(tostr(item[<NUM_LIT:1>]))<EOL><DEDENT>s.append('<STR_LIT:U+002CU+0020>')<EOL><DEDENT>s.append('<STR_LIT:}>')<EOL>return '<STR_LIT>'.join(s)<EOL><DEDENT>try:<EOL><INDENT>return unicode(object)<EOL><DEDENT>except:<EOL><INDENT>return str(object)<EOL><DEDENT>
get a unicode safe string representation of an object