Conditions | 29 |
Total Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 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 |
||
55 | @classmethod |
||
56 | def format(cls, entries, *args, **kwargs): |
||
57 | attributes = kwargs.get('attributes', []) |
||
58 | attribute_transform_functions = kwargs.get('attribute_transform_functions', {}) |
||
59 | widths = kwargs.get('widths', []) |
||
60 | widths = widths or [] |
||
61 | |||
62 | if not widths and attributes: |
||
63 | # Dynamically calculate column size based on the terminal size |
||
64 | lines, cols = get_terminal_size() |
||
65 | |||
66 | if attributes[0] == 'id': |
||
67 | # consume iterator and save as entries so collection is accessible later. |
||
68 | entries = [e for e in entries] |
||
69 | # first column contains id, make sure it's not broken up |
||
70 | first_col_width = cls._get_required_column_width(values=[e.id for e in entries], |
||
71 | minimum_width=MIN_ID_COL_WIDTH) |
||
72 | cols = (cols - first_col_width) |
||
73 | col_width = int(math.floor((cols / len(attributes)))) |
||
74 | else: |
||
75 | col_width = int(math.floor((cols / len(attributes)))) |
||
76 | first_col_width = col_width |
||
77 | widths = [] |
||
78 | subtract = 0 |
||
79 | for index in range(0, len(attributes)): |
||
80 | attribute_name = attributes[index] |
||
81 | |||
82 | if index == 0: |
||
83 | widths.append(first_col_width) |
||
84 | continue |
||
85 | |||
86 | if attribute_name in COLORIZED_ATTRIBUTES: |
||
87 | current_col_width = COLORIZED_ATTRIBUTES[attribute_name]['col_width'] |
||
88 | subtract += (current_col_width - col_width) |
||
89 | else: |
||
90 | # Make sure we subtract the added width from the last column so we account |
||
91 | # for the fixed width columns and make sure table is not wider than the |
||
92 | # terminal width. |
||
93 | if index == (len(attributes) - 1) and subtract: |
||
94 | current_col_width = (col_width - subtract) |
||
95 | |||
96 | if current_col_width <= MIN_COL_WIDTH: |
||
97 | # Make sure column width is always grater than MIN_COL_WIDTH |
||
98 | current_col_width = MIN_COL_WIDTH |
||
99 | else: |
||
100 | current_col_width = col_width |
||
101 | |||
102 | widths.append(current_col_width) |
||
103 | |||
104 | if not attributes or 'all' in attributes: |
||
105 | entries = list(entries) if entries else [] |
||
106 | |||
107 | if len(entries) >= 1: |
||
108 | attributes = entries[0].__dict__.keys() |
||
109 | attributes = sorted([attr for attr in attributes if not attr.startswith('_')]) |
||
110 | else: |
||
111 | # There are no entries so we can't infer available attributes |
||
112 | attributes = [] |
||
113 | |||
114 | # Determine table format. |
||
115 | if len(attributes) == len(widths): |
||
116 | # Customize width for each column. |
||
117 | columns = zip(attributes, widths) |
||
118 | else: |
||
119 | # If only 1 width value is provided then |
||
120 | # apply it to all columns else fix at 28. |
||
121 | width = widths[0] if len(widths) == 1 else 28 |
||
122 | columns = zip(attributes, |
||
123 | [width for i in range(0, len(attributes))]) |
||
124 | |||
125 | # Format result to table. |
||
126 | table = PrettyTable() |
||
127 | for column in columns: |
||
128 | table.field_names.append(column[0]) |
||
129 | table.max_width[column[0]] = column[1] |
||
130 | table.padding_width = 1 |
||
131 | table.align = 'l' |
||
132 | table.valign = 't' |
||
133 | for entry in entries: |
||
134 | # TODO: Improve getting values of nested dict. |
||
135 | values = [] |
||
136 | for field_name in table.field_names: |
||
137 | if '.' in field_name: |
||
138 | field_names = field_name.split('.') |
||
139 | value = getattr(entry, field_names.pop(0), {}) |
||
140 | for name in field_names: |
||
141 | value = cls._get_field_value(value, name) |
||
142 | if type(value) is str: |
||
143 | break |
||
144 | value = strutil.strip_carriage_returns(strutil.unescape(value)) |
||
145 | values.append(value) |
||
146 | else: |
||
147 | value = cls._get_simple_field_value(entry, field_name) |
||
148 | transform_function = attribute_transform_functions.get(field_name, |
||
149 | lambda value: value) |
||
150 | value = transform_function(value=value) |
||
151 | value = strutil.strip_carriage_returns(strutil.unescape(value)) |
||
152 | values.append(value) |
||
153 | table.add_row(values) |
||
154 | |||
155 | # width for the note |
||
156 | try: |
||
157 | cls.table_width = len(table.get_string().split("\n")[0]) |
||
158 | except IndexError: |
||
159 | cls.table_width = 0 |
||
160 | |||
161 | return table |
||
162 | |||
292 |
It is generally discouraged to redefine built-ins as this makes code very hard to read.