Conditions | 9 |
Total Lines | 129 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
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:
1 | # -*- coding: utf-8 |
||
21 | def main(): |
||
22 | """Main.""" |
||
23 | # Different examples : |
||
24 | # Code examples are groupes by error type then by suggestion type |
||
25 | # Numbers have been added in dict keys just to be able to iterate |
||
26 | # over them and have the result in the wanted order. |
||
27 | examples = { |
||
28 | (1, NameError): { |
||
29 | (1, "Fuzzy matches on existing names " |
||
30 | "(local, builtin, keywords, modules, etc)"): [ |
||
31 | "def my_func(foo, bar):\n\treturn foob\nmy_func(1, 2)", |
||
32 | "leng([0])", |
||
33 | "import math\nmaths.pi", |
||
34 | "passs", |
||
35 | "def my_func():\n\tfoo = 1\n\tfoob +=1\nmy_func()" |
||
36 | ], |
||
37 | (2, "Checking if name is the attribute of a defined object"): [ |
||
38 | "class Duck():\n\tdef __init__(self):\n\t\tquack()" |
||
39 | "\n\tdef quack(self):\n\t\tpass\nd = Duck()", |
||
40 | "import math\npi", |
||
41 | ], |
||
42 | (3, "Looking for missing imports"): [ |
||
43 | "string.ascii_lowercase", |
||
44 | ], |
||
45 | (4, "Looking in missing imports"): [ |
||
46 | "choice", |
||
47 | ], |
||
48 | (5, "Special cases"): [ |
||
49 | "assert j ** 2 == -1", |
||
50 | ], |
||
51 | }, |
||
52 | (2, AttributeError): { |
||
53 | (1, "Fuzzy matches on existing attributes"): [ |
||
54 | "lst = [1, 2, 3]\nlst.appendh(4)", |
||
55 | "import math\nmath.pie", |
||
56 | ], |
||
57 | (2, "Detection of mis-used builtins"): [ |
||
58 | "lst = [1, 2, 3]\nlst.max()", |
||
59 | ], |
||
60 | (3, "Trying to find method with similar meaning (hardcoded)"): [ |
||
61 | "lst = [1, 2, 3]\nlst.add(4)", |
||
62 | "lst = [1, 2, 3]\nlst.get(5, None)", |
||
63 | ], |
||
64 | }, |
||
65 | (3, ImportError): { |
||
66 | (1, "Fuzzy matches on existing modules"): [ |
||
67 | "from maths import pi", |
||
68 | ], |
||
69 | (2, "Fuzzy matches on elements of the module"): [ |
||
70 | "from math import pie", |
||
71 | ], |
||
72 | (3, "Looking for import from wrong module"): [ |
||
73 | "from itertools import pi", |
||
74 | ], |
||
75 | }, |
||
76 | (4, TypeError): { |
||
77 | (1, "Fuzzy matches on keyword arguments"): [ |
||
78 | "def my_func(abcde):\n\tpass\nmy_func(abcdf=1)", |
||
79 | ], |
||
80 | (2, "Confusion between brackets and parenthesis"): [ |
||
81 | "lst = [1, 2, 3]\nlst(0)", |
||
82 | "def my_func(a):\n\tpass\nmy_func[1]", |
||
83 | ], |
||
84 | }, |
||
85 | (5, ValueError): { |
||
86 | (1, "Special cases"): [ |
||
87 | "'Foo{}'.format('bar')", |
||
88 | 'import datetime\n' |
||
89 | 'datetime.datetime.strptime("%d %b %y", "30 Nov 00")', |
||
90 | ], |
||
91 | }, |
||
92 | (6, SyntaxError): { |
||
93 | (1, "Fuzzy matches when importing from __future__"): [ |
||
94 | "from __future__ import divisio", |
||
95 | ], |
||
96 | (2, "Various"): [ |
||
97 | "return", |
||
98 | ], |
||
99 | }, |
||
100 | (7, MemoryError): { |
||
101 | (1, "Search for a memory-efficient equivalent"): [ |
||
102 | "range(999999999999999)", |
||
103 | ], |
||
104 | }, |
||
105 | (8, OverflowError): { |
||
106 | (1, "Search for a memory-efficient equivalent"): [ |
||
107 | "range(999999999999999)", |
||
108 | ], |
||
109 | }, |
||
110 | (9, (OSError, IOError)): { |
||
111 | (1, "Suggestion for tilde/variable expansions"): [ |
||
112 | "os.listdir('~')", |
||
113 | ] |
||
114 | }, |
||
115 | (10, RuntimeError): { |
||
116 | (1, "Suggestion to avoid reaching maximum recursion depth"): [ |
||
117 | "global rec\ndef rec(n): return rec(n-1)\nrec(0)" |
||
118 | ], |
||
119 | }, |
||
120 | } |
||
121 | |||
122 | str_func = repr # could be str or repr |
||
123 | for (_, exc_types), exc_examples in sorted(examples.items()): |
||
124 | if not isinstance(exc_types, tuple): |
||
125 | exc_types = (exc_types, ) |
||
126 | print("### {0}\n".format("/".join(e.__name__ for e in exc_types))) |
||
127 | for (_, desc), codes in sorted(exc_examples.items()): |
||
128 | print("##### {0}\n".format(desc)) |
||
129 | for code in codes: |
||
130 | exc = common.get_exception(code) |
||
131 | if exc is None: |
||
132 | before = after = \ |
||
133 | "No exception thrown on this version of Python" |
||
134 | else: |
||
135 | type_, value, traceback = exc |
||
136 | if not issubclass(type_, exc_types): |
||
137 | before = after = \ |
||
138 | "Wrong exception thrown on this version of Python" |
||
139 | else: |
||
140 | before = standardise(str_func(value)) |
||
141 | add_suggestions_to_exception(type_, value, traceback) |
||
142 | after = standardise(str_func(value)) |
||
143 | if before == after: |
||
144 | after += " (unchanged on this version of Python)" |
||
145 | print("""```python |
||
146 | {0} |
||
147 | #>>> Before: {1} |
||
148 | #>>> After: {2} |
||
149 | ```""".format(standardise(code), before, after)) |
||
150 | |||
153 |