#!/usr/bin/env python import sys from the_semantic_db_code import * from the_semantic_db_functions import * from the_semantic_db_processor import * C = context_list("column merge") def normalize_column(s,n): lines = s.split('\n') if len(lines) < n: lines += [''] * (n - len(lines)) lines = lines[:n] print("len:",len(lines)) print(lines) max_len = 0 for x in lines: max_len = max(max_len,len(x)) r = [] for x in lines: r += [x.ljust(max_len)] print(r) return '\n'.join(r) text = "some fish\nsoup\ntasty" text2 = "fish" text3 = "hungry\nhippo\njungle\nrats\n" text4 = "a\nb\nc\nd\ne\nf\ng" #print(normalize_column(text,5)) def normalize_column_return_list(s,n): lines = s.split('\n') if len(lines) < n: lines += [''] * (n - len(lines)) lines = lines[:n] max_len = 0 for x in lines: max_len = max(max_len,len(x)) return [x.ljust(max_len) for x in lines] def paste_columns(data,sep=' ',pre='',post=''): columns = len(data) rows = 0 for s in data: rows = max(rows,s.count('\n') + 1) print("columns:",columns) print("rows:",rows) r = [normalize_column_return_list(s,rows) for s in data] return "\n".join(pre + sep.join(r[j][k] for j in range(columns)) + post for k in range(rows)) print(paste_columns([text,text2,text3,text4])) y_vect = "y1\ny2\ny3" x_vect = "x1\nx2\nx3\nx4\nx5\nx6" z_vect = "z1\nz2\nz3\nz4" equal = "=" col_1 = "7\n1\n9\n14" col_2 = "6\n1\n13\n10" col_3 = "14\n19\n13\n4" col_4 = "12\n16\n10\n14" col_5 = "10\n0\n12\n6" col_6 = "10\n14\n3\n16" matrix1 = paste_columns([col_1,col_2,col_3,col_4,col_5,col_6],' ','[ ',' ]') col_1 = "0.25\n4\n-7" col_2 = "1020\n0\n5.37" col_3 = "1\n-2\n4.1" col_4 = "3\n5\n13" matrix2 = paste_columns([col_1,col_2,col_3,col_4],' ','[ ',' ]') x = paste_columns([x_vect],' ','[ ',' ]') y = paste_columns([y_vect],' ','[ ',' ]') z = paste_columns([z_vect],' ','[ ',' ]') r1 = paste_columns([z,equal,matrix1,x]) r2 = paste_columns([y,equal,matrix2,matrix1,x]) print(matrix1,'\n') print(matrix2,'\n') print(x,'\n') print(y,'\n') print(z,'\n') print(r1,'\n') print(r2,'\n')