I am recording the input from a serial connection.
At the moment, I have been using a basic class that the input is run through updating self.min & self.max
But perhaps there is a more efficient way to do this?
perhaps a deque? would this have efficiency benefits?
perhaps a class is not needed?
An example of one that I have used:
class RangeFinder(object):
def __init__(self, _mem=3) :
self._mem = deque(maxlen=_mem)
self.absmax = -10**10
self.absmin = 10**10
self.relmax = None
self.relmin = None
def read(self, _data) :
if _data != None:
self._mem.append(_data)
if len(self._mem) == self._mem.maxlen :
self.relmax = max(self._mem)
self.absmax = max(self.absmax, self.relmax)
self.relmin = min(self._mem)
self.absmin = min(self.absmin, self.relmin)
another idea:
class MinMax(deque):
def __init__(self):
deque.__init__(self, [ 10**10, -10**10 ], maxlen=2)
def read(self, _data):
if _data < self[0]:
self.popleft()
self.appendleft(_data)
if _data > self[1]:
self.pop()
self.append(_data)
input is a string that is converted to an int. I have only recently begun using the serial connection as a generator so there are some discrepancies.
The min and max are then used to calculate the intensity of a lazer for reading binary data.
I would go for a bisection here to maintain the order when you input data. So that the lookup for the min/max will be O(1) and the load would go on the insertion (O(nlogn)). Also I don’t think you need a class here, you can safely implement only functions (if the problem is as you described it).