slither.utils.myprettytable
1from typing import List, Dict, Union 2 3from prettytable import PrettyTable 4from prettytable.colortable import ColorTable, Themes 5 6from slither.utils.colors import Colors 7 8 9class MyPrettyTable: 10 def __init__(self, field_names: List[str], pretty_align: bool = True): # TODO: True by default? 11 self._field_names = field_names 12 self._rows: List = [] 13 self._options: Dict = {} 14 if pretty_align: 15 self._options["set_alignment"] = [] 16 self._options["set_alignment"] += [(field_names[0], "l")] 17 for field_name in field_names[1:]: 18 self._options["set_alignment"] += [(field_name, "r")] 19 else: 20 self._options["set_alignment"] = [] 21 22 def add_row(self, row: List[Union[str, List[str]]]) -> None: 23 self._rows.append(row) 24 25 def to_pretty_table(self) -> PrettyTable: 26 if Colors.COLORIZATION_ENABLED: 27 table = ColorTable(self._field_names, theme=Themes.OCEAN) 28 else: 29 table = PrettyTable(self._field_names) 30 31 for row in self._rows: 32 table.add_row(row) 33 if len(self._options["set_alignment"]): 34 for column_header, value in self._options["set_alignment"]: 35 table.align[column_header] = value 36 return table 37 38 def to_json(self) -> Dict: 39 return {"fields_names": self._field_names, "rows": self._rows} 40 41 def __str__(self) -> str: 42 return str(self.to_pretty_table()) 43 44 45# UTILITY FUNCTIONS 46 47 48def make_pretty_table( 49 headers: list, body: dict, totals: bool = False, total_header="TOTAL" 50) -> MyPrettyTable: 51 """ 52 Converts a dict to a MyPrettyTable. Dict keys are the row headers. 53 Args: 54 headers: str[] of column names 55 body: dict of row headers with a dict of the values 56 totals: bool optional add Totals row 57 total_header: str optional if totals is set to True this will override the default "TOTAL" header 58 Returns: 59 MyPrettyTable 60 """ 61 table = MyPrettyTable(headers) 62 for row in body: 63 table_row = [row] + [body[row][key] for key in headers[1:]] 64 table.add_row(table_row) 65 if totals: 66 table.add_row( 67 [total_header] + [sum([body[row][key] for row in body]) for key in headers[1:]] 68 ) 69 return table
class
MyPrettyTable:
10class MyPrettyTable: 11 def __init__(self, field_names: List[str], pretty_align: bool = True): # TODO: True by default? 12 self._field_names = field_names 13 self._rows: List = [] 14 self._options: Dict = {} 15 if pretty_align: 16 self._options["set_alignment"] = [] 17 self._options["set_alignment"] += [(field_names[0], "l")] 18 for field_name in field_names[1:]: 19 self._options["set_alignment"] += [(field_name, "r")] 20 else: 21 self._options["set_alignment"] = [] 22 23 def add_row(self, row: List[Union[str, List[str]]]) -> None: 24 self._rows.append(row) 25 26 def to_pretty_table(self) -> PrettyTable: 27 if Colors.COLORIZATION_ENABLED: 28 table = ColorTable(self._field_names, theme=Themes.OCEAN) 29 else: 30 table = PrettyTable(self._field_names) 31 32 for row in self._rows: 33 table.add_row(row) 34 if len(self._options["set_alignment"]): 35 for column_header, value in self._options["set_alignment"]: 36 table.align[column_header] = value 37 return table 38 39 def to_json(self) -> Dict: 40 return {"fields_names": self._field_names, "rows": self._rows} 41 42 def __str__(self) -> str: 43 return str(self.to_pretty_table())
MyPrettyTable(field_names: List[str], pretty_align: bool = True)
11 def __init__(self, field_names: List[str], pretty_align: bool = True): # TODO: True by default? 12 self._field_names = field_names 13 self._rows: List = [] 14 self._options: Dict = {} 15 if pretty_align: 16 self._options["set_alignment"] = [] 17 self._options["set_alignment"] += [(field_names[0], "l")] 18 for field_name in field_names[1:]: 19 self._options["set_alignment"] += [(field_name, "r")] 20 else: 21 self._options["set_alignment"] = []
def
to_pretty_table(self) -> prettytable.prettytable.PrettyTable:
26 def to_pretty_table(self) -> PrettyTable: 27 if Colors.COLORIZATION_ENABLED: 28 table = ColorTable(self._field_names, theme=Themes.OCEAN) 29 else: 30 table = PrettyTable(self._field_names) 31 32 for row in self._rows: 33 table.add_row(row) 34 if len(self._options["set_alignment"]): 35 for column_header, value in self._options["set_alignment"]: 36 table.align[column_header] = value 37 return table
def
make_pretty_table( headers: list, body: dict, totals: bool = False, total_header='TOTAL') -> MyPrettyTable:
49def make_pretty_table( 50 headers: list, body: dict, totals: bool = False, total_header="TOTAL" 51) -> MyPrettyTable: 52 """ 53 Converts a dict to a MyPrettyTable. Dict keys are the row headers. 54 Args: 55 headers: str[] of column names 56 body: dict of row headers with a dict of the values 57 totals: bool optional add Totals row 58 total_header: str optional if totals is set to True this will override the default "TOTAL" header 59 Returns: 60 MyPrettyTable 61 """ 62 table = MyPrettyTable(headers) 63 for row in body: 64 table_row = [row] + [body[row][key] for key in headers[1:]] 65 table.add_row(table_row) 66 if totals: 67 table.add_row( 68 [total_header] + [sum([body[row][key] for row in body]) for key in headers[1:]] 69 ) 70 return table
Converts a dict to a MyPrettyTable. Dict keys are the row headers. Args: headers: str[] of column names body: dict of row headers with a dict of the values totals: bool optional add Totals row total_header: str optional if totals is set to True this will override the default "TOTAL" header Returns: MyPrettyTable