Conditions | 12 |
Total Lines | 53 |
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 BucketHandler.get() 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 | #!/usr/bin/env python3.5 |
||
48 | def get(self, slug): |
||
49 | slug_dir = os.path.dirname(slug) |
||
50 | output_options_string = os.path.basename(slug_dir) |
||
51 | output_options = output_options_string.split(",") |
||
52 | original_slug = slug.replace(output_options_string, '') |
||
53 | original_file = options.originals_path + "/" + original_slug |
||
54 | output_file = options.buckets_path + "/" + slug |
||
55 | output_dir = os.path.dirname(output_file) |
||
56 | output = None |
||
57 | size = [] |
||
58 | new_width = 50 |
||
59 | new_height = 50 |
||
60 | process_type = 't' |
||
61 | new_adjust = 'w' |
||
62 | if os.path.isfile(original_file): |
||
63 | if not os.path.exists(output_dir): |
||
64 | os.makedirs(output_dir) |
||
65 | image = Image.open(original_file) |
||
66 | image.load() |
||
67 | original_width, original_height = image.size |
||
68 | o = BytesIO() |
||
69 | for item in output_options: |
||
70 | if item.startswith('w_'): |
||
71 | new_width = int(item.replace('w_', '')) |
||
72 | if item.startswith('h_'): |
||
73 | new_height = int(item.replace('h_', '')) |
||
74 | if item.startswith('crop'): |
||
75 | process_type = 'c' |
||
76 | if item.startswith('a_'): |
||
77 | new_adjust = item.replace('a_', '') |
||
78 | if process_type == 't': |
||
79 | if new_adjust == 'w': |
||
80 | new_height = new_width*original_height/original_width |
||
81 | elif new_adjust == 'h': |
||
82 | new_width = new_height*original_width/original_height |
||
83 | size.append(new_width) |
||
84 | size.append(new_height) |
||
85 | image.thumbnail(size, Image.ANTIALIAS) |
||
86 | image.save(output_file, image.format, quality=90) |
||
87 | image.save(o, image.format, quality=90) |
||
88 | output = o.getvalue() |
||
89 | elif process_type == 'c': |
||
90 | box = self.innerDetermineBox() |
||
91 | new_image = image.crop(box) |
||
92 | new_image.load() |
||
93 | new_image.save(output_file, image.format, quality=90) |
||
94 | new_image.save(o, image.format, quality=90) |
||
95 | output = o.getvalue() |
||
96 | self.set_header('Content-type', 'image/'+image.format) |
||
97 | self.set_status(404, 'Not Found') |
||
98 | output = "404/rcdn o: ("+options.originals_path+" --- "+original_slug+")" + original_file + " n:" + \ |
||
99 | output_file + " op:" + " ".join(output_options) |
||
100 | self.write(output) |
||
101 | |||
128 |