| Conditions | 28 |
| Total Lines | 101 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like MultiColumnTable.format() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 51 | @classmethod |
||
| 52 | def format(cls, entries, *args, **kwargs): |
||
| 53 | attributes = kwargs.get('attributes', []) |
||
| 54 | attribute_transform_functions = kwargs.get('attribute_transform_functions', {}) |
||
| 55 | widths = kwargs.get('widths', []) |
||
| 56 | widths = widths or [] |
||
| 57 | |||
| 58 | if not widths and attributes: |
||
| 59 | # Dynamically calculate column size based on the terminal size |
||
| 60 | lines, cols = get_terminal_size() |
||
| 61 | |||
| 62 | if attributes[0] == 'id': |
||
| 63 | # consume iterator and save as entries so collection is accessible later. |
||
| 64 | entries = [e for e in entries] |
||
| 65 | # first column contains id, make sure it's not broken up |
||
| 66 | first_col_width = cls._get_required_column_width(values=[e.id for e in entries], |
||
| 67 | minimum_width=MIN_ID_COL_WIDTH) |
||
| 68 | cols = (cols - first_col_width) |
||
| 69 | col_width = int(math.floor((cols / len(attributes)))) |
||
| 70 | else: |
||
| 71 | col_width = int(math.floor((cols / len(attributes)))) |
||
| 72 | first_col_width = col_width |
||
| 73 | |||
| 74 | widths = [] |
||
| 75 | subtract = 0 |
||
| 76 | for index in range(0, len(attributes)): |
||
| 77 | attribute_name = attributes[index] |
||
| 78 | |||
| 79 | if index == 0: |
||
| 80 | widths.append(first_col_width) |
||
| 81 | continue |
||
| 82 | |||
| 83 | if attribute_name in COLORIZED_ATTRIBUTES: |
||
| 84 | current_col_width = COLORIZED_ATTRIBUTES[attribute_name]['col_width'] |
||
| 85 | subtract += (current_col_width - col_width) |
||
| 86 | else: |
||
| 87 | # Make sure we subtract the added width from the last column so we account |
||
| 88 | # for the fixed width columns and make sure table is not wider than the |
||
| 89 | # terminal width. |
||
| 90 | if index == (len(attributes) - 1) and subtract: |
||
| 91 | current_col_width = (col_width - subtract) |
||
| 92 | |||
| 93 | if current_col_width <= MIN_COL_WIDTH: |
||
| 94 | # Make sure column width is always grater than MIN_COL_WIDTH |
||
| 95 | current_col_width = MIN_COL_WIDTH |
||
| 96 | else: |
||
| 97 | current_col_width = col_width |
||
| 98 | |||
| 99 | widths.append(current_col_width) |
||
| 100 | |||
| 101 | if not attributes or 'all' in attributes: |
||
| 102 | entries = list(entries) if entries else [] |
||
| 103 | |||
| 104 | if len(entries) >= 1: |
||
| 105 | attributes = entries[0].__dict__.keys() |
||
| 106 | attributes = sorted([attr for attr in attributes if not attr.startswith('_')]) |
||
| 107 | else: |
||
| 108 | # There are no entries so we can't infer available attributes |
||
| 109 | attributes = [] |
||
| 110 | |||
| 111 | # Determine table format. |
||
| 112 | if len(attributes) == len(widths): |
||
| 113 | # Customize width for each column. |
||
| 114 | columns = zip(attributes, widths) |
||
| 115 | else: |
||
| 116 | # If only 1 width value is provided then |
||
| 117 | # apply it to all columns else fix at 28. |
||
| 118 | width = widths[0] if len(widths) == 1 else 28 |
||
| 119 | columns = zip(attributes, |
||
| 120 | [width for i in range(0, len(attributes))]) |
||
| 121 | |||
| 122 | # Format result to table. |
||
| 123 | table = PrettyTable() |
||
| 124 | for column in columns: |
||
| 125 | table.field_names.append(column[0]) |
||
| 126 | table.max_width[column[0]] = column[1] |
||
| 127 | table.padding_width = 1 |
||
| 128 | table.align = 'l' |
||
| 129 | table.valign = 't' |
||
| 130 | for entry in entries: |
||
| 131 | # TODO: Improve getting values of nested dict. |
||
| 132 | values = [] |
||
| 133 | for field_name in table.field_names: |
||
| 134 | if '.' in field_name: |
||
| 135 | field_names = field_name.split('.') |
||
| 136 | value = getattr(entry, field_names.pop(0), {}) |
||
| 137 | for name in field_names: |
||
| 138 | value = cls._get_field_value(value, name) |
||
| 139 | if type(value) is str: |
||
| 140 | break |
||
| 141 | value = strutil.unescape(value) |
||
| 142 | values.append(value) |
||
| 143 | else: |
||
| 144 | value = cls._get_simple_field_value(entry, field_name) |
||
| 145 | transform_function = attribute_transform_functions.get(field_name, |
||
| 146 | lambda value: value) |
||
| 147 | value = transform_function(value=value) |
||
| 148 | value = strutil.unescape(value) |
||
| 149 | values.append(value) |
||
| 150 | table.add_row(values) |
||
| 151 | return table |
||
| 152 | |||
| 251 |
It is generally discouraged to redefine built-ins as this makes code very hard to read.