This class provides a generic function for comparing any two tuples. Each instance records a list of tuple-indices (from most significant to least significant), and sort direction (ascending or descending) for each tuple-index. The compare functions can then be used as the function
| 554 | |
| 555 | |
| 556 | class TupleComp: |
| 557 | """This class provides a generic function for comparing any two tuples. |
| 558 | Each instance records a list of tuple-indices (from most significant |
| 559 | to least significant), and sort direction (ascending or descending) for |
| 560 | each tuple-index. The compare functions can then be used as the function |
| 561 | argument to the system sort() function when a list of tuples need to be |
| 562 | sorted in the instances order.""" |
| 563 | |
| 564 | def __init__(self, comp_select_list): |
| 565 | self.comp_select_list = comp_select_list |
| 566 | |
| 567 | def compare (self, left, right): |
| 568 | for index, direction in self.comp_select_list: |
| 569 | l = left[index] |
| 570 | r = right[index] |
| 571 | if l < r: |
| 572 | return -direction |
| 573 | if l > r: |
| 574 | return direction |
| 575 | return 0 |
| 576 | |
| 577 | |
| 578 | #************************************************************************** |
no outgoing calls
no test coverage detected
searching dependent graphs…