简体   繁体   English

在任何条件下返回true的值?

[英]Value that returns true on any condition?

I have a function that loops through a large dataset. 我有一个循环大数据集的函数。 It takes multiple inputs and contains a lot of conditions. 它需要多个输入并包含许多条件。 For testing, I sometimes like to run the function without certain conditions, but it is a time consuming to uncomment the conditions - which is what I do know. 对于测试,我有时喜欢在没有特定条件的情况下运行该函数,但是取消注释条件是耗时的 - 这就是我所知道的。

Is it possible to give the function an input that always makes the condition true - both for strings and numeric values? 是否可以为函数提供始终使条件成立的输入 - 对于字符串和数值?

A simplified example: 一个简化的例子:

def function(a,b,c....):

    .....

    item = dict(zip(header,row))
    for row in list:
        if item["variable"] == a:
            print row

    ....

    return something

Now I want to print any row. 现在我要打印任何一行。

function(any,b,c...)

I thought about giving it item["variable"] as input, but this does not work. 我考虑过将item["variable"]作为输入,但这不起作用。

If you're looking for a function that returns true for any arguments: 如果您正在寻找一个对任何参数都返回true的函数:

def always(*args, **kw):
    return True

If you're looking for an object that always compares == to any object, you can almost do that: 如果您正在寻找一个始终将==与任何对象进行比较的对象,您几乎可以这样做:

class AlwaysEqual:
    def __eq__(self, other):
        return True

But just almost—if, say, you put this irresistible force of truthiness up against an immovable object of falsity, whoever's on the left will win. 但是,差不多 - 比如说,如果你把这种不可抗拒的真实力量对抗一个不可动摇的虚假对象,那么左边的人就会赢。

And if you want to overload multiple comparisons in ways that aren't consistent for any plausible value, like making it equal everything and also be less than everything, there's nothing stopping you. 如果你想以不同于任何合理价值的方式超载多重比较,比如让它与所有东西相等而且比所有东西都要小,那就没有什么能阻止你了。 (After all, math.nan is not equal to anything, including itself, and that's required by standards written by people who thought it through in great detail, so Python's not going to stop you from doing something similar.) (毕竟, math.nan不等于任何东西,包括它自己,并且这是由那些math.nan考虑它的人所写的标准所要求的,因此Python不会阻止你做类似的事情。)

Split the code into 'debug mode' and 'release mode' and use explicit check in conditions. 将代码拆分为“调试模式”和“释放模式”,并使用显式检入条件。 Stay away from fancy approaches as it will cause more troubles in future. 远离花哨的方法,因为它将来会带来更多的麻烦。

debug = True


if debug or len(something) > 2:
    # something always on in debug mode
    do_something()

if not debug and len(some_other_thing) > 7:
    # something always off in debug mode
    do_something_else()

Something like this could work. 像这样的东西可以工作。

def function(a,b,c,test=False):

    .....

    item = dict(zip(header,row))
    for row in list:
        if test:
            print row
        else:
            if item["vaariable"] == a:
                print row

    ....

    return something

You could use a function that takes a boolean and returns True if you're running tests, and the boolean's real value otherwise: 您可以使用一个带布尔值的函数,如果您正在运行测试则返回True否则返回布尔值的实际值:

def func(x, y, test=False):
    def _bool(b): 
        return True if test else b

    if _bool(foo == x):
        if _bool(bar == y):
            do_something()

I sometimes instrument my programs with a comparable item, turning debug tracing off and on. 我有时用一个类似的项目来检测我的程序,关闭和打开调试跟踪。 In your case, imbue each condition with a short-circuiting or on a flag: 在您的情况下,通过短路or标志来灌输每个条件:

all_true = True
# all_true = False
...

    if all_true or item["vaariable"] == a:
...

Do this once; 这样做一次; you can now control it by changing the comment mark. 您现在可以通过更改注释标记来控制它。 Perhaps more useful, you can turn it on and off during the run. 也许更有用,您可以在运行期间打开和关闭它。

From reading your code I think you need a Python object which when you use == to compare it to any other Python object the result always will be True ; 从阅读代码开始,我认为你需要一个Python对象,当你使用==将它与任何其他Python对象进行比较时,结果总是为True ;

First you need to define a class the property I mentioned then use one instance of it instead of any to call the function : 首先,您需要定义一个类,我提到的属性然后使用它的一个实例而不是any来调用该function

class AnyObject(object):
    def __eq__(self, anything):
        return True

1 == AnyObject()
'' == AnyObject()
None == AnyObject()

Your code: 你的代码:

def function(a,b,c....):

    .....

    item = dict(zip(header,row))
    for row in list:
        if item["variable"] == a:
            print row

    ....

    return something

You only need to use an instance of AnyObject class in place of any when you call function : 在调用function时,只需要使用AnyObject类的实例代替any

any = AnyObject()
function(any,b,c...)

Now the condition is true for all the rows! 现在条件适用于所有行!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM