def result(): if not cur.description: return None headers = [h[0] for h in cur.description] def zipHeaders(row): d = {} for i, h in enumerate(headers): if h not in d or d[h] is None: d[h] = row[i] elif row[i] and row[i] != d[h]: print('WARNING: query returned multiple columns with the same name (%s) with conflicting data. Taking the data from the first returned column' % h) return d theData = [zipHeaders(row) for row in cur.fetchall()] if scrub is not None: for desc in cur.description: replace = scrub(desc) if replace is not None: for row in theData: row[desc[0]] = replace(row[desc[0]]) if customScrub: for row in theData: for header, fromDbValue in customScrub.items(): if row[header] is not None: try: row[header] = fromDbValue(row[header]) except: pass if indexedResults: return Hierarchy.fromTable(theData, index, set(headers).difference(index)) return DataTable(theData)
def result(): if not cur.description: return None headers = [h[0] for h in cur.description] def zipHeaders(row): d = {} for i, h in enumerate(headers): if h not in d or d[h] is None: d[h] = row[i] elif row[i] and row[i] != d[h]: print( 'WARNING: query returned multiple columns with the same name (%s) with conflicting data. Taking the data from the first returned column' % h) return d theData = [zipHeaders(row) for row in cur.fetchall()] if scrub is not None: for desc in cur.description: replace = scrub(desc) if replace is not None: for row in theData: row[desc[0]] = replace(row[desc[0]]) if customScrub: for row in theData: for header, fromDbValue in customScrub.items(): if row[header] is not None: try: row[header] = fromDbValue(row[header]) except: pass if indexedResults: return Hierarchy.fromTable(theData, index, set(headers).difference(index)) return DataTable(theData)
def index(self, keyHeaders, leafHeaders=None): if leafHeaders is None: leafHeaders = set(self.headers()).difference(keyHeaders) return Hierarchy.fromTable(self, keyHeaders, leafHeaders)