def testBytesType2(self): ex = self.gateway.jvm.py4j.examples.UTFExample() int_list = [0, 1, 10, 127, 255, 128] a1 = ex.getBytesValue() self.assertTrue(isbytearray(a1)) for i1, i2 in zip(a1, int_list): self.assertEqual(i1, i2)
def testBytesType2(self): ex = self.gateway.jvm.py4j.examples.UTFExample() int_list = [0, 1, 10, 127, 255, 128] a1 = ex.getBytesValue() # Python 2: bytearray (because str is too easy to confuse with normal # strings) # Python 3: bytes (because bytes is closer to the byte[] representation # in Java) self.assertTrue(isbytearray(a1) or ispython3bytestr(a1)) for i1, i2 in zip(a1, int_list): self.assertEqual(i1, i2)
def get_command_part(parameter, python_proxy_pool=None, appendNewLine=True): """Converts a Python object into a string representation respecting the Py4J protocol. For example, the integer `1` is converted to `u'i1'` :param parameter: the object to convert :rtype: the string representing the command part """ command_part = '' if parameter == None: command_part = NULL_TYPE elif isinstance(parameter, bool): command_part = BOOLEAN_TYPE + smart_decode(parameter) elif isinstance(parameter, Decimal): command_part = DECIMAL_TYPE + smart_decode(parameter) elif isinstance(parameter, int) and parameter <= JAVA_MAX_INT: command_part = INTEGER_TYPE + smart_decode(parameter) elif isinstance(parameter, long) or isinstance(parameter, int): command_part = LONG_TYPE + smart_decode(parameter) elif isinstance(parameter, float): command_part = DOUBLE_TYPE + smart_decode(parameter) elif isbytearray(parameter): command_part = BYTES_TYPE + encode_bytearray(parameter) elif ispython3bytestr(parameter): command_part = BYTES_TYPE + encode_bytearray(parameter) elif isinstance(parameter, basestring): command_part = STRING_TYPE + escape_new_line(parameter) elif isinstance(parameter, list): command_part = LIST_TYPE + encode_list(parameter) elif isinstance(parameter, dict): command_part = MAP_TYPE + encode_map(parameter) elif is_python_proxy(parameter): command_part = PYTHON_PROXY_TYPE + python_proxy_pool.put(parameter) for interface in parameter.Java.implements: command_part += ';' + interface else: command_part = REFERENCE_TYPE + parameter._get_object_id() if appendNewLine: command_part += '\n' #print('THIS IS GOING OUT: {0}'.format(command_part)) #print(type(command_part)) #for c in command_part: #print(ord(c)) return command_part
def get_command_part(parameter, python_proxy_pool=None, appendNewLine=True): """Converts a Python object into a string representation respecting the Py4J protocol. For example, the integer `1` is converted to `u'i1'` :param parameter: the object to convert :rtype: the string representing the command part """ command_part = '' if parameter == None: command_part = NULL_TYPE elif isinstance(parameter, bool): command_part = BOOLEAN_TYPE + smart_decode(parameter) elif isinstance(parameter, Decimal): command_part = DECIMAL_TYPE + smart_decode(parameter) elif isinstance(parameter, int) and parameter <= JAVA_MAX_INT: command_part = INTEGER_TYPE + smart_decode(parameter) elif isinstance(parameter, long) or isinstance(parameter, int): command_part = LONG_TYPE + smart_decode(parameter) elif isinstance(parameter, float): command_part = DOUBLE_TYPE + encode_float(parameter) elif isbytearray(parameter): command_part = BYTES_TYPE + encode_bytearray(parameter) elif ispython3bytestr(parameter): command_part = BYTES_TYPE + encode_bytearray(parameter) elif isinstance(parameter, basestring): command_part = STRING_TYPE + escape_new_line(parameter) elif isinstance(parameter, list): command_part = LIST_TYPE + encode_list(parameter) elif isinstance(parameter, dict): command_part = MAP_TYPE + encode_map(parameter) elif is_python_proxy(parameter): command_part = PYTHON_PROXY_TYPE + python_proxy_pool.put(parameter) for interface in parameter.Java.implements: command_part += ';' + interface else: command_part = REFERENCE_TYPE + parameter._get_object_id() if appendNewLine: command_part += '\n' #print('THIS IS GOING OUT: {0}'.format(command_part)) #print(type(command_part)) #for c in command_part: #print(ord(c)) return command_part
def get_command_part(parameter, python_proxy_pool=None): """Converts a Python object into a string representation respecting the Py4J protocol. For example, the integer `1` is converted to `u"i1"` :param parameter: the object to convert :rtype: the string representing the command part """ command_part = "" if parameter is None: command_part = NULL_TYPE elif isinstance(parameter, bool): command_part = BOOLEAN_TYPE + smart_decode(parameter) elif isinstance(parameter, Decimal): command_part = DECIMAL_TYPE + smart_decode(parameter) elif isinstance(parameter, int) and parameter <= JAVA_MAX_INT\ and parameter >= JAVA_MIN_INT: command_part = INTEGER_TYPE + smart_decode(parameter) elif isinstance(parameter, long) or isinstance(parameter, int): command_part = LONG_TYPE + smart_decode(parameter) elif isinstance(parameter, float): command_part = DOUBLE_TYPE + encode_float(parameter) elif isbytearray(parameter): command_part = BYTES_TYPE + encode_bytearray(parameter) elif ispython3bytestr(parameter): command_part = BYTES_TYPE + encode_bytearray(parameter) elif isinstance(parameter, basestring): command_part = STRING_TYPE + escape_new_line(parameter) elif is_python_proxy(parameter): command_part = PYTHON_PROXY_TYPE + python_proxy_pool.put(parameter) for interface in parameter.Java.implements: command_part += ";" + interface else: command_part = REFERENCE_TYPE + parameter._get_object_id() command_part += "\n" return command_part
def can_convert(self, object): # Check for iterator protocol and should not be an instance of byte # array (taken care of by protocol) return hasattr2(object, "__iter__") and not isbytearray(object) and\ not ispython3bytestr(object) and not isinstance(object, basestring)