def testMixed(self): d = {'a':'b', 'c':'d'} l = ['e', 'f', 'g', 'h'] s = set(['i', 'j', 'k']) # mixed types in a dictionary md = {'d': d, 'l': l, 's': s, 'str': 'hello'} jmd = to_java(md) self.assertEqual(len(md), jmd.size()) for k, v in md.items(): jk = to_java(k) self.assertTrue(jmd.containsKey(jk)) self.assertEqual(v, to_python(jmd.get(jk))) pmd = to_python(jmd) self.assertEqual(md, pmd) self.assertEqual(str(md), str(pmd)) # mixed types in a list ml = [d, l, s, 'hello'] jml = to_java(ml) for e, a in zip(ml, jml): self.assertEqual(e, to_python(a)) pml = to_python(jml) self.assertEqual(ml, pml) self.assertEqual(str(ml), str(pml))
def testDict(self): d = { 'access_log': [ {'stored_proc': 'getsomething'}, {'uses': [ {'usedin': 'some->bread->crumb'}, {'usedin': 'something else here'}, {'stored_proc': 'anothersp'} ]}, {'uses': [ {'usedin': 'blahblah'} ]} ], 'reporting': [ {'stored_proc': 'reportingsp'}, {'uses': [{'usedin': 'breadcrumb'}]} ] } jd = to_java(d) self.assertEqual(len(d), jd.size()) for k, v in d.items(): jk = to_java(k) self.assertTrue(jd.containsKey(jk)) self.assertEqual(v, to_python(jd.get(jk))) pd = to_python(jd) self.assertEqual(d, pd) self.assertEqual(str(d), str(pd))
def testList(self): l = 'The quick brown fox jumps over the lazy dogs'.split() jl = to_java(l) for e, a in zip(l, jl): self.assertEqual(e, to_python(a)) pl = to_python(jl) self.assertEqual(l, pl) self.assertEqual(str(l), str(pl)) self.assertEqual(pl[1], 'quick') pl[7] = 'silly' self.assertEqual('The quick brown fox jumps over the silly dogs', ' '.join(pl))
def testBoolean(self): jt = to_java(True) self.assertEqual(True, jt.booleanValue()) pt = to_python(jt) self.assertEqual(True, pt) self.assertEqual('True', str(pt)) jf = to_java(False) self.assertEqual(False, jf.booleanValue()) pf = to_python(jf) self.assertEqual(False, pf) self.assertEqual('False', str(pf))
def testGentle(self): Object = jimport('java.lang.Object') unknown_thing = Object() converted_thing = to_python(unknown_thing, gentle=True) assert type(converted_thing) == Object bad_conversion = None try: bad_conversion = to_python(unknown_thing) except: # NB: Failure is expected here. pass self.assertIsNone(bad_conversion)
def testBigInteger(self): bi = 9879999999999999789 jbi = to_java(bi) self.assertEqual(bi, int(str(jbi.toString()))) pbi = to_python(jbi) self.assertEqual(bi, pbi) self.assertEqual(str(bi), str(pbi))
def testLong(self): l = 4000000001 jl = to_java(l) self.assertEqual(l, jl.longValue()) pl = to_python(jl) self.assertEqual(l, pl) self.assertEqual(str(l), str(pl))
def testInteger(self): i = 5 ji = to_java(i) self.assertEqual(i, ji.intValue()) pi = to_python(ji) self.assertEqual(i, pi) self.assertEqual(str(i), str(pi))
def testString(self): s = 'Hello world!' js = to_java(s) for e, a in zip(s, js.toCharArray()): self.assertEqual(e, a) ps = to_python(js) self.assertEqual(s, ps) self.assertEqual(str(s), str(ps))
def testSet(self): s = set(['orange', 'apple', 'pineapple', 'plum']) js = to_java(s) self.assertEqual(len(s), js.size()) for e in s: self.assertTrue(js.contains(to_java(e))) ps = to_python(js) self.assertEqual(s, ps) self.assertEqual(str(s), str(ps))
def testNone(self): d = {'key':None, None:'value', 'foo':'bar'} jd = to_java(d) self.assertEqual(3, jd.size()) self.assertEqual(None, jd.get('key')) self.assertEqual('value', jd.get(None)) self.assertEqual('bar', jd.get('foo')) pd = to_python(jd) self.assertEqual(d, pd)
def from_java(self, data): """ Converts the data into a python equivalent """ if not isjava(data): return data if self._ij.convert().supports(data, Dataset): # HACK: Converter exists for ImagePlus -> Dataset, but not ImagePlus -> RAI. data = self._ij.convert().convert(data, Dataset) if (self._ij.convert().supports(data, RandomAccessibleInterval)): rai = self._ij.convert().convert(data, RandomAccessibleInterval) return self.rai_to_numpy(rai) return to_python(data)
def from_java(self, data): """ Converts the data into a python equivalent """ # todo: convert a datset to xarray if not isjava(data): return data try: if self._ij.convert().supports(data, Dataset): # HACK: Converter exists for ImagePlus -> Dataset, but not ImagePlus -> RAI. data = self._ij.convert().convert(data, Dataset) return self._dataset_to_xarray(data) if self._ij.convert().supports(data, RandomAccessibleInterval): rai = self._ij.convert().convert(data, RandomAccessibleInterval) return self.rai_to_numpy(rai) except Exception as exc: _dump_exception(exc) raise exc return to_python(data)
def testStructureWithSomeUnsupportedItems(self): # Create Java data structure with some challenging items. Object = jimport('java.lang.Object') jmap = to_java({ 'list': ['a', Object(), 1], 'set': {'x', Object(), 2}, 'object': Object(), 'foo': 'bar' }) self.assertEqual('java.util.LinkedHashMap', jclass(jmap).getName()) # Convert it back to Python. pdict = to_python(jmap) l = pdict['list'] self.assertEqual(pdict['list'][0], 'a') assert type(pdict['list'][1]) == Object assert pdict['list'][2] == 1 assert 'x' in pdict['set'] assert 2 in pdict['set'] assert len(pdict['set']) == 3 assert type(pdict['object']) == Object self.assertEqual(pdict['foo'], 'bar')
def _parser(self) -> None: """"A method to parse imagej ops help and extract imagej op information. This method utilizes the python re module to parse the imagej instance ops help. The method then instantiates class Op and class Plugin objects to store information about the ops and methods. Finally relevant information about the ops and methods is written to the log file. Args: None Returns: None Raises: None """ # Get list of all available ops to be converted to plugins plugins = scyjava.to_python(self._ij.op().ops().iterator()) # Complile the regular expression search pattern for op overloading # methods re_path = re.compile(r'\t(?P<path>.*\.)(?P<name>.*)(?=\()') # Coompile the regular expression search pattern for the input data # types and title re_inputs = re.compile(r'(?<=\t\t)(.*?)\s(.*)(?=,|\))') # Complile the regular expression search pattern for the outputs re_output = re.compile(r'^\((.*?)\s(.*)\)') # Create a counter for number of ops parsed ops_count = 0 # Iterate over all ops for plugin in plugins: # Add the plugin to the dictionary self._plugins[plugin] = Plugin(plugin) # Get the help info about the plugin/op op_docs = scyjava.to_python(self._ij.op().help(plugin)) # Split the help string into seperate ops split_ops = re.split(r'\t(?=\()', op_docs) # Iterate over all op methods in the plugin/op for op_doc in split_ops[1:]: # Increment the ops parsed count ops_count += 1 # Search for op path and name op_path = re_path.search(op_doc).groupdict() # Save op name and full path name = op_path['name'] full_path = op_path['path'] + name # Find all inputs inputs = re_inputs.findall(op_doc) # Search for output output = re_output.findall(op_doc)[0] # Create an Op object to store the op data op = Op(plugin, name, full_path, inputs, output) # Check if the op is supported if op.partial_support: support_msg = True else: support_msg = op.support_msg # Log the plugin info to the main log self._logger.info( self._msg.format(counter=ops_count, plugin=plugin, name=name, full_path=full_path, inputs=op._inputs, output=op._output, support=support_msg)) # Add the overlaoding method to the plugin/op self._plugins[plugin].add_op(op)