summaryrefslogtreecommitdiffstats
path: root/Monitoring/src/main/python/DataProcessing/Prefix.py
blob: 9778f87de342585052a73ed6a7a7acc899b4dba2 (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
from __future__ import with_statement
'''
Created on Feb 27, 2012

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

class PrefixManager(object):
    '''
    @summary: the unit prefix container
    '''
    class Prefix(object):
        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
        def _get_reference(self):
            return self._data[0]
        def _get_symbol(self):
            return self._data[1]
        def _get_base(self):
            return self._data[2]
        def _get_exponent(self):
            return self._data[3]
        def _get_scale(self):
            return self._data[4]


        symbol = property(_get_symbol,None,None)

        base = property(_get_base,None,None)

        exponent = property(_get_exponent,None,None)

        reference = property(_get_reference,None,None)

        scale = property(_get_scale,None,None)
    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 unit prefix or its symbol
        @type item: Prefix or str
        @return: True if the prefix is known by the PrefixManager
        @rtype: bool
        @raise 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 PrefixManager
        @return: the number of prefixes known by the 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: Prefix
        @raise 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 PrefixManager based on its reference
        @param reference: the reference to the unit prefix
        @type reference: str
        @return: the unit prefix found
        @rtype: Prefix
        @raise 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)