Conditions | 11 |
Total Lines | 59 |
Lines | 0 |
Ratio | 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 src.redisboard._raw_get_db_summary() 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 | from logging import getLogger |
||
119 | def _raw_get_db_summary(server, db): |
||
120 | server.connection.execute_command('SELECT', db) |
||
121 | pipe = server.connection.pipeline() |
||
122 | |||
123 | pipe.dbsize() |
||
124 | for i in range(server.sampling_threshold): |
||
125 | pipe.randomkey() |
||
126 | |||
127 | results = pipe.execute() |
||
128 | size = results.pop(0) |
||
129 | keys = sorted(set(results)) |
||
130 | |||
131 | pipe = server.connection.pipeline() |
||
132 | for key in keys: |
||
133 | pipe.execute_command('DEBUG', 'OBJECT', key) |
||
134 | pipe.ttl(key) |
||
135 | |||
136 | total_memory = 0 |
||
137 | volatile_memory = 0 |
||
138 | persistent_memory = 0 |
||
139 | total_keys = 0 |
||
140 | volatile_keys = 0 |
||
141 | persistent_keys = 0 |
||
142 | results = pipe.execute() |
||
143 | for key, details, ttl in zip(keys, results[::2], results[1::2]): |
||
144 | if not isinstance(details, dict): |
||
145 | details = dict(_fixup_pair(i.split(b':')) |
||
146 | for i in details.split() if b':' in i) |
||
147 | |||
148 | length = details[b'serializedlength'] + len(key) |
||
149 | |||
150 | if ttl: |
||
151 | persistent_memory += length |
||
152 | persistent_keys += 1 |
||
153 | else: |
||
154 | volatile_memory += length |
||
155 | volatile_keys += 1 |
||
156 | total_memory += length |
||
157 | total_keys += 1 |
||
158 | |||
159 | if total_keys: |
||
160 | total_memory = (total_memory / total_keys) * size |
||
161 | else: |
||
162 | total_memory = 0 |
||
163 | |||
164 | if persistent_keys: |
||
165 | persistent_memory = (persistent_memory / persistent_keys) * size |
||
166 | else: |
||
167 | persistent_memory = 0 |
||
168 | |||
169 | if volatile_keys: |
||
170 | volatile_memory = (volatile_memory / volatile_keys) * size |
||
171 | else: |
||
172 | volatile_memory = 0 |
||
173 | return dict( |
||
174 | size=size, |
||
175 | total_memory=total_memory, |
||
176 | volatile_memory=volatile_memory, |
||
177 | persistent_memory=persistent_memory, |
||
178 | ) |
||
261 |