def export_to_table(self) -> None: """ Export in Unicode Table format. """ self.__preprocess() sub = len(self.keys) writer = UnicodeTableWriter() writer.table_name = "Analytics" writer.margin = 2 writer.headers = self.keys if sub >= 1: writer.value_matrix = [ self.values[ctr:ctr + sub] for ctr in range(0, len(self.values), sub) ] else: typer.secho( "An error occured please check your query.", fg=typer.colors.RED, bold=True, ) sys.exit() writer.write_table()
def shap_tablewriter(matrix, pairsname): """ use python tool tablewrite to write the shapely value into a more nice table :param matrix: shapley value matrix :param pairsname: name of docu pairs :return: """ feature_names = [ 'Body TF', 'Anchor TF', 'Title TF', 'URL TF', 'Whole docu TF', 'Body IDF', 'Anchor IDF', 'Title IDF', 'URL IDF', 'Whole docu IDF', 'Body TF*IDF', 'Anchor TF*IDF', 'Title TF*IDF', 'URL TF*IDF', 'Whole docu TF*IDF', 'Body DL', 'Anchor DL', 'Title DL', 'URL DL', 'Whole docu DL', 'Body BM25', 'Anchor BM25', 'Title BM25', 'URL BM25', 'Whole docu BM25', 'Body LMIR.ABS', 'Anchor LMIR.ABS', 'Title LMIR.ABS', 'URL LMIR.ABS', 'Whole docu LMIR.ABS', 'Body LMIR.DIR', 'Anchor LMIR.DIR', 'Title LMIR.DIR', 'URL LMIR.DIR', 'Whole docu LMIR.DIR', 'Body LMIR.JM', 'Anchor LMIR.JM', 'Title LMIR.JM', 'URL LMIR.JM', 'Whole docu LMIR.JM', 'PageRank', 'Inlink number', 'Outlink number', 'Number of slash in URL', 'URL Length', 'Number of child page' ] feature_names2 = np.array(feature_names).reshape(-1, 1) matrix2 = np.array(matrix) matrix2 = np.append(feature_names2, matrix2, axis=1) writer = UnicodeTableWriter() writer.table_name = "parameter_matrix" pairsname.insert(0, 'feature') writer.headers = pairsname matrix3 = [] for i in range(matrix2.shape[0]): row = list(matrix2[i]) matrix3.append(row) writer.value_matrix = matrix3 writer.write_table()
def query_lister(filename: str) -> None: """ Show details of the selected query. """ import toml from pytablewriter import UnicodeTableWriter # type: ignore writer = UnicodeTableWriter() writer.table_name = filename if not filename.endswith(".toml"): filename = filename + ".toml" p = Path.home() / ".queries" / filename with open(str(p), "r") as file: query_file = toml.load(file) writer.headers = [k for k in query_file["query"]] writer.value_matrix = [[ " ".join(v) if isinstance(v, list) else v for k, v in query_file["query"].items() ]] writer.write_table()
def weak_table(t1, t2=None): if t2 is None: w_type = tc.loc[tc[t1] > 1].index weak = UnicodeTableWriter() t_name = f"{t1} Type Weaknesses" weak.headers = ["Type", "Effectiveness"] weak.value_matrix = [[t, f'\t\t {tc[t1][t]}x'] for t in w_type] else: w_type = tc.loc[tc[t1] * tc[t2] > 1].index weak = UnicodeTableWriter() t_name = f"{t1}/{t2} Type Weaknesses" weak.headers = ["Type", "Effectiveness"] weak.value_matrix = [[t, f'\t\t {(tc[t1] * tc[t2])[t]}x'] for t in w_type] return t_name, weak
def res_table(t1, t2=None): if t2 is None: r_type = tc.loc[tc[t1] < 1].index res = UnicodeTableWriter() t_name = f"{t1} Type Resistances" res.headers = ["Type", "Effectiveness"] res.value_matrix = [[t, f'\t\t {tc[t1][t]}x'] for t in r_type] else: r_type = tc.loc[tc[t1] * tc[t2] < 1].index res = UnicodeTableWriter() t_name = f"{t1}/{t2} Type Resistances" res.headers = ["Type", "Effectiveness"] res.value_matrix = [[t, f'\t\t {(tc[t1] * tc[t2])[t]}x'] for t in r_type] return t_name, res
def send(self, pull_requests): from pytablewriter import UnicodeTableWriter value_matrix_nested = self.format_pr_values(pull_requests) value_matrix = [ line for lines in value_matrix_nested for line in lines ] writer = UnicodeTableWriter() writer.table_name = "open pull request to review" writer.headers = [ "repository_name", "[pull.creator] pull.title / pull.url" ] writer.value_matrix = value_matrix writer.margin = 1 writer.write_table()
def test_normal_styles(self): writer = UnicodeTableWriter() writer.from_tabledata(vut_style_tabledata) writer.column_styles = vut_styles writer.write_table() expected = dedent("""\ ┌────┬─────┬────┬─────┬──────┬─────┬────────────┬──────┬────────┬─────────────┐ │none│empty│tiny│small│medium│large│null w/ bold│L bold│S italic│L bold italic│ ├────┼─────┼────┼─────┼──────┼─────┼────────────┼──────┼────────┼─────────────┤ │ 111│ 111│ 111│ 111│ 111│ 111│ │ 111│ 111│ 111│ ├────┼─────┼────┼─────┼──────┼─────┼────────────┼──────┼────────┼─────────────┤ │1234│ 1234│1234│ 1234│ 1,234│1 234│ │ 1234│ 1234│ 1234│ └────┴─────┴────┴─────┴──────┴─────┴────────────┴──────┴────────┴─────────────┘ """) out = writer.dumps() print_test_result(expected=expected, actual=out) assert regexp_ansi_escape.search(out) assert strip_ansi_escape(out) == expected
def test_normal_numbers(self): writer = UnicodeTableWriter( dataframe=pd.DataFrame({ "realnumber": ["0.000000000000001", "1"], "long": ["1,000,000,000,000", "1"] }), margin=1, ) expected = dedent("""\ ┌───────────────────┬───────────────┐ │ realnumber │ long │ ├───────────────────┼───────────────┤ │ 0.000000000000001 │ 1000000000000 │ ├───────────────────┼───────────────┤ │ 1.000000000000000 │ 1 │ └───────────────────┴───────────────┘ """) output = writer.dumps() print_test_result(expected=expected, actual=output) assert output == expected
def test_normal_max_precision(self): writer = UnicodeTableWriter( headers=["realnumber", "long"], value_matrix=[ ["0.000000000000001", "1,000,000,000,000"], ["1", "1"], ], margin=1, max_precision=3, ) expected = dedent("""\ ┌────────────┬───────────────┐ │ realnumber │ long │ ├────────────┼───────────────┤ │ 0.000 │ 1000000000000 │ ├────────────┼───────────────┤ │ 1.000 │ 1 │ └────────────┴───────────────┘ """) output = writer.dumps() print_test_result(expected=expected, actual=output) assert output == expected