| Conditions | 21 |
| Total Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 analyse() 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 | """Graphical exception handler for PyGTK applications |
||
| 63 | def analyse(exctyp, value, tback): |
||
| 64 | import tokenize, keyword |
||
| 65 | |||
| 66 | trace = StringIO() |
||
| 67 | nlines = 3 |
||
| 68 | frecs = inspect.getinnerframes(tback, nlines) |
||
| 69 | trace.write('Traceback (most recent call last):\n') |
||
| 70 | # pylint: disable=unused-variable |
||
| 71 | for frame, fname, lineno, funcname, context, cindex in frecs: |
||
| 72 | trace.write(' File "%s", line %d, ' % (fname, lineno)) |
||
| 73 | args, varargs, varkw, lcls = inspect.getargvalues(frame) |
||
| 74 | |||
| 75 | def readline(lno=[lineno], *args): |
||
| 76 | if args: |
||
| 77 | print args |
||
| 78 | try: |
||
| 79 | return linecache.getline(fname, lno[0]) |
||
| 80 | finally: |
||
| 81 | lno[0] += 1 |
||
| 82 | _all, prev, name, scope = {}, None, '', None |
||
| 83 | for ttype, tstr, stup, etup, lin in tokenize.generate_tokens(readline): |
||
| 84 | if ttype == tokenize.NAME and tstr not in keyword.kwlist: |
||
| 85 | if name: |
||
| 86 | if name[-1] == '.': |
||
| 87 | try: |
||
| 88 | val = getattr(prev, tstr) |
||
| 89 | except AttributeError: |
||
| 90 | # XXX skip the rest of this identifier only |
||
| 91 | break |
||
| 92 | name += tstr |
||
| 93 | else: |
||
| 94 | assert not name and not scope |
||
| 95 | scope, val = lookup(tstr, frame, lcls) |
||
| 96 | name = tstr |
||
| 97 | try: |
||
| 98 | if val: |
||
| 99 | prev = val |
||
| 100 | except: |
||
| 101 | pass |
||
| 102 | # TODO |
||
| 103 | # print(' found', scope, 'name', name, 'val', val, 'in', |
||
| 104 | # prev, 'for token', tstr) |
||
| 105 | elif tstr == '.': |
||
| 106 | if prev: |
||
| 107 | name += '.' |
||
| 108 | else: |
||
| 109 | if name: |
||
| 110 | _all[name] = (scope, prev) |
||
| 111 | prev, name, scope = None, '', None |
||
| 112 | if ttype == tokenize.NEWLINE: |
||
| 113 | break |
||
| 114 | |||
| 115 | trace.write(funcname + |
||
| 116 | inspect.formatargvalues(args, varargs, varkw, lcls, |
||
| 117 | formatvalue=lambda v: '=' + pydoc.text.repr(v)) + '\n') |
||
| 118 | trace.write(''.join([' ' + x.replace('\t', ' ') |
||
| 119 | for x in context if x.strip()])) |
||
| 120 | if len(_all): |
||
| 121 | trace.write(' variables: %s\n' % pformat(_all, indent=3)) |
||
| 122 | |||
| 123 | trace.write('%s: %s' % (exctyp.__name__, value)) |
||
| 124 | return trace |
||
| 125 | |||
| 252 |