summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/DataProcessing/Prefix.py
blob: 5e4e1d319ab1014cf5ffde388a2323250aebef8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'''
Created on Feb 27, 2012

@author: steger
'''
from DataProcessing.DataError import PrefixError

class PrefixManager(object):
    '''
    @summary: acts a unit prefix container
    @ivar prefixes: the container of the known prefixes
    @type prefixes: dict(str: L{Prefix})
    @ivar duplicatesymbols: the set of prefixes, which map to the same symbol
    @type duplicatesymbols: set(str)
    '''
    class Prefix(object):
        '''
        @summary: represents a unit prefix and the scaling information
        @ivar reference: a uniq prefix identifier
        @ivar symbol: a short representation of the prefix
        @type symbol: str
        @ivar base: the base of the scaling factor
        @type base: int
        @ivar exponent: the exponent of the scaling factor
        @type exponent: int
        @ivar scale: the scaling factor, which is base ** exponent
        @type scale: float
        '''
        def __init__(self, reference, symbol, base, exponent):
            '''
            @summary: constructor
            @param reference: the reference to the unit prefix
            @type reference: str
            @param symbol: a short form of the unit prefix
            @type symbol: str
            @param base: the base of the unit prefix, typically 2 or 10
            @type base: int
            @param exponent: the exponent of the unit prefix
            @type exponent: int
            '''
            scale = base ** exponent
            self._data = (reference, symbol, base, exponent, scale)
        def __str__(self):
            return self.symbol
        @property
        def reference(self):
            return self._data[0]
        @property
        def symbol(self):
            return self._data[1]
        @property
        def base(self):
            return self._data[2]
        @property
        def exponent(self):
            return self._data[3]
        @property
        def scale(self):
            return self._data[4]

    def __init__(self):
        '''
        @summary: constructor
        '''
        self.prefixes = {}
        self.duplicatesymbols = set()

    def __contains__(self, item):
        '''
        @summary: check the existence of a unit prefix
        @param item: a prefix or a prefix symbol
        @type item: L{Prefix} or str
        @return: True if the prefix is known by the L{PrefixManager}
        @rtype: bool
        @raise L{PrefixError}: Wrong item type
        '''
        if isinstance(item, self.Prefix):
            return item in self.prefixes.values()
        elif isinstance(item, str):
            for prefix in self.prefixes.values():
                if prefix.symbol == item:
                    return True
            return False
        else:
            raise PrefixError("Wrong item type %s" % item)
    
    def __len__(self):
        '''
        @summary: the number of prefixes known by the L{PrefixManager}
        @return: the number of prefixes known by the L{PrefixManager}
        @rtype: int
        '''
        return len(self.prefixes)
    
    def newPrefix(self, reference, symbol, base, exponent):
        '''
        @summary: generate a new unit prefix
        @param reference: the reference to the unit prefix
        @type reference: str
        @param symbol: a short form of the unit prefix
        @type symbol: str
        @param base: the base of the unit prefix, typically 2 or 10
        @type base: int
        @param exponent: the exponent of the unit prefix
        @type exponent: int
        @return: the new unit prefix
        @rtype: L{Prefix}
        @raise L{PrefixError}: Prefix with reference exists
        '''
        if self.prefixes.has_key(reference): 
            raise PrefixError("Prefix with reference %s already exists" % reference)
        if PrefixManager.__contains__(self, symbol):
            self.duplicatesymbols.add(symbol)
        prefix = self.Prefix(reference, symbol, base, exponent)
        self.prefixes[reference] = prefix
        return prefix

    def __getitem__(self, reference):
        '''
        @summary: look up the prefix in the L{PrefixManager} based on its reference
        @param reference: the reference to the unit prefix
        @type reference: str
        @return: the unit prefix found
        @rtype: L{Prefix}
        @raise L{PrefixError}: Prefix with reference not found
        '''
        if self.prefixes.has_key(reference):
            return self.prefixes[reference]
        raise PrefixError("Prefix with reference %s not found" % reference)