什么是对对象方法的调用排序的最python方式

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

Tensorflow LinearR

推荐文章

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

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

推荐文章

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

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

推荐文章

移除http响应中的多余的头(X-AspNet-Version,Server等)

移除http响应中的多余的头(X-AspNet-Version,Server等)

推荐文章

来自命令行的蓝牙

来自命令行的蓝牙

推荐文章

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

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

推荐文章

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

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

推荐文章

排除特殊字符的正则表达式

排除特殊字符的正则表达式

推荐文章

如何命名接口的集合?

如何命名接口的集合?

推荐文章

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

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

推荐文章

从中选择化合物

从中选择化合物

推荐文章

每晚按XP计划任务运行构建-类路径问题

每晚按XP计划任务运行构建-类路径问题

推荐文章

abp简单了解

abp简单了解

推荐文章

在win32上编译idl文件时出现midl错误2025(midl不喜欢字符串<40>)

在win32上编译idl文件时出现midl错误2025(midl不喜欢字符串)

推荐文章

用WCF序列化linq实体

用WCF序列化linq实体

推荐文章

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

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