Conditions | 9 |
Total Lines | 52 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 | """ |
||
39 | def generate(): |
||
40 | import xml.etree.ElementTree as ET |
||
41 | import string |
||
42 | |||
43 | printable = set(string.printable) |
||
44 | files = glob.glob("/Applications/LXSeries/LXFree.app/Contents/Resources/keys/*.lxkey") |
||
45 | |||
46 | for file in files: |
||
47 | tree = ET.parse(file) |
||
48 | root = tree.getroot() |
||
49 | for fixture in root.findall("kentry"): |
||
50 | try: |
||
51 | # Get names |
||
52 | name = fixture.find("fname").text |
||
53 | full_name = fixture.find("name").text |
||
54 | |||
55 | # Get points to numbers |
||
56 | raw_points = fixture.find("custom").find("symbol").find("points") |
||
57 | parsed_points = [] |
||
58 | for point in raw_points: |
||
59 | parsed_points.append([ |
||
60 | float(point.find("x").text), |
||
61 | float(point.find("y").text), |
||
62 | int(point.find("op").text) |
||
63 | ]) |
||
64 | |||
65 | # Find the smallest x/y (often negative) |
||
66 | minx = min([f[0] for f in parsed_points]) |
||
67 | minx = minx * -1 if minx < 0 else 0 |
||
68 | miny = min([f[1] for f in parsed_points]) |
||
69 | miny = miny * -1 if miny < 0 else 0 |
||
70 | |||
71 | # Force all points to be positive |
||
72 | parsed_points = [[f[0] + minx, f[1] + miny, f[2]] for f in parsed_points] |
||
73 | |||
74 | # Scale to reduce likelihood of decimal point and then make int |
||
75 | parsed_points = [[f[0] * 4, f[1] * 4, f[2]] for f in parsed_points] |
||
76 | parsed_points = [[int(f[0]), int(f[1]), f[2]] for f in parsed_points] |
||
77 | |||
78 | # Output |
||
79 | lines = [ |
||
80 | "name1 = \"{}\"".format(name), |
||
81 | "name2 = \"{}\"".format(full_name), |
||
82 | "points = {}".format(parsed_points) |
||
83 | ] |
||
84 | file = name.replace(" ", "_").replace(".", "").replace("/", "") \ |
||
85 | + "_" + full_name.replace(" ", "_").replace(".", "").replace("/", "") |
||
86 | file = ''.join(filter(lambda x: x in printable, file)) |
||
87 | with open("{}/{}.data.py".format(dir_path, file), "w") as f: |
||
88 | f.write("\n".join(lines)) |
||
89 | except: |
||
90 | pass |
||
91 |