A Table defines how to convert a set of Stats into a specific set of rows displaying some aspect of the data.
| 627 | |
| 628 | |
| 629 | class Table: |
| 630 | """ |
| 631 | A Table defines how to convert a set of Stats into a specific set of rows |
| 632 | displaying some aspect of the data. |
| 633 | """ |
| 634 | |
| 635 | def __init__( |
| 636 | self, |
| 637 | column_names: Columns, |
| 638 | calc_rows: RowCalculator, |
| 639 | join_mode: JoinMode = JoinMode.SIMPLE, |
| 640 | ): |
| 641 | self.columns = column_names |
| 642 | self.calc_rows = calc_rows |
| 643 | self.join_mode = join_mode |
| 644 | |
| 645 | def join_row(self, key: str, row_a: tuple, row_b: tuple) -> tuple: |
| 646 | match self.join_mode: |
| 647 | case JoinMode.SIMPLE: |
| 648 | return (key, *row_a, *row_b) |
| 649 | case JoinMode.CHANGE | JoinMode.CHANGE_NO_SORT: |
| 650 | return (key, *row_a, *row_b, DiffRatio(row_a[0], row_b[0])) |
| 651 | case JoinMode.CHANGE_ONE_COLUMN: |
| 652 | return (key, row_a[0], row_b[0], DiffRatio(row_a[0], row_b[0])) |
| 653 | |
| 654 | def join_columns(self, columns: Columns) -> Columns: |
| 655 | match self.join_mode: |
| 656 | case JoinMode.SIMPLE: |
| 657 | return ( |
| 658 | columns[0], |
| 659 | *("Base " + x for x in columns[1:]), |
| 660 | *("Head " + x for x in columns[1:]), |
| 661 | ) |
| 662 | case JoinMode.CHANGE | JoinMode.CHANGE_NO_SORT: |
| 663 | return ( |
| 664 | columns[0], |
| 665 | *("Base " + x for x in columns[1:]), |
| 666 | *("Head " + x for x in columns[1:]), |
| 667 | ) + ("Change:",) |
| 668 | case JoinMode.CHANGE_ONE_COLUMN: |
| 669 | return ( |
| 670 | columns[0], |
| 671 | "Base " + columns[1], |
| 672 | "Head " + columns[1], |
| 673 | "Change:", |
| 674 | ) |
| 675 | |
| 676 | def join_tables(self, rows_a: Rows, rows_b: Rows) -> tuple[Columns, Rows]: |
| 677 | ncols = len(self.columns) |
| 678 | |
| 679 | default = ("",) * (ncols - 1) |
| 680 | data_a = {x[0]: x[1:] for x in rows_a} |
| 681 | data_b = {x[0]: x[1:] for x in rows_b} |
| 682 | |
| 683 | if len(data_a) != len(rows_a) or len(data_b) != len(rows_b): |
| 684 | raise ValueError("Duplicate keys") |
| 685 | |
| 686 | # To preserve ordering, use A's keys as is and then add any in B that |
no outgoing calls
no test coverage detected
searching dependent graphs…