#format wiki #language en #pragma section-numbers off = Core Jython / Python Examples = DocumentationAndEducation <> ---- Examples related to core Jython / Python will be here. Intended for those new to Python / Jython == Print == Print Hello world {{{#!python print 'Hello World' print "Hello World" }}} == JArray == I was curious on how to work with jarray and move the arrays between classes after being filled with values and operated. I think this example explains some of the basics. Of course, there are more pythonic ways to do this. Cheers! Alfonso Reyes {{{#!python from java.lang import Math from jarray import array, zeros import java.util.Random; class ArrayClass: """ This class will fill an array with random numbers and then return the array for further operations """ def __init__(self, elems): self.N = elems systemEnergy = 0.0025 self.v = zeros(self.N, "d") # array of zeros, double type v0 = Math.sqrt(2.0 * systemEnergy / self.N) for i in range(self.N): r = java.util.Random() self.v[i] = v0 * r.nextInt(self.N) # same velocity for all particles def out(self): for i in range(self.N): print i, self.v[i] def get(self): return self.v n = 100 uarr = zeros(n, "d") # array of double to store some operations ac = ArrayClass(n) ac.out() # print the array arr = ac.get() # get the array to start doing some work on it # get a first third of the array members and times 10 print "Get a first third of the array members" for i in range(n/3): uarr[i] = arr[i] * 10 print i, arr[i], uarr[i] }}} == Multidimensional 2-D String Array == In case you need 2-D string arrays this example may help you to start. Cheers! Alfonso Reyes, Houston-Texas {{{#!python from java.lang.reflect import Array import java rows = 3 cols = 3 str2d = java.lang.reflect.Array.newInstance(java.lang.String,[rows, cols]) str2d[0][0] = "python" str2d[1][0] = "jython" str2d[2][0] = "java" str2d[0][1] = "syntax " str2d[1][1] = "strength" str2d[2][1] = "libraries" str2d[0][2] = "unclutter" str2d[1][2] = "combine" str2d[2][2] = "graphics" print str2d print "printing multidimensional array" for i in range(len(str2d)): for j in range(len(str2d[i])): print str2d[i][j]+"\t", print print }}} == Finding methods in a python script == Posted on the Jython list March 18, 2008: This will give you name-method pairs; if you just want the names, call keys() on it. {{{ import inspect def _is_some_method(object): return inspect.ismethod(object) or inspect.ismethoddescriptor(object) def allmethods(cl): methods = {} for key, value in inspect.getmembers(cl, _is_some_method): methods[key] = 1 for base in cl.__bases__: methods.update(allmethods(base)) # all your base are belong to us for key in methods.keys(): methods[key] = getattr(cl, key) return methods }}} Usage example: {{{ >>> allmethods(str).keys() ['__unicode__', 'rjust', 'center', 'endswith', 'decode', 'translate', 'lower', 'encode', 'isunicode', '__setattr__', '__rmul__', '__add__', '__str__', 'istitle', 'lstrip', 'join', '__reduce_ex__', 'startswith', 'isalnum', 'rstrip', '__getattribute__', '__getslice__', '__eq__', '__init__', '__mod__', 'isupper', '__len__', 'rindex', 'isalpha', 'replace', '__ne__', 'strip', 'isspace', 'swapcase', 'split', 'isnumeric', 'capitalize', 'rfind', '__getitem__', '__contains__', 'title', '__ge__', 'index', '__repr__', 'ljust', 'islower', '__le__', 'isdecimal', 'expandtabs', '__delattr__', 'find', 'splitlines', 'count', 'upper', '__gt__', '__reduce__', 'isdigit', '__lt__', 'zfill', '__cmp__', '__mul__', '__hash__'] }}}