|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding=utf-8 |
| 3 | +""" |
| 4 | +tableformatter supports and has been tested with the following tabular data types: |
| 5 | +* list of lists or another iterable of iterables |
| 6 | +* two-dimensional NumPy arrays |
| 7 | +* NumPy record arrays (names as columns) |
| 8 | +* pandas.DataFrame |
| 9 | +* list or another iterable of arbitrary non-iterable objects (column specifier required) |
| 10 | +* list or another iterable of dicts (dict keys iterated through as rows where each key must be a hashable iterable) |
| 11 | +* dict of iterables (keys as columns) |
| 12 | +
|
| 13 | +This example demonstrates tableformatter working with these data types in the simplest possible manner. |
| 14 | +""" |
| 15 | +from collections import OrderedDict |
| 16 | +import numpy as np |
| 17 | +import pandas as pd |
| 18 | +import tableformatter as tf |
| 19 | + |
| 20 | +iteralbe_of_iterables = [[1, 2, 3, 4], |
| 21 | + [5, 6, 7, 8]] |
| 22 | +print("Data type: iterable of iterables") |
| 23 | +print(iteralbe_of_iterables) |
| 24 | +print(tf.generate_table(iteralbe_of_iterables)) |
| 25 | + |
| 26 | +np_2d_array = np.array([[1, 2, 3, 4], |
| 27 | + [5, 6, 7, 8]]) |
| 28 | +print("Data type: NumPy 2D array") |
| 29 | +print(np_2d_array) |
| 30 | +print(tf.generate_table(np_2d_array)) |
| 31 | + |
| 32 | +np_rec_array = np.rec.array([(1, 2., 'Hello'), |
| 33 | + (2, 3., "World")], |
| 34 | + dtype=[('foo', 'i4'), ('bar', 'f4'), ('baz', 'U10')]) |
| 35 | +print("Data type: Numpy record array") |
| 36 | +print(np_rec_array) |
| 37 | +print(tf.generate_table(np_rec_array)) |
| 38 | + |
| 39 | +d = {'col1': [1, 5], 'col2': [2, 6], 'col3': [3, 7], 'col4': [4, 8]} |
| 40 | +od = OrderedDict(sorted(d.items(), key=lambda t: t[0])) |
| 41 | +pandas_dataframe = pd.DataFrame(data=od) |
| 42 | +print("Data type: Pandas DataFrame") |
| 43 | +print(pandas_dataframe) |
| 44 | +print(tf.generate_table(pandas_dataframe)) |
| 45 | + |
| 46 | +d1 = {1: 'a', 2: 'b', 3: 'c', 4: 'd'} |
| 47 | +d2 = {5: 'e', 6: 'f', 7: 'g', 8: 'h'} |
| 48 | +iterable_of_dicts = [ OrderedDict(sorted(d1.items(), key=lambda t: t[0])), |
| 49 | + OrderedDict(sorted(d2.items(), key=lambda t: t[0]))] |
| 50 | +print("Data type: iterable of dicts (dict keys iterated through as column values)") |
| 51 | +print(iterable_of_dicts) |
| 52 | +print(tf.generate_table(iterable_of_dicts)) |
| 53 | + |
| 54 | +dict_of_iterables = od |
| 55 | +print("Data type: dict of iterables (dict keys iterated through as rows where each key must be a hashable iterable)") |
| 56 | +print(dict_of_iterables) |
| 57 | +print(tf.generate_table(dict_of_iterables)) |
| 58 | + |
| 59 | + |
| 60 | +class MyRowObject(object): |
| 61 | + """Simple object to demonstrate using a list of non-iterable objects with TableFormatter""" |
| 62 | + def __init__(self, field1: int, field2: int, field3: int, field4: int): |
| 63 | + self.field1 = field1 |
| 64 | + self.field2 = field2 |
| 65 | + self._field3 = field3 |
| 66 | + self.field4 = field4 |
| 67 | + |
| 68 | + def get_field3(self): |
| 69 | + """Demonstrates accessing object functions""" |
| 70 | + return self._field3 |
| 71 | + |
| 72 | + |
| 73 | +rows = [MyRowObject(1, 2, 3, 4), |
| 74 | + MyRowObject(5, 6, 7, 8)] |
| 75 | +columns = (tf.Column('Col1', attrib='field1'), |
| 76 | + tf.Column('Col2', attrib='field2'), |
| 77 | + tf.Column('Col3', attrib='get_field3'), |
| 78 | + tf.Column('Col4', attrib='field4')) |
| 79 | +print("Data type: iterable of arbitrary non-iterable objects") |
| 80 | +print(rows) |
| 81 | +print(tf.generate_table(rows, columns)) |
0 commit comments