├── README.md └── tableize ├── __init__.py └── tableize.py /README.md: -------------------------------------------------------------------------------- 1 | ## Deprecated 2 | 3 | This project is no longer maintained. 4 | -------------------------------------------------------------------------------- /tableize/__init__.py: -------------------------------------------------------------------------------- 1 | from tableize import tableize -------------------------------------------------------------------------------- /tableize/tableize.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def longest(columns, x): 4 | l = {} 5 | for x in xrange(x): 6 | try: l[x] = max(len(w) for w in columns[x]) 7 | except: pass 8 | return l 9 | 10 | def resize(length, method, z): 11 | 12 | if method == 'cols': 13 | x = z 14 | y = length/x 15 | if length%x != 0: 16 | y += 1 17 | 18 | elif method == 'rows': 19 | y = z 20 | x = length/y 21 | if length%y != 0: 22 | x += 1 23 | 24 | return x,y 25 | 26 | def columnify(listy, direction, x, y): 27 | 28 | columns = {} 29 | i, j = 0, 0 30 | if direction == 'lr': 31 | for k in xrange(x): 32 | columns[k] = [] 33 | j = i 34 | while(j < len(listy)): 35 | try: columns[k].append(listy[j]) 36 | except: pass 37 | j += x 38 | i += 1 39 | 40 | elif direction == 'tb': 41 | while j < len(listy): 42 | columns[i] = [] 43 | for k in xrange(y): 44 | try: columns[i].append(listy[j]) 45 | except: pass 46 | j += 1 47 | i += 1 48 | 49 | return columns 50 | 51 | class tableize: 52 | 53 | def __init__(self, listy, cols = 0, rows = 0): 54 | 55 | if cols == 0 and rows == 0: 56 | print 'Value Error: Set a value for either cols or rows.\n' 57 | raise ValueError 58 | 59 | if cols != 0 and rows != 0: 60 | print 'Value Error: Set either cols or rows, not both.\n' 61 | raise ValueError 62 | 63 | if cols < 0 or rows < 0: 64 | print 'Value Error: Positive integers only.\n' 65 | raise ValueError 66 | 67 | if cols != 0: 68 | self.method = 'cols' 69 | self.direction = 'lr' 70 | self.x, self.y = resize(len(listy), 'cols', cols) 71 | 72 | elif rows != 0: 73 | self.method = 'rows' 74 | self.direction = 'tb' 75 | self.x, self.y = resize(len(listy), 'rows', rows) 76 | 77 | self.listy = listy 78 | self.columns = columnify(listy, self.direction, self.x, self.y) 79 | 80 | def flip(self): 81 | if self.direction == 'lr': self.direction = 'tb' 82 | elif self.direction == 'tb': self.direction = 'lr' 83 | self.columns = columnify(self.listy, self.direction, self.x, self.y) 84 | 85 | def switch(self): 86 | if self.method == 'cols': 87 | self.method = 'rows' 88 | self.direction = 'tb' 89 | self.x, self.y = resize(len(self.listy), 'rows', self.x) 90 | 91 | elif self.method == 'rows': 92 | self.method = 'cols' 93 | self.direction = 'lr' 94 | self.x, self.y = resize(len(self.listy), 'cols', self.y) 95 | 96 | self.columns = columnify(self.listy, self.direction, self.x, self.y) 97 | 98 | def show(self): 99 | self.text() 100 | 101 | def write(self, filename, bullet = '', spaces = 1, spacer = ' '): 102 | temp = sys.stdout 103 | sys.stdout = open(filename+'.txt', 'w') 104 | self.text(bullet, spaces, spacer) 105 | sys.stdout = temp 106 | 107 | def text(self, bullet = '', spaces = 1, spacer = ' '): 108 | manu = spacer*spaces 109 | l = longest(self.columns, self.x) 110 | for y in xrange(self.y): 111 | for x in xrange(self.x): 112 | try: 113 | auto = spacer*(l[x]-len(self.columns[x][y])) 114 | sys.stdout.write(bullet+self.columns[x][y]+auto+manu) 115 | except: pass 116 | sys.stdout.write('\n') --------------------------------------------------------------------------------