<s> '''<STR_LIT>''' <EOL> import datetime <EOL> import functools <EOL> import inspect <EOL> import itertools <EOL> import json <EOL> import types <EOL> import xmlrpclib <EOL> import netaddr <EOL> import six <EOL> from heat_cfnclient . openstack . common import timeutils <EOL> _nasty_type_tests = [ inspect . ismodule , inspect . isclass , inspect . ismethod , <EOL> inspect . isfunction , inspect . isgeneratorfunction , <EOL> inspect . isgenerator , inspect . istraceback , inspect . isframe , <EOL> inspect . iscode , inspect . isbuiltin , inspect . isroutine , <EOL> inspect . isabstract ] <EOL> _simple_types = ( types . NoneType , int , basestring , bool , float , long ) <EOL> def to_primitive ( value , convert_instances = False , convert_datetime = True , <EOL> level = <NUM_LIT:0> , max_depth = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , _simple_types ) : <EOL> return value <EOL> if isinstance ( value , datetime . datetime ) : <EOL> if convert_datetime : <EOL> return timeutils . strtime ( value ) <EOL> else : <EOL> return value <EOL> if type ( value ) == itertools . count : <EOL> return six . text_type ( value ) <EOL> if getattr ( value , '<STR_LIT>' , None ) == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> if level > max_depth : <EOL> return '<STR_LIT:?>' <EOL> try : <EOL> recursive = functools . partial ( to_primitive , <EOL> convert_instances = convert_instances , <EOL> convert_datetime = convert_datetime , <EOL> level = level , <EOL> max_depth = max_depth ) <EOL> if isinstance ( value , dict ) : <EOL> return dict ( ( k , recursive ( v ) ) for k , v in value . iteritems ( ) ) <EOL> elif isinstance ( value , ( list , tuple ) ) : <EOL> return [ recursive ( lv ) for lv in value ] <EOL> if isinstance ( value , xmlrpclib . DateTime ) : <EOL> value = datetime . datetime ( * tuple ( value . timetuple ( ) ) [ : <NUM_LIT:6> ] ) <EOL> if convert_datetime and isinstance ( value , datetime . datetime ) : <EOL> return timeutils . strtime ( value ) <EOL> elif hasattr ( value , '<STR_LIT>' ) : <EOL> return recursive ( dict ( value . iteritems ( ) ) , level = level + <NUM_LIT:1> ) <EOL> elif hasattr ( value , '<STR_LIT>' ) : <EOL> return recursive ( list ( value ) ) <EOL> elif convert_instances and hasattr ( value , '<STR_LIT>' ) : <EOL> return recursive ( value . __dict__ , level = level + <NUM_LIT:1> ) <EOL> elif isinstance ( value , netaddr . IPAddress ) : <EOL> return six . text_type ( value ) <EOL> else : <EOL> if any ( test ( value ) for test in _nasty_type_tests ) : <EOL> return six . text_type ( value ) <EOL> return value <EOL> except TypeError : <EOL> return six . text_type ( value ) <EOL> def dumps ( value , default = to_primitive , ** kwargs ) : <EOL> return json . dumps ( value , default = default , ** kwargs ) <EOL> def loads ( s ) : <EOL> return json . loads ( s ) <EOL> def load ( s ) : <EOL> return json . load ( s ) <EOL> try : <EOL> import anyjson <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> anyjson . _modules . append ( ( __name__ , '<STR_LIT>' , TypeError , <EOL> '<STR_LIT>' , ValueError , '<STR_LIT>' ) ) <EOL> anyjson . force_implementation ( __name__ ) </s>
