<s> """<STR_LIT>""" <EOL> import base64 <EOL> import collections <EOL> import io <EOL> import operator <EOL> import time <EOL> import types <EOL> import inspect <EOL> from jsonpickle import tags <EOL> from jsonpickle . compat import set , unicode , long , bytes , PY3 <EOL> if not PY3 : <EOL> import __builtin__ <EOL> SEQUENCES = ( list , set , tuple ) <EOL> SEQUENCES_SET = set ( SEQUENCES ) <EOL> PRIMITIVES = set ( ( unicode , bool , float , int , long ) ) <EOL> def is_type ( obj ) : <EOL> """<STR_LIT>""" <EOL> if PY3 : <EOL> return isinstance ( obj , type ) <EOL> else : <EOL> return isinstance ( obj , ( type , types . ClassType ) ) <EOL> def has_method ( obj , name ) : <EOL> if not hasattr ( obj , name ) : <EOL> return False <EOL> func = getattr ( obj , name ) <EOL> if isinstance ( func , types . BuiltinMethodType ) : <EOL> return True <EOL> if not isinstance ( func , ( types . MethodType , types . FunctionType ) ) : <EOL> return False <EOL> base_type = obj if is_type ( obj ) else obj . __class__ <EOL> original = None <EOL> for subtype in inspect . getmro ( base_type ) : <EOL> original = vars ( subtype ) . get ( name ) <EOL> if original is not None : <EOL> break <EOL> if original is None : <EOL> return False <EOL> if isinstance ( original , staticmethod ) : <EOL> return True <EOL> self_attr = '<STR_LIT>' if PY3 else '<STR_LIT>' <EOL> if not hasattr ( func , self_attr ) : <EOL> return False <EOL> bound_to = getattr ( func , self_attr ) <EOL> if isinstance ( original , classmethod ) : <EOL> return issubclass ( base_type , bound_to ) <EOL> return isinstance ( obj , type ( bound_to ) ) <EOL> def is_object ( obj ) : <EOL> """<STR_LIT>""" <EOL> return ( isinstance ( obj , object ) and <EOL> not isinstance ( obj , ( type , types . FunctionType ) ) ) <EOL> def is_primitive ( obj ) : <EOL> """<STR_LIT>""" <EOL> if obj is None : <EOL> return True <EOL> elif type ( obj ) in PRIMITIVES : <EOL> return True <EOL> return False <EOL> def is_dictionary ( obj ) : <EOL> """<STR_LIT>""" <EOL> return type ( obj ) is dict <EOL> def is_sequence ( obj ) : <EOL> """<STR_LIT>""" <EOL> return type ( obj ) in SEQUENCES_SET <EOL> def is_list ( obj ) : <EOL> """<STR_LIT>""" <EOL> return type ( obj ) is list <EOL> def is_set ( obj ) : <EOL> """<STR_LIT>""" <EOL> return type ( obj ) is set <EOL> def is_bytes ( obj ) : <EOL> """<STR_LIT>""" <EOL> return type ( obj ) is bytes <EOL> def is_unicode ( obj ) : <EOL> """<STR_LIT>""" <EOL> return type ( obj ) is unicode <EOL> def is_tuple ( obj ) : <EOL> """<STR_LIT>""" <EOL> return type ( obj ) is tuple <EOL> def is_dictionary_subclass ( obj ) : <EOL> """<STR_LIT>""" <EOL> return ( hasattr ( obj , '<STR_LIT>' ) and <EOL> issubclass ( obj . __class__ , dict ) and not is_dictionary ( obj ) ) <EOL> def is_sequence_subclass ( obj ) : <EOL> """<STR_LIT>""" <EOL> return ( hasattr ( obj , '<STR_LIT>' ) and <EOL> ( issubclass ( obj . __class__ , SEQUENCES ) or <EOL> is_list_like ( obj ) ) and <EOL> not is_sequence ( obj ) ) <EOL> def is_noncomplex ( obj ) : <EOL> """<STR_LIT>""" <EOL> if type ( obj ) is time . struct_time : <EOL> return True <EOL> return False <EOL> def is_function ( obj ) : <EOL> """<STR_LIT>""" <EOL> if type ( obj ) in ( types . FunctionType , <EOL> types . MethodType , <EOL> types . LambdaType , <EOL> types . BuiltinFunctionType , <EOL> types . BuiltinMethodType ) : <EOL> return True <EOL> if not hasattr ( obj , '<STR_LIT>' ) : <EOL> return False <EOL> module = translate_module_name ( obj . __class__ . __module__ ) <EOL> name = obj . __class__ . __name__ <EOL> return ( module == '<STR_LIT>' and <EOL> name in ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> def is_module_function ( obj ) : <EOL> """<STR_LIT>""" <EOL> return ( hasattr ( obj , '<STR_LIT>' ) and <EOL> isinstance ( obj , types . FunctionType ) and <EOL> hasattr ( obj , '<STR_LIT>' ) and <EOL> hasattr ( obj , '<STR_LIT>' ) and <EOL> obj . __name__ != '<STR_LIT>' ) <EOL> def is_module ( obj ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( obj , types . ModuleType ) <EOL> def is_picklable ( name , value ) : <EOL> """<STR_LIT>""" <EOL> if name in tags . RESERVED : <EOL> return False <EOL> return is_module_function ( value ) or not is_function ( value ) <EOL> def is_installed ( module ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> __import__ ( module ) <EOL> return True <EOL> except ImportError : <EOL> return False <EOL> def is_list_like ( obj ) : <EOL> return hasattr ( obj , '<STR_LIT>' ) and hasattr ( obj , '<STR_LIT>' ) <EOL> def is_iterator ( obj ) : <EOL> is_file = False <EOL> if not PY3 : <EOL> is_file = isinstance ( obj , __builtin__ . file ) <EOL> return ( isinstance ( obj , collections . Iterator ) and <EOL> not isinstance ( obj , io . IOBase ) and not is_file ) <EOL> def is_reducible ( obj ) : <EOL> """<STR_LIT>""" <EOL> return ( not ( is_list ( obj ) or is_list_like ( obj ) or is_primitive ( obj ) or <EOL> is_bytes ( obj ) or is_unicode ( obj ) or <EOL> is_dictionary ( obj ) or is_sequence ( obj ) or is_set ( obj ) or is_tuple ( obj ) or <EOL> is_dictionary_subclass ( obj ) or is_sequence_subclass ( obj ) or is_noncomplex ( obj ) <EOL> or is_function ( obj ) or is_module ( obj ) or type ( obj ) is object or obj is object <EOL> or ( is_type ( obj ) and obj . __module__ == '<STR_LIT>' ) ) ) <EOL> def in_dict ( obj , key , default = False ) : <EOL> """<STR_LIT>""" <EOL> return ( key in obj . __dict__ ) if getattr ( obj , '<STR_LIT>' , None ) else default <EOL> def in_slots ( obj , key , default = False ) : <EOL> """<STR_LIT>""" <EOL> return ( key in obj . __slots__ ) if getattr ( obj , '<STR_LIT>' , None ) else default <EOL> def has_reduce ( obj ) : <EOL> """<STR_LIT>""" <EOL> if not is_reducible ( obj ) or is_type ( obj ) : <EOL> return ( False , False ) <EOL> has_reduce = False <EOL> has_reduce_ex = False <EOL> REDUCE = '<STR_LIT>' <EOL> REDUCE_EX = '<STR_LIT>' <EOL> has_reduce = in_dict ( obj , REDUCE ) <EOL> has_reduce_ex = in_dict ( obj , REDUCE_EX ) <EOL> has_reduce = has_reduce or in_slots ( obj , REDUCE ) <EOL> has_reduce_ex = has_reduce_ex or in_slots ( obj , REDUCE_EX ) <EOL> for base in type ( obj ) . __mro__ : <EOL> if is_reducible ( base ) : <EOL> has_reduce = has_reduce or in_dict ( base , REDUCE ) <EOL> has_reduce_ex = has_reduce_ex or in_dict ( base , REDUCE_EX ) <EOL> if has_reduce_ex and has_reduce_ex : <EOL> return ( True , True ) <EOL> return ( has_reduce , has_reduce_ex ) <EOL> def translate_module_name ( module ) : <EOL> """<STR_LIT>""" <EOL> if ( PY3 and module == '<STR_LIT>' ) or module == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return module <EOL> def untranslate_module_name ( module ) : <EOL> """<STR_LIT>""" <EOL> if PY3 : <EOL> if module == '<STR_LIT>' : <EOL> module = '<STR_LIT>' <EOL> elif module == '<STR_LIT>' : <EOL> module = '<STR_LIT>' <EOL> return module <EOL> def importable_name ( cls ) : <EOL> """<STR_LIT>""" <EOL> name = cls . __name__ <EOL> module = translate_module_name ( cls . __module__ ) <EOL> return '<STR_LIT>' % ( module , name ) <EOL> def b64encode ( data ) : <EOL> payload = base64 . b64encode ( data ) <EOL> if PY3 and type ( payload ) is bytes : <EOL> payload = payload . decode ( '<STR_LIT:ascii>' ) <EOL> return payload <EOL> def b64decode ( payload ) : <EOL> if PY3 and type ( payload ) is not bytes : <EOL> payload = bytes ( payload , '<STR_LIT:ascii>' ) <EOL> return base64 . b64decode ( payload ) <EOL> def itemgetter ( obj , getter = operator . itemgetter ( <NUM_LIT:0> ) ) : <EOL> return unicode ( getter ( obj ) ) </s>
