def encode_data( self, value_IN, encoding_IN = None ): ''' accepts data to be passed with request. If data is a unicode string, encodes it to the encoding passed in. If no encoding passed in, uses the default encoding (UTF-8). ''' # return reference value_OUT = "" # declare variables is_data_unicode = False # see if data is a unicode object. is_data_unicode = StringHelper.is_unicode( value_IN ) if ( is_data_unicode == True ): # yes, its unicode. Encode it. Got an encoding? if ( ( encoding_IN is not None ) and ( encoding_IN != "" ) ): # yes - use it. my_encoding = encoding_IN else: # no - get default. my_encoding = self.get_default_encoding() #-- END check for encoding. --# # Encode. value_OUT = StringHelper.encode_string( value_IN, my_encoding ) else: # not unicode. Use as-is. value_OUT = value_IN #-- END check to see if data is unicode object --# return value_OUT
def print_calais_json( cls, json_IN, logger_IN = None ): ''' Accepts OpenCalais API JSON object, prints selected parts of it to a string variable. Returns that string. ''' # return reference string_OUT = "" # declare variables me = "OpenCalaisV2ApiResponse.print_calais_json()" my_logger = None temp_string = "" properties_to_output_list = [] current_property = "" my_logger = logger_IN # set properties we want to output properties_to_output_list = [ "_type", "_typeGroup", "commonname", "name", "person" ] # loop over the stuff in the response: item_counter = 0 current_container = json_IN # got something in current_container? if ( current_container is not None ): # yes - loop on keys. for item in current_container.keys(): item_counter += 1 temp_string = "==> " + str( item_counter ) + ": " + item + "\n" string_OUT += temp_string if ( my_logger is not None ): my_logger.debug( "In " + me + ": " + temp_string ) #-- END check to see if logger --# # loop over properties that we care about. for current_property in properties_to_output_list: # is property in the current JSON item we are looking at? if ( current_property in current_container[ item ] ): # yes - output. current_property_value = current_container[ item ][ current_property ] # exception handling to try to deal with unicode added in # OpenCalais API version 2. try: # first, try using str() temp_string = str( current_property_value ) except Exception as e: # on exception, try using StringHelper.encode_string() temp_string = StringHelper.encode_string( current_property_value ) #-- END try/except --# temp_string = "----> " + current_property + ": " + temp_string + "\n" string_OUT += temp_string if ( my_logger is not None ): my_logger.debug( "In " + me + ": " + temp_string ) #-- END check to see if logger --# # is it a Quotation or a Person? if ( ( current_property_value == "Quotation" ) or ( current_property_value == "Person" ) ): string_OUT += str( current_container[ item ] ) + "\n" #-- END check to see if type is "Quotation" --# #-- END current_property --# #-- END loop over list of properties we want to output. --# #-- END loop over items --# #-- END check to see if JSON passed in. --# return string_OUT