我自己找不到任何"好"答案的简单问题:
假设我有以下条件:
if 'foo' in mystring or 'bar' in mystring or 'hello' in mystring:
# Do something
pass
Run Code Online (Sandbox Code Playgroud)
or根据情况,声明的数量可以更长.
在不牺牲性能的情况下,是否有一种"更好"(更多Pythonic)的写法方式?
如果考虑使用,any()但它需要一个类似布尔元素的列表,所以我必须首先构建该列表(在此过程中放弃短路评估),所以我猜它效率较低.
非常感谢你.
我想要实现的是,如果他/她试图关闭页面或远离它而不先保存,则警告用户未保存的更改.
我设法让OnBeforeUnload()对话框弹出...但如果用户没有修改任何字段值,我根本不希望它显示.为此,我正在使用名为is_modified的隐藏输入字段,该字段以默认值false开始,并在编辑任何字段时翻转为true.
我尝试将change事件绑定到此is_modified字段以尝试检测值更改...然后才激活OnBeforeUnload.
$( '#is_modified' ).change( function() {
if( $( '#is_modified' ).val() == 'true' )
window.onbeforeunload = function() { return "You have unsaved changes."; }
});
Run Code Online (Sandbox Code Playgroud)
但是从我的想法来看,change()事件仅在这3个步骤之后起作用 - 一个场获得焦点,一个值被改变而场失去了焦点.在隐藏输入字段的情况下,我不确定这个接收和失去焦点部分是如何工作的!因此,永远不会激活onbeforeunload函数.
任何人都可以建议一种方法来维持is_modified的触发器吗?
谢谢.
我觉得以下应该是可能的我只是不确定采取什么方法.
我想要做的是使用include方法来塑造我的结果,即定义沿着对象图遍历的距离.但是......我希望这种遍历是有条件的.
something like...
dealerships
.include( d => d.parts.where(p => p.price < 100.00))
.include( d => d.parts.suppliers.where(s => s.country == "brazil"));
Run Code Online (Sandbox Code Playgroud)
我知道这不是有效的linq,事实上,它是非常错误的,但实质上我正在寻找一些方法来构建一个表达树,它将返回形状结果,相当于......
select *
from dealerships as d
outer join parts as p on d.dealerid = p.dealerid
and p.price < 100.00
outer join suppliers as s on p.partid = s.partid
and s.country = 'brazil'
Run Code Online (Sandbox Code Playgroud)
重点是加入条件.
我觉得这对esql来说是相当直接的,但我最喜欢的是动态构建表达式树.
一如既往,感谢任何建议或指导
有时,我觉得检查所有条件是否都是真的更容易,但是只处理"其他"情况.
我想我有时会觉得更容易知道某些东西是有效的,并假设所有其他情况都无效.
例如,假设我们只在出现问题时才真正关心:
object value = GetValueFromSomeAPIOrOtherMethod();
if((value != null) && (!string.IsNullOrEmpty(value.Prop)) && (possibleValues.Contains(value.prop)))
{
// All the conditions passed, but we don't actually do anything
}
else
{
// Do my stuff here, like error handling
}
Run Code Online (Sandbox Code Playgroud)
或者我应该改变它:
object value = GetValueFromSomeAPIOrOtherMethod();
if((value == null) || (string.IsNullOrEmpty(value.Prop)) || (!possibleValues.Contains(value.prop)))
{
// Do my stuff here, like error handling
}
Run Code Online (Sandbox Code Playgroud)
或者(我觉得很难看):
object value = GetValueFromSomeAPIOrOtherMethod();
if(!((value != null) && (!string.IsNullOrEmpty(value.Prop)) && (possibleValues.Contains(value.prop))))
{
// Do my stuff here, like error …Run Code Online (Sandbox Code Playgroud) 非常简单的问题.我知道这可能是一个很小的优化,但最终你会使用足够的if语句来解决它.
编辑:谢谢那些提供答案的人.
对于那些觉得需要打击我的人,要知道好奇心和对知识的渴望并不能转化为愚蠢.
非常感谢所有提出建设性批评的人.直到现在我才知道if(var)的能力.我很确定我现在会用它.;)
我有以下行作为更大的bash脚本的一部分:
if [ `packages/TinySVM-0.09/bin/svm_learn 2>&1| grep TinySVM | wc -l | cut -c0-7 | sed 's/^ *//g'` -eq 1 ]
Run Code Online (Sandbox Code Playgroud)
在运行脚本时,我得到:
./install.sh:219行:[: - eq:一元运算符预期
其中219行是上面的行.有什么修复建议吗?
我有一个bash脚本,我希望在没有附加tty的情况下保持安静(比如来自cron).我现在正在寻找一种方法,在一行中有条件地将输出重定向到/ dev/null.这是我想到的一个例子,但我将有更多的命令在脚本中输出
#!/bin/bash
# conditional-redirect.sh
if tty -s; then
REDIRECT=
else
REDIRECT=">& /dev/null"
fi
echo "is this visible?" $REDIRECT
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用:
$ ./conditional-redirect.sh
is this visible?
$ echo "" | ./conditional-redirect.sh
is this visible? >& /dev/null
Run Code Online (Sandbox Code Playgroud)
我不想做的是复制with-redirection或with-no-redirection变种中的所有命令:
if tty -s; then
echo "is this visible?"
else
echo "is this visible?" >& /dev/null
fi
Run Code Online (Sandbox Code Playgroud)
编辑:
如果解决方案能够为我提供一种以"安静"模式输出内容的方式,那将是很好的,例如,当某些事情确实出错时,我可能希望从cron获得通知.
我是CodeCeption的新手.
我想根据另一个断言结果做一个动作/断言,如下所示:
if ($I->see('message')){
$I->click('button_close');
}
Run Code Online (Sandbox Code Playgroud)
有可能吗?我试过了,但没办法.可能断言结果不适用于IF,但还有其他选择吗?
提前致谢!
最后Codeception现在有了这个功能performOn!!
http://codeception.com/docs/modules/WebDriver#performOn
在我从一名前员工继承的许多脚本中,我一直看到这种模式:
if (true $SOME_VAR)&>/dev/null; then
...
fi
Run Code Online (Sandbox Code Playgroud)
或者这个
(true $SOME_VAR)&>/dev/null || SOME_VAR="..."
Run Code Online (Sandbox Code Playgroud)
人工页面true说它总是返回true,因此我一直在想,这些检查有什么意义?在第一种情况下,then部件始终执行,在第二种情况下,从不执行右手部件.
如何检查20个变量是否都为真,或者20个变量都是假的?
如果可能的话,如果没有使用真的长
变量实际上是数组元素:
array('a'=> true,'b'=> true ...)
使它更清楚:
php conditional boolean-logic boolean conditional-statements
conditional ×10
if-statement ×5
bash ×3
boolean ×2
c# ×2
php ×2
assertion ×1
codeception ×1
dialog ×1
include ×1
javascript ×1
linq ×1
optimization ×1
python ×1
syntax-error ×1
triggers ×1