def makeJSON(column_names, data): """ Creates a valid JSON for javascript in the format expected by YUI datatable Input: column_names - list of variables data - list of array data or a list of values Output: result - JSON as an array """ number_of_columns = len(column_names) if len(data) > 0: if isinstance(data[0], list): number_of_rows = len(data[0]) else: number_of_rows = 1 else: number_of_rows = 0 result = "[" for r in range(number_of_rows): result += "{" for c in range(number_of_columns): if isinstance(data[c], list): if len(data[c]) - 1 < r: item = "" # Handle ragged columns else: item = data[c][r] else: item = data[c] value = str(item) # Assume use item as-is if (item is None): value = "" elif isFloats(item) and not isinstance(item, collections.Iterable): if np.isnan(float(item)): value = "" else: value = str(item) elif isStr(item): if item == 'nan': value = "" result += '"' + column_names[c] + '": ' + '`' + value + '`' if c != number_of_columns - 1: result += "," else: result += "}" if r < number_of_rows -1: result += "," result += "]" return result
def _findColumnWithType(self, table, val): # Inputs: table - table being analyzed # val - value whose type is to be matched # Returns the index of the column with the specified type or None # Assumes that the columns are either str or a number def notIsStrs(vals): return not cell_types.isStrs(vals) if cell_types.isStr(val): func = cell_types.isStrs else: func = notIsStrs for column in table.getColumns(): if Table.isNameColumn(column): continue if func(column.getCells()): return table.indexFromColumn(column)
def _findColumnWithType(self, table, val): """ :param Table table: :param object val: :return Column: Assumes that the columns are either str or a number """ def notIsStrs(vals): return not cell_types.isStrs(vals) if cell_types.isStr(val): func = cell_types.isStrs else: func = notIsStrs for column in table.getColumns(): if Table.isNameColumn(column): continue if func(column.getCells()): return column
def makeJSData(data): """ Creates a javascript array by row from the input data, handling columns of different lengths. :param list-of-list-of-object: list of column values :return list-of-list-of-object: list of row values """ def findNumberOfRows(data): number_of_rows = 0 if len(data) > 0: for item in data: number_of_rows = max(number_of_rows, len(item)) return number_of_rows # Initializations number_of_columns = len(data) new_data = [c if isinstance(c, list) else [c] for c in data] number_of_rows = findNumberOfRows(new_data) # Construct the output result = [] for r in range(number_of_rows): row = [] for c in range(number_of_columns): if len(new_data[c]) - 1 < r: item = "" # Handle ragged columns else: item = new_data[c][r] value = str(item) # Assume use item as-is if (item is None): value = "" elif isFloats(item) and not isinstance(item, collections.Iterable): if np.isnan(float(item)): value = "" else: value = str(item) elif isStr(item): if item == 'nan': value = "" row.append(value) result.append(row) return result
def createURL(self, address="dummy", count=None, values=None, names=None): # Input: count - number of variables # names - variable names # values - values to use for each variable # address - URL address # Returns - a URL string with variables in the GET format url = address count = 0 if values is not None: count = len(values) if names is None: names = [] for n in range(count): names.append("var%d" % n) for n in range(count): if n == 0: url += "command?" else: url += "&" if values is None: url += "%s=%d" % (names[n], n) else: if cell_types.isStr(values[n]): url += "%s=%s" % (names[n], values[n]) elif isinstance(values[n], int): url += "%s=%d" % (names[n], values[n]) elif cell_types.isFloats([n]): url += "%s=%f" % (names[n], values[n]) elif values[n] is None: url += "%s=%s" % (names[n], None) elif isinstance(values[n], list): url += "%s=%s" % (names[n], values[n]) else: import pdb pdb.set_trace() UNKNOWN_TYPE return url