def _getJyDictionary(self, obj): ret = {} found = java.util.HashMap() original = obj if hasattr(obj, '__class__') and obj.__class__ == java.lang.Class: #get info about superclasses classes = [] classes.append(obj) c = obj.getSuperclass() while c != None: classes.append(c) c = c.getSuperclass() #get info about interfaces interfs = [] for obj in classes: interfs.extend(obj.getInterfaces()) classes.extend(interfs) #now is the time when we actually get info on the declared methods and fields for obj in classes: declaredMethods = obj.getDeclaredMethods() declaredFields = obj.getDeclaredFields() for i in xrange(len(declaredMethods)): name = declaredMethods[i].getName() ret[name] = declaredMethods[i].toString() found.put(name, 1) for i in xrange(len(declaredFields)): name = declaredFields[i].getName() found.put(name, 1) #if declaredFields[i].isAccessible(): declaredFields[i].setAccessible(True) #ret[name] = declaredFields[i].get( declaredFields[i] ) try: ret[name] = declaredFields[i].get(original) except: ret[name] = declaredFields[i].toString() #this simple dir does not always get all the info, that's why we have the part before #(e.g.: if we do a dir on String, some methods that are from other interfaces such as #charAt don't appear) try: d = dir(original) for name in d: if found.get(name) is not 1: ret[name] = getattr(original, name) except: #sometimes we're unable to do a dir pass return ret
def array_to_meta_xml(array, name, format): type = array.dtype.kind slice = name l = len(array.shape) # initial load, compute slice if format == '%': if l > 2: slice += '[0]' * (l - 2) for r in xrange(l - 2): array = array[0] if type == 'f': format = '.5f' elif type == 'i' or type == 'u': format = 'd' else: format = 's' else: format = format.replace('%', '') l = len(array.shape) reslice = "" if l > 2: raise Exception("%s has more than 2 dimensions." % slice) elif l == 1: # special case with 1D arrays arr[i, :] - row, but arr[:, i] - column with equal shape and ndim # http://stackoverflow.com/questions/16837946/numpy-a-2-rows-1-column-file-loadtxt-returns-1row-2-columns # explanation: http://stackoverflow.com/questions/15165170/how-do-i-maintain-row-column-orientation-of-vectors-in-numpy?rq=1 # we use kind of a hack - get information about memory from C_CONTIGUOUS is_row = array.flags['C_CONTIGUOUS'] if is_row: rows = 1 cols = min(len(array), MAX_SLICE_SIZE) if cols < len(array): reslice = '[0:%s]' % (cols) array = array[0:cols] else: cols = 1 rows = min(len(array), MAX_SLICE_SIZE) if rows < len(array): reslice = '[0:%s]' % (rows) array = array[0:rows] elif l == 2: rows = min(array.shape[-2], MAX_SLICE_SIZE) cols = min(array.shape[-1], MAX_SLICE_SIZE) if cols < array.shape[-1] or rows < array.shape[-2]: reslice = '[0:%s, 0:%s]' % (rows, cols) array = array[0:rows, 0:cols] # avoid slice duplication if not slice.endswith(reslice): slice += reslice bounds = (0, 0) if type in "biufc": bounds = (array.min(), array.max()) xml = '<array slice=\"%s\" rows=\"%s\" cols=\"%s\" format=\"%s\" type=\"%s\" max=\"%s\" min=\"%s\"/>' % \ (slice, rows, cols, format, type, bounds[1], bounds[0]) return array, xml, rows, cols, format
def get_dictionary(self, obj): ret = {} for i in xrange(len(obj)): ret[ i ] = obj[i] ret['__len__'] = len(obj) return ret
def array_to_xml(array, roffset, coffset, rows, cols, format): xml = "" rows = min(rows, MAXIMUM_ARRAY_SIZE) cols = min(cols, MAXIMUM_ARRAY_SIZE) # there is no obvious rule for slicing (at least 5 choices) if len(array) == 1 and (rows > 1 or cols > 1): array = array[0] if array.size > len(array): array = array[roffset:, coffset:] rows = min(rows, len(array)) cols = min(cols, len(array[0])) if len(array) == 1: array = array[0] elif array.size == len(array): if roffset == 0 and rows == 1: array = array[coffset:] cols = min(cols, len(array)) elif coffset == 0 and cols == 1: array = array[roffset:] rows = min(rows, len(array)) xml += "<arraydata rows=\"%s\" cols=\"%s\"/>" % (rows, cols) for row in xrange(rows): xml += "<row index=\"%s\"/>" % to_string(row) for col in xrange(cols): value = array if rows == 1 or cols == 1: if rows == 1 and cols == 1: value = array[0] else: if rows == 1: dim = col else: dim = row value = array[dim] if "ndarray" in str(type(value)): value = value[0] else: value = array[row][col] value = format % value xml += var_to_xml(value, '') return xml
def set_args(self, frame, recursive=False): self.args = [] code = frame.f_code locals = frame.f_locals for i in xrange(0, code.co_argcount): name = code.co_varnames[i] class_name = get_type_of_value(locals[name], recursive=recursive) self.add_arg(name, class_name)
def get_dictionary(self, obj): ret = {} declaredFields = obj.__class__.getDeclaredFields() for i in xrange(len(declaredFields)): name = declaredFields[i].getName() try: declaredFields[i].setAccessible(True) ret[name] = declaredFields[i].get(obj) except: traceback.print_exc() return ret
def do_find(f, mod): import linecache if inspect.ismodule(mod): return f, 0, 0 lines = linecache.getlines(f) if inspect.isclass(mod): name = mod.__name__ pat = re.compile(r'^\s*class\s*' + name + r'\b') for i in xrange(len(lines)): if pat.match(lines[i]): return f, i, 0 return f, 0, 0 if inspect.ismethod(mod): mod = mod.im_func if inspect.isfunction(mod): try: mod = mod.func_code except AttributeError: mod = mod.__code__ #python 3k if inspect.istraceback(mod): mod = mod.tb_frame if inspect.isframe(mod): mod = mod.f_code if inspect.iscode(mod): if not hasattr(mod, 'co_filename'): return None, 0, 0 if not hasattr(mod, 'co_firstlineno'): return mod.co_filename, 0, 0 lnum = mod.co_firstlineno pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') while lnum > 0: if pat.match(lines[lnum]): break lnum -= 1 return f, lnum, 0 raise RuntimeError('Do not know about: ' + f + ' ' + str(mod))
def create_signature(self, frame, with_args=True): try: code = frame.f_code locals = frame.f_locals filename, modulename, funcname = self.file_module_function_of( frame) res = Signature(filename, funcname) if with_args: for i in xrange(0, code.co_argcount): name = code.co_varnames[i] class_name = get_type_of_value(locals[name]) res.add_arg(name, class_name) return res except: import traceback traceback.print_exc()
def create_signature(self, frame): try: code = frame.f_code locals = frame.f_locals filename, modulename, funcname = self.file_module_function_of(frame) res = Signature(filename, funcname) for i in xrange(0, code.co_argcount): name = code.co_varnames[i] tp = type(locals[name]) class_name = tp.__name__ if class_name == 'instance': # old-style classes tp = locals[name].__class__ class_name = tp.__name__ if hasattr(tp, '__module__') and tp.__module__ and tp.__module__ not in self._ignore_module_name: class_name = "%s.%s"%(tp.__module__, class_name) res.add_arg(name, class_name) return res except: import traceback traceback.print_exc()
def getVariable(thread_id, frame_id, scope, attrs): """ returns the value of a variable :scope: can be BY_ID, EXPRESSION, GLOBAL, LOCAL, FRAME BY_ID means we'll traverse the list of all objects alive to get the object. :attrs: after reaching the proper scope, we have to get the attributes until we find the proper location (i.e.: obj\tattr1\tattr2). :note: when BY_ID is used, the frame_id is considered the id of the object to find and not the frame (as we don't care about the frame in this case). """ if scope == 'BY_ID': if thread_id != get_thread_id(threading.currentThread()): raise VariableError("getVariable: must execute on same thread") try: import gc objects = gc.get_objects() except: pass # Not all python variants have it. else: frame_id = int(frame_id) for var in objects: if id(var) == frame_id: if attrs is not None: attrList = attrs.split('\t') for k in attrList: _type, _typeName, resolver = get_type(var) var = resolver.resolve(var, k) return var # If it didn't return previously, we coudn't find it by id (i.e.: alrceady garbage collected). sys.stderr.write('Unable to find object with id: %s\n' % (frame_id, )) return None frame = find_frame(thread_id, frame_id) if frame is None: return {} if attrs is not None: attrList = attrs.split('\t') else: attrList = [] for attr in attrList: attr.replace("@_@TAB_CHAR@_@", '\t') if scope == 'EXPRESSION': for count in xrange(len(attrList)): if count == 0: # An Expression can be in any scope (globals/locals), therefore it needs to evaluated as an expression var = evaluate_expression(thread_id, frame_id, attrList[count], False) else: _type, _typeName, resolver = get_type(var) var = resolver.resolve(var, attrList[count]) else: if scope == "GLOBAL": var = frame.f_globals del attrList[ 0] # globals are special, and they get a single dummy unused attribute else: # in a frame access both locals and globals as Python does var = {} var.update(frame.f_globals) var.update(frame.f_locals) for k in attrList: _type, _typeName, resolver = get_type(var) var = resolver.resolve(var, k) return var
def getVariable(thread_id, frame_id, scope, attrs): """ returns the value of a variable :scope: can be BY_ID, EXPRESSION, GLOBAL, LOCAL, FRAME BY_ID means we'll traverse the list of all objects alive to get the object. :attrs: after reaching the proper scope, we have to get the attributes until we find the proper location (i.e.: obj\tattr1\tattr2) :note: when BY_ID is used, the frame_id is considered the id of the object to find and not the frame (as we don't care about the frame in this case). """ if scope == 'BY_ID': if thread_id != get_thread_id(threading.currentThread()): raise VariableError("getVariable: must execute on same thread") try: import gc objects = gc.get_objects() except: pass # Not all python variants have it. else: frame_id = int(frame_id) for var in objects: if id(var) == frame_id: if attrs is not None: attrList = attrs.split('\t') for k in attrList: _type, _typeName, resolver = get_type(var) var = resolver.resolve(var, k) return var # If it didn't return previously, we coudn't find it by id (i.e.: alrceady garbage collected). sys.stderr.write('Unable to find object with id: %s\n' % (frame_id,)) return None frame = find_frame(thread_id, frame_id) if frame is None: return {} if attrs is not None: attrList = attrs.split('\t') else: attrList = [] for attr in attrList: attr.replace("@_@TAB_CHAR@_@", '\t') if scope == 'EXPRESSION': for count in xrange(len(attrList)): if count == 0: # An Expression can be in any scope (globals/locals), therefore it needs to evaluated as an expression var = evaluate_expression(thread_id, frame_id, attrList[count], False) else: _type, _typeName, resolver = get_type(var) var = resolver.resolve(var, attrList[count]) else: if scope == "GLOBAL": var = frame.f_globals del attrList[0] # globals are special, and they get a single dummy unused attribute else: # in a frame access both locals and globals as Python does var = {} var.update(frame.f_globals) var.update(frame.f_locals) for k in attrList: _type, _typeName, resolver = get_type(var) var = resolver.resolve(var, k) return var
def dataframe_to_xml(df, name, roffset, coffset, rows, cols, format): """ :type df: pandas.core.frame.DataFrame :type name: str :type coffset: int :type roffset: int :type rows: int :type cols: int :type format: str """ num_rows = min(df.shape[0], MAX_SLICE_SIZE) num_cols = min(df.shape[1], MAX_SLICE_SIZE) if (num_rows, num_cols) != df.shape: df = df.iloc[0:num_rows, 0:num_cols] slice = '.iloc[0:%s, 0:%s]' % (num_rows, num_cols) else: slice = '' slice = name + slice xml = '<array slice=\"%s\" rows=\"%s\" cols=\"%s\" format=\"\" type=\"\" max=\"0\" min=\"0\"/>\n' % \ (slice, num_rows, num_cols) if (rows, cols) == (-1, -1): rows, cols = num_rows, num_cols rows = min(rows, MAXIMUM_ARRAY_SIZE) cols = min(min(cols, MAXIMUM_ARRAY_SIZE), num_cols) # need to precompute column bounds here before slicing! col_bounds = [None] * cols for col in xrange(cols): dtype = df.dtypes.iloc[coffset + col].kind if dtype in "biufc": cvalues = df.iloc[:, coffset + col] bounds = (cvalues.min(), cvalues.max()) else: bounds = (0, 0) col_bounds[col] = bounds df = df.iloc[roffset:roffset + rows, coffset:coffset + cols] rows, cols = df.shape xml += "<headerdata rows=\"%s\" cols=\"%s\">\n" % (rows, cols) format = format.replace('%', '') col_formats = [] get_label = lambda label: str(label) if not isinstance( label, tuple) else '/'.join(map(str, label)) for col in xrange(cols): dtype = df.dtypes.iloc[col].kind if dtype == 'f' and format: fmt = format elif dtype == 'f': fmt = '.5f' elif dtype == 'i' or dtype == 'u': fmt = 'd' else: fmt = 's' col_formats.append('%' + fmt) bounds = col_bounds[col] xml += '<colheader index=\"%s\" label=\"%s\" type=\"%s\" format=\"%s\" max=\"%s\" min=\"%s\" />\n' % \ (str(col), get_label(df.axes[1].values[col]), dtype, fmt, bounds[1], bounds[0]) for row, label in enumerate(iter(df.axes[0])): xml += "<rowheader index=\"%s\" label = \"%s\"/>\n" % \ (str(row), get_label(label)) xml += "</headerdata>\n" xml += "<arraydata rows=\"%s\" cols=\"%s\"/>\n" % (rows, cols) for row in xrange(rows): xml += "<row index=\"%s\"/>\n" % str(row) for col in xrange(cols): value = df.iat[row, col] value = col_formats[col] % value xml += var_to_xml(value, '') return xml
def dataframe_to_xml(df, name, roffset, coffset, rows, cols, format): """ :type df: pandas.core.frame.DataFrame :type name: str :type coffset: int :type roffset: int :type rows: int :type cols: int :type format: str """ num_rows = min(df.shape[0], MAX_SLICE_SIZE) num_cols = min(df.shape[1], MAX_SLICE_SIZE) if (num_rows, num_cols) != df.shape: df = df.iloc[0:num_rows, 0: num_cols] slice = '.iloc[0:%s, 0:%s]' % (num_rows, num_cols) else: slice = '' slice = name + slice xml = '<array slice=\"%s\" rows=\"%s\" cols=\"%s\" format=\"\" type=\"\" max=\"0\" min=\"0\"/>\n' % \ (slice, num_rows, num_cols) if (rows, cols) == (-1, -1): rows, cols = num_rows, num_cols rows = min(rows, MAXIMUM_ARRAY_SIZE) cols = min(min(cols, MAXIMUM_ARRAY_SIZE), num_cols) # need to precompute column bounds here before slicing! col_bounds = [None] * cols for col in xrange(cols): dtype = df.dtypes.iloc[coffset + col].kind if dtype in "biufc": cvalues = df.iloc[:, coffset + col] bounds = (cvalues.min(), cvalues.max()) else: bounds = (0, 0) col_bounds[col] = bounds df = df.iloc[roffset: roffset + rows, coffset: coffset + cols] rows, cols = df.shape xml += "<headerdata rows=\"%s\" cols=\"%s\">\n" % (rows, cols) format = format.replace('%', '') col_formats = [] get_label = lambda label: str(label) if not isinstance(label, tuple) else '/'.join(map(str, label)) for col in xrange(cols): dtype = df.dtypes.iloc[col].kind if dtype == 'f' and format: fmt = format elif dtype == 'f': fmt = '.5f' elif dtype == 'i' or dtype == 'u': fmt= 'd' else: fmt= 's' col_formats.append('%' + fmt) bounds = col_bounds[col] xml += '<colheader index=\"%s\" label=\"%s\" type=\"%s\" format=\"%s\" max=\"%s\" min=\"%s\" />\n' % \ (str(col), get_label(df.axes[1].values[col]), dtype, fmt, bounds[1], bounds[0]) for row, label in enumerate(iter(df.axes[0])): xml += "<rowheader index=\"%s\" label = \"%s\"/>\n" % \ (str(row), get_label(label)) xml += "</headerdata>\n" xml += "<arraydata rows=\"%s\" cols=\"%s\"/>\n" % (rows, cols) for row in xrange(rows): xml += "<row index=\"%s\"/>\n" % str(row) for col in xrange(cols): value = df.iat[row, col] value = col_formats[col] % value xml += var_to_xml(value, '') return xml