summaryrefslogtreecommitdiffstats
path: root/Monitoring/src/main/python/DataProcessing/Prefix.py
diff options
context:
space:
mode:
Diffstat (limited to 'Monitoring/src/main/python/DataProcessing/Prefix.py')
-rw-r--r--Monitoring/src/main/python/DataProcessing/Prefix.py119
1 files changed, 119 insertions, 0 deletions
diff --git a/Monitoring/src/main/python/DataProcessing/Prefix.py b/Monitoring/src/main/python/DataProcessing/Prefix.py
new file mode 100644
index 0000000..9778f87
--- /dev/null
+++ b/Monitoring/src/main/python/DataProcessing/Prefix.py
@@ -0,0 +1,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)