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
``````

#### #0

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

``````class RobsNumbers(object):
def __init__(self, numbers, min, max):
self._numbers = numbers
self._min = min
self._max = max
self._filters = []

def bigger(self, x):
new_obj = RobsNumbers(self._numbers, x, self._max)
new_obj._filters = self._filters + [GreaterThanFilter(x)]
return new_obj

# Other filter methods simply add a filter to the _filters list of the object

def __iter__(self):
# Here is the complicated optimization logic to reorder and execute the filters
pass
``````

Note that this code does zero filtering until the developer asks for the data contained inside a `RobsNumbers` object, so it is quite lazy.

#### 阅读全文

Tensorflow LinearR

Python中的多元组到两对元组？

include文件可以从另一个include文件调用函数吗？

asp.net core 使用中间件拦截请求和返回数据，并对数据进行加密解密。

Oracle中一个表名的最大长度是多少？

python 安装surprise库解决 c++tools错误问题

abp简单了解

Spring注解驱动开发(四)-----aop、声明式事务