summaryrefslogtreecommitdiffstats
path: root/Monitoring/src/main/python/DataProcessing/Sampler.py.old
diff options
context:
space:
mode:
Diffstat (limited to 'Monitoring/src/main/python/DataProcessing/Sampler.py.old')
-rw-r--r--Monitoring/src/main/python/DataProcessing/Sampler.py.old191
1 files changed, 191 insertions, 0 deletions
diff --git a/Monitoring/src/main/python/DataProcessing/Sampler.py.old b/Monitoring/src/main/python/DataProcessing/Sampler.py.old
new file mode 100644
index 0000000..f1c0622
--- /dev/null
+++ b/Monitoring/src/main/python/DataProcessing/Sampler.py.old
@@ -0,0 +1,191 @@
+'''
+Created on Nov 20, 2012
+
+@author: steger
+'''
+from DataProcessing.DataReader import DataReader
+from DataProcessing.DataHeader import DataHeader
+from DataProcessing.DataSource import DataSource
+from DataProcessing.Data import Data
+from DataProcessing.DataError import SamplerError
+
+class Sampler(DataSource):
+ '''
+ classdocs
+ '''
+
+ def __init__(self, datasource):
+ '''
+ Constructor
+ @param datasource: table of records to manipulate with
+ @type datasource: DataSource
+ '''
+ if not isinstance(datasource, DataSource):
+ raise SamplerError("Wrong type of datasource %s" % datasource)
+ DataSource.__init__(self, datasource)
+ self._reader = DataReader(datasource = datasource._data)
+ header = DataHeader("%sSample(%s)" % (self.name, self.source.name))
+ for c in self._reader.headercells():
+ header.addColumn(c)
+ self._data = Data(self.um, header)
+
+ @property
+ def header(self):
+ return self._data.header
+
+ def __len__(self):
+ self.process()
+ return len(self._data)
+
+ def __getitem__(self, k):
+ return self._data._rawrecords.__getitem__(k)
+
+
+class Head(Sampler):
+ def __init__(self, datasource, head = 10):
+ '''
+ Constructor
+ @param datasource: table of records to manipulate with
+ @type datasource: DataSource
+ @param head: the top n records of the table
+ @type head: int
+ '''
+ Sampler.__init__(self, datasource)
+ self._head = head
+
+ @property
+ def name(self):
+ return "Head"
+
+ @property
+ def head(self):
+ return self._head
+ @head.setter
+ def head(self, head):
+ self._head = int(head)
+ self._data.clear()
+ self._reader.rewind()
+
+ def _process(self):
+ if self._reader.sourceCleared.isSet():
+ self._reader.sourceCleared.clear()
+ self._reader.rewind()
+ self._data.clear()
+ if len(self._data) == self.head:
+ return
+ for x in self._reader:
+ self._data._rawrecords.append(x)
+ if len(self._data) == self.head:
+ self._data._onexpand()
+ return
+ raise SamplerError("Not enough sample %d/%d" % (len(self._data), self.head))
+
+class Tail(Sampler):
+ def __init__(self, datasource, tail = 10):
+ '''
+ Constructor
+ @param datasource: table of records to manipulate with
+ @type datasource: DataSource
+ @param tail: the last n records of the table
+ @type tail: int
+ '''
+ Sampler.__init__(self, datasource)
+ self._tail = tail
+
+ @property
+ def name(self):
+ return "Tail"
+
+ @property
+ def tail(self):
+ return self._tail
+ @tail.setter
+ def tail(self, tail):
+ self._tail = int(tail)
+ self._data.clear()
+ self._reader.rewind()
+
+ def _process(self):
+ clear = False
+ if self._reader.sourceCleared.isSet():
+ self._reader.sourceCleared.clear()
+ self._reader.rewind()
+ self._data.clear()
+ for x in self._reader:
+ if len(self._data) == self.tail:
+ self._data._rawrecords.pop(0)
+ clear = True
+ self._data._rawrecords.append(x)
+ if clear:
+ self._data._onclear()
+ if len(self._data) == self.tail:
+ self._data._onexpand()
+ else:
+ raise SamplerError("Not enough sample %d/%d" % (len(self._data), self.tail))
+
+class Sorter(Sampler):
+ def __init__(self, datasource, keycell = None, ascending = True):
+ '''
+ Constructor
+ @param datasource: table of records to manipulate with
+ @type datasource: DataSource
+ @param keycell: the key column to use for sorting
+ @type keycell: CellRequest or None
+ @param ascending: indicate the sortin order
+ @type ascending: bool
+ '''
+ Sampler.__init__(self, datasource)
+ self._asc = ascending
+ self._key = 0
+ if keycell:
+ self.keycell = keycell
+
+ @property
+ def name(self):
+ return "Sort"
+
+ @property
+ def ascending(self):
+ return self._asc
+ @ascending.setter
+ def ascending(self, ascending):
+ if bool(ascending) != self._asc:
+ self._asc = bool(ascending)
+ self._data.clear()
+ self._reader.rewind()
+
+ @property
+ def key(self):
+ return self._key
+ @key.setter
+ def key(self, key):
+ self._key = int(key)
+
+ @property
+ def keycell(self):
+ raise SamplerError("don't read this property")
+ @keycell.setter
+ def keycell(self, cellrequest):
+ for idx, _ in self.source.header.getCell(cellrequest):
+ self._key = idx
+ break
+
+ def _process(self):
+ clear = False
+ if self._reader.sourceCleared.isSet():
+ self._reader.sourceCleared.clear()
+ clear = True
+ if self._reader.sourceExpanded.isSet():
+ self._reader.sourceExpanded.clear()
+ clear = True
+ if not clear:
+ return
+ self._reader.rewind()
+ self._data.clear()
+ self._data._rawrecords = sorted(self._reader.source._rawrecords, key=lambda r: r[self.key], reverse = not self.ascending)
+ if len(self._data):
+ self._data._onclear()
+ self._data._onexpand()
+ else:
+ raise SamplerError("Not enough sample...")
+