Total Complexity | 9 |
Total Lines | 41 |
Duplicated Lines | 100 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | """ |
||
9 | View Code Duplication | class DownscaleFilter(ImagineFilterInterface): |
|
|
|||
10 | """ |
||
11 | Downscale filter |
||
12 | """ |
||
13 | size = None |
||
14 | |||
15 | def __init__(self, **kwargs): |
||
16 | """ |
||
17 | :param kwargs: dict |
||
18 | """ |
||
19 | if 'max' in kwargs and isinstance(kwargs['max'], list) and len(kwargs['max']) == 2: |
||
20 | self.size = kwargs.get('max') |
||
21 | else: |
||
22 | raise ValueError('Unsupported configuration') |
||
23 | |||
24 | def apply(self, resource): |
||
25 | """ |
||
26 | Apply filter to resource. |
||
27 | :param resource: Image |
||
28 | :return: Image |
||
29 | """ |
||
30 | if not isinstance(resource, Image.Image): |
||
31 | raise ValueError('Unsupported resource format: %s' % str(type(resource))) |
||
32 | |||
33 | original_width, original_height = resource.size |
||
34 | |||
35 | if original_width > self.size[0] or original_height > self.size[1]: |
||
36 | k = original_width / original_height |
||
37 | |||
38 | if original_width >= original_height: |
||
39 | target_width = self.size[0] |
||
40 | target_height = int(target_width / k) |
||
41 | else: |
||
42 | target_height = self.size[1] |
||
43 | target_width = int(target_height * k) |
||
44 | |||
45 | resource_format = resource.format |
||
46 | resource = resource.resize((target_width, target_height), Image.ANTIALIAS) |
||
47 | resource.format = resource_format |
||
48 | |||
49 | return resource |
||
50 |