Condition.check()   F
last analyzed

Complexity

Conditions 12

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
dl 0
loc 30
rs 2.7855
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like Condition.check() 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
# -*- coding: utf-8 -*-
2
3
__all__ = ['Condition']
4
5
6
class Condition(object):
7
    '''
8
        class Condition permits to reduce the size of the data to return
9
        by applying a rule of filtering
10
    '''
11
12
    def __init__(self, **kwargs):
13
        '''
14
            set the 2 filters type : match and does_not_match
15
        '''
16
        if 'does_not_match' in kwargs:
17
            self.does_not_match = kwargs['does_not_match']
18
19
        if 'match' in kwargs:
20
            self.match = kwargs['match']
21
22
    def check(self, datas, *filers):
23
        '''
24
            this method permits to reduce the quantity of information to read
25
            by applying some filtering
26
            here '*filers' can receive a list of properties to be filtered
27
        '''
28
        # special case : no filter : want to read all the feed
29
        if self.match == "" and self.does_not_match == '':
30
            yield datas
31
        # let's filtering :
32
        else:
33
            condition1 = False
34
            condition2 = False
35
            # arg contain the property from which we want to check the 'data'
36
            for prop in filers:
37
                # check if my datas contains my property
38
                if prop in datas:
39
                    # filter to find only this data
40
                    if self.match != '' and condition1 is False:
41
                        condition1 = self.filter_that(self.match,
42
                                                      datas[prop])
43
                    # filter to exclude this data,
44
                    # when found, continue to the next entry
45
                    if self.does_not_match != '' and condition2 is False:
46
                        condition2 = self.filter_that(self.does_not_match,
47
                                                      datas[prop])
48
                        if condition2:
49
                            continue
50
        if condition1 and condition2 is False:
51
            yield datas
52
53
    def filter_that(self, criteria, data):
54
        '''
55
            this method just use the module 're' to check if the data contain
56
            the string to find
57
        '''
58
        import re
59
        prog = re.compile(criteria)
60
61
        return True if prog.match(data) else False
62