When you give a developer a “robs_numbers” object, the developer can call the various filters in any order they want e.g. obj.filter1().filter2().filtern() ..
However generally the more numbers I filter out early, the faster the filtering is.
I want the developer to be able to call the filter() methods in any order they want.
The developer then gets back a robs_numbers object (or perhaps an iterable..).
However I want my methods (filters) to actually run in the order given by priority().
technically priority() should take the pair filter: [args], so that I can use any filter. priority() will then return the priority value
Here is some sample code, though you may change the object completely if it’s more pythonic:
from random import randint
data = [randint(0,100) for i in range(100)]
class robs_numbers():
def __init__(self,numbers,_min,_max):
self.numbers = iter(numbers)
self._min = _min #approx
self._max = _max #approx
def bigger(self,x):
print "priority = ", self.priority(x)
return robs_numbers([i for i in self.numbers if i>x],x,self._max)
def smaller(self,x):
print "priority = ", self.priority(x)
return robs_numbers([i for i in self.numbers if i<x],self._min,x)
def order(self,backwards=False):
print "priority = ", self.priority()
self.numbers = iter(sorted(self.numbers,reverse=backwards))
return self
# ...
# More filter methods
# ...
def priority(self,x=None):
#works out the order methods should be called to increase efficiency
if x is None:
return 99999
else:
return abs(x-(self._max-self._min)/2)
and example of two valid uses, however I want them both to be fast, right now only one is efficient:
def get():
return robs_numbers(data,0,100)
rf = get().bigger(80).smaller(90).order() #faster in this order
print list(rf.numbers), rf._min, rf._max
#priority = 30
#priority = 80
#priority = 99999
#[81, 81, 82, 83, 85, 86, 87, 87, 89] 80 90
rf = get().order().smaller(90).bigger(80) #slower in this order
print list(rf.numbers), rf._min, rf._max
#priority = 99999
#priority = 40
#priority = 35
#[81, 81, 82, 83, 85, 86, 87, 87, 89] 80 90
You’ll have to create some kind of lazily evaluated query set, and only execute all the filters whenever the developer calls
list()or something else that requires you to export the data to another object. Django has a good example of this in their query sets, which are lazy.So, you could write something to the effect of
Note that this code does zero filtering until the developer asks for the data contained inside a
RobsNumbersobject, so it is quite lazy.