| Conditions | 20 |
| Total Lines | 92 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 Query.__init__() 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 __future__ import absolute_import |
||
| 34 | def __init__(self, **query): |
||
| 35 | """ |
||
| 36 | Args: |
||
| 37 | query: criteria to match on. |
||
| 38 | |||
| 39 | Accepted arguments: |
||
| 40 | ``arg``, |
||
| 41 | ``calls``, |
||
| 42 | ``code``, |
||
| 43 | ``depth``, |
||
| 44 | ``filename``, |
||
| 45 | ``frame``, |
||
| 46 | ``fullsource``, |
||
| 47 | ``function``, |
||
| 48 | ``globals``, |
||
| 49 | ``kind``, |
||
| 50 | ``lineno``, |
||
| 51 | ``locals``, |
||
| 52 | ``module``, |
||
| 53 | ``source``, |
||
| 54 | ``stdlib``, |
||
| 55 | ``threadid``, |
||
| 56 | ``threadname``. |
||
| 57 | """ |
||
| 58 | query_eq = {} |
||
| 59 | query_startswith = {} |
||
| 60 | query_endswith = {} |
||
| 61 | query_in = {} |
||
| 62 | query_contains = {} |
||
| 63 | query_regex = {} |
||
| 64 | query_lt = {} |
||
| 65 | query_lte = {} |
||
| 66 | query_gt = {} |
||
| 67 | query_gte = {} |
||
| 68 | |||
| 69 | for key, value in query.items(): |
||
| 70 | parts = [p for p in key.split('_') if p] |
||
| 71 | count = len(parts) |
||
| 72 | if count > 2: |
||
| 73 | raise TypeError('Unexpected argument %r. Must be one of %s with optional operators like: %s' % ( |
||
| 74 | key, ALLOWED_KEYS, ALLOWED_OPERATORS |
||
| 75 | )) |
||
| 76 | elif count == 2: |
||
| 77 | prefix, operator = parts |
||
| 78 | if operator in ('startswith', 'sw'): |
||
| 79 | if not isinstance(value, string_types): |
||
| 80 | if not isinstance(value, (list, set, tuple)): |
||
| 81 | raise ValueError('Value %r for %r is invalid. Must be a string, list, tuple or set.' % (value, key)) |
||
| 82 | value = tuple(value) |
||
| 83 | mapping = query_startswith |
||
| 84 | elif operator in ('endswith', 'ew'): |
||
| 85 | if not isinstance(value, string_types): |
||
| 86 | if not isinstance(value, (list, set, tuple)): |
||
| 87 | raise ValueError('Value %r for %r is invalid. Must be a string, list, tuple or set.' % (value, key)) |
||
| 88 | value = tuple(value) |
||
| 89 | mapping = query_endswith |
||
| 90 | elif operator == 'in': |
||
| 91 | mapping = query_in |
||
| 92 | elif operator in ('contains', 'has'): |
||
| 93 | mapping = query_contains |
||
| 94 | elif operator in ('regex', 'rx'): |
||
| 95 | value = re.compile(value) |
||
| 96 | mapping = query_regex |
||
| 97 | elif operator == 'lt': |
||
| 98 | mapping = query_lt |
||
| 99 | elif operator == 'lte': |
||
| 100 | mapping = query_lte |
||
| 101 | elif operator == 'gt': |
||
| 102 | mapping = query_gt |
||
| 103 | elif operator == 'gte': |
||
| 104 | mapping = query_gte |
||
| 105 | else: |
||
| 106 | raise TypeError('Unexpected operator %r. Must be one of %s.' % (operator, ALLOWED_OPERATORS)) |
||
| 107 | else: |
||
| 108 | mapping = query_eq |
||
| 109 | prefix = key |
||
| 110 | |||
| 111 | if prefix not in ALLOWED_KEYS: |
||
| 112 | raise TypeError('Unexpected argument %r. Must be one of %s.' % (key, ALLOWED_KEYS)) |
||
| 113 | |||
| 114 | mapping[prefix] = value |
||
| 115 | |||
| 116 | self.query_eq = tuple(sorted(query_eq.items())) |
||
| 117 | self.query_startswith = tuple(sorted(query_startswith.items())) |
||
| 118 | self.query_endswith = tuple(sorted(query_endswith.items())) |
||
| 119 | self.query_in = tuple(sorted(query_in.items())) |
||
| 120 | self.query_contains = tuple(sorted(query_contains.items())) |
||
| 121 | self.query_regex = tuple(sorted(query_regex.items())) |
||
| 122 | self.query_lt = tuple(sorted(query_lt.items())) |
||
| 123 | self.query_lte = tuple(sorted(query_lte.items())) |
||
| 124 | self.query_gt = tuple(sorted(query_gt.items())) |
||
| 125 | self.query_gte = tuple(sorted(query_gte.items())) |
||
| 126 | |||
| 404 |