| Total Complexity | 9 |
| Total Lines | 39 |
| Duplicated Lines | 100 % |
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 | """ |
||
| 8 | View Code Duplication | class DownscaleFilter(ImagineFilterInterface): |
|
|
|
|||
| 9 | """ |
||
| 10 | Downscale filter |
||
| 11 | """ |
||
| 12 | size = None |
||
| 13 | |||
| 14 | def __init__(self, **kwargs): |
||
| 15 | """ |
||
| 16 | :param kwargs: dict |
||
| 17 | """ |
||
| 18 | if 'max' in kwargs and isinstance(kwargs['max'], list) and len(kwargs['max']) == 2: |
||
| 19 | self.size = kwargs.get('max') |
||
| 20 | else: |
||
| 21 | raise ValueError('Unsupported configuration') |
||
| 22 | |||
| 23 | def apply(self, resource): |
||
| 24 | """ |
||
| 25 | Apply filter to resource. |
||
| 26 | :param resource: Image |
||
| 27 | :return: Image |
||
| 28 | """ |
||
| 29 | if not isinstance(resource, Image.Image): |
||
| 30 | raise ValueError('Unsupported resource format: %s' % str(type(resource))) |
||
| 31 | |||
| 32 | original_width, original_height = resource.size |
||
| 33 | |||
| 34 | if original_width > self.size[0] or original_height > self.size[1]: |
||
| 35 | k = original_width / float(original_height) |
||
| 36 | |||
| 37 | if original_width >= original_height: |
||
| 38 | target_width = self.size[0] |
||
| 39 | target_height = int(target_width / k) |
||
| 40 | else: |
||
| 41 | target_height = self.size[1] |
||
| 42 | target_width = int(target_height * k) |
||
| 43 | |||
| 44 | resource = resource.resize((target_width, target_height), Image.ANTIALIAS) |
||
| 45 | |||
| 46 | return resource |
||
| 47 |