def _columns_general(pyl, items, cols): longest = len(max(items, key=real_len)) if not cols: cols = (pyl.wrap_at + 2) / (longest + 2) items = ["{1:<{0}}".format(longest+(len(i)-real_len(i)), i) for i in items] count = len(items) rows = count / cols + int(count % cols != 0) return items, rows, cols
def table(self): '''Generate a mysql like result table''' # more pythonic approach for assigning value to multi vars? header, body, sep = [''], [''], [''] for key, value in self.items(): if isinstance(value, Dataset): value = value.summarize() value = str(value) width = real_len(max(len(key), len(value))) header.append(expand(key, width)) body.append(expand(value, width)) sep.append('-' * (width + TAB_WIDTH)) for i in (header, body, sep): i.append('') header = '|'.join(header) body = '|'.join(body) sep = '+'.join(sep) return '\n'.join((sep, header, sep, body, sep))
def table(self): '''Generate a mysql like result table''' header, body, sep = [''], [], [''] #: find out the max length of word of each column max_len = {} for key in self.header: width = len(str(key)) for line in self.body: if isinstance(line[key], Dataset): value = line[key].summarize() else: value = str(line[key]) width = max(width, len(value)) max_len[key] = real_len(width) #: build the header and sep in the same time header.append(expand(key, max_len[key])) sep.append('-' * (max_len[key] + TAB_WIDTH)) for i in (header, sep): i.append('') header = '|'.join(header) sep = '+'.join(sep) #: build body for line in self.body: b = [''] for key, value in line.items(): if isinstance(value, Dataset): value = value.summarize() else: value = str(value) width = max_len[key] b.append(expand(value, width)) b.append('') body.append('|'.join(b)) #: build table table = [sep, header, sep] for line in body: table.append(line) table.append(sep) return '\n'.join(table)