标签: conditional-statements

如何让 MSbuild 仅使用“DefineConstants”之一进行条件测试?

我在 C# 项目中将条件编译符号定义为“ABC;XYZ”,并且我可以在项目 XML 文件中使用以下内容,以便在 MSBuild 期间有条件包含项目代码文件:

<Compile Include="SomeFile.cs" Condition="$(DefineConstants)'=='ABC;XYZ'"/>
Run Code Online (Sandbox Code Playgroud)

但我需要类似以下的东西,但它不起作用:

<Compile Include="SomeFile.cs" Condition="$(DefineConstants)'=='ABC'"/>
Run Code Online (Sandbox Code Playgroud)

是否可以仅使用其中一个定义常量进行条件测试?

msbuild compilation include conditional-statements

6
推荐指数
1
解决办法
1369
查看次数

React 路由器重定向条件

我正在尝试制作一个按钮,仅在验证正确完成后将用户重定向到新页面。

有没有办法做这样的事情?如何在类方法内激活路由?

import validator from './validator';

class Example {

  constructor(props) {
    super(props)
    this.saveAndContinue = thos.saveAndContinue.bind(this)
  }

  saveAndContinue () {
    var valid = validator.validate(this.props.form)
    if (valid) {
      axios.post('/path')
      <Redirect to='/other_tab'>
    } else {
      validator.showErrors()
    }
  }

  render() {
    <button onClick={this.saveAndContinue}>Save and Continue</button>
  }

}
Run Code Online (Sandbox Code Playgroud)

conditional-statements reactjs react-router

6
推荐指数
1
解决办法
3万
查看次数

如何解决Python中的StopIteration错误?

我刚刚读了一堆关于如何处理 Python 中的 StopIteration 错误的帖子,我在解决我的特定示例时遇到了麻烦。我只想用我的代码打印出 1 到 20,但它打印出错误 StopIteration。我的代码是:(我是这里的新手,所以请不要阻止我。)

def simpleGeneratorFun(n):

    while n<20:
        yield (n)
        n=n+1
    # return [1,2,3]

x = simpleGeneratorFun(1)
while x.__next__() <20:
    print(x.__next__())
    if x.__next__()==10:
        break
Run Code Online (Sandbox Code Playgroud)

python generator break while-loop conditional-statements

6
推荐指数
1
解决办法
3万
查看次数

R (dplyr) 中重置的条件运行计数(累积和)

我正在尝试计算一个运行计数(即累积和),该计数以其他变量为条件,并且可以针对另一个变量的特定值进行重置。我正在 R 工作,dplyr如果可能的话,我更喜欢基于 - 的解决方案。

我想cumulative根据以下算法为运行计数创建一个变量:

  • 计算和cumulative组合内的运行计数 ( )idage
  • 对于每个后续的where 、和 ,将运行计数 ( cumulative) 加 1trialaccuracy = 0block = 2condition = 1
  • 对于每个、、 和,将运行计数 ( cumulative) 重置为 0 ,并且下一个增量从 1 恢复(不是之前的数字)trialaccuracy = 1block = 2condition = 1
  • 对于每个trialwhere block != 2, or condition != 1,将运行计数 ( cumulative) 保留为NA

这是一个最小的工作示例:

mydata <- data.frame(id = c(1,1,1,1,1,1,1,1,1,1,1),
                 age = …
Run Code Online (Sandbox Code Playgroud)

r conditional-statements cumulative-sum dplyr

6
推荐指数
1
解决办法
2475
查看次数

TypeScript 条件类型和类型防护

我在结合 TypeScript 的类型保护和条件类型时遇到一些问题。考虑:

export interface IThisThing {
    someProp: number;
}

export function isIThisThing(type: any): type is IThisThing { 
    return !!type.someProp;
}

export interface IThatThing {
    someOtherProp: string;
}

export function isIThatThing(type: any): type is IThatThing { 
    return !!type.someOtherProp;
}

function doAThing<T extends IThisThing | IThatThing>(
    data: T
): T extends IThisThing ? IThisThing : IThatThing {
    if (isIThisThing(data)) { 
        return data; // Type 'T & IThisThing' is not assignable to type 'T extends IThisThing ? IThisThing : IThatThing'.
    };
    return …
Run Code Online (Sandbox Code Playgroud)

types conditional-statements typescript

6
推荐指数
1
解决办法
1862
查看次数

检查 Firebase 中是否存在文档并根据图标返回

我想检查 Firebase 集合中是否存在特定文档。据此,我的应用程序应该显示一个彩色图标或灰色图标。我试图用一种方法来解决这个问题,它返回一个布尔值。在我的 Build Widget 中,我调用该方法并分配正确的图标。

这是我检查文档是否存在的方法:

checkIfLikedOrNot(reference) async{
   DocumentSnapshot ds = await reference.collection("likes").document(currentUser.uid).get();
   print(ds.exists);
   return ds.exists;
}
Run Code Online (Sandbox Code Playgroud)

打印在控制台中显示了正确的值,但我的构建小部件似乎忽略了一个事实,即布尔值为 true 并始终返回应该在集合中没有文档时显示的图标。

这是我的构建小部件的一部分:

 GestureDetector(
      child: checkIfLikedOrNot(list[index].reference) == true

      ? 
         Icon(
          Icons.favorite,
          color: Colors.red,
         )
      : 
         Icon(
         FontAwesomeIcons.heart,
         color: null,
         ),
 )
Run Code Online (Sandbox Code Playgroud)

这个说法哪里有问题?有任何想法吗?

conditional-statements firebase flutter google-cloud-firestore

6
推荐指数
1
解决办法
7049
查看次数

Python 构建动态 if 条件 - 或/和

我有以下示例,其中从 JSON 格式过滤并打印数据:

arr = [
    {
        "EffectiveDate": "2018-05-01T00:00:00Z",
        "IncludedQuantity": 0.0,
        "MeterCategory": "VM",
        "Unit": "1 GB/Month",
        "MeterName": "P4 Disks"
    },
    {
        "EffectiveDate": "2018-03-14T00:00:00Z",
        "IncludedQuantity": 0.0,
        "MeterCategory": "Storage",
        "MeterName": "P4 Disks"
    },
    {
        "EffectiveDate": "2017-04-01T00:00:00Z",
        "IncludedQuantity": 0.0,
        "MeterCategory": "VM",
        "Unit": "1 Hour",
        "MeterName": "P4 Disks"
    }
]

def get_data(getarr):
    for data in getarr:
        if data['MeterCategory'] == "VM"\
            and data['MeterName'] == "P4 Disks"\
                and data['Unit'] == "1 GB/Month":
            print(data)

get_data(arr)
Run Code Online (Sandbox Code Playgroud)

我想在函数 get_data 中动态指定“AND”条件。假设我想在以下条件下运行函数 get_data :

示例 1. 仅按“MeterCategory”搜索:

if data['MeterCategory'] == "VM": …
Run Code Online (Sandbox Code Playgroud)

python dynamic filter conditional-statements

6
推荐指数
1
解决办法
7833
查看次数

如何使提供程序假设_角色块有条件

我有一个provider块,我想为其提供assume_role属性,但前提是它没有在我的本地计算机上运行。

islocal我在所有环境文件中定义了一个变量.tfvars,只有本地文件的值为true.

这是provider块:

provider "aws" {
    region = var.region1
    profile = var.islocal == true ? "default" : null # ONLY USED LOCALLY
    
    assume_role {       # NOT TO BE USED LOCALLY
        role_arn = var.terraform_execution_role
    }
}
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 如果我将该role_arn属性设置为null是否会使该assume_role块无效?(即:与不存在相同)
  2. 如果该assume_role块确实有影响,即使值为role_arnnull我怎样才能在为 时完全删除var.islocaltrue

我考虑过动态块,但我不确定如何构建它。

amazon-web-services conditional-statements terraform terraform-provider-azure

6
推荐指数
1
解决办法
1765
查看次数

如何使C中的条件语句更紧凑?

我有一个使用块的代码片段if-else if-elseelse if (cardLength == 16) && (numberArray[0] == 5 && (numberArray[1] == 1 || numberArray[1] == 2 || numberArray[1] == 3 || numberArray[1] == 4 || numberArray[1] == 5))例如,我想知道在不改变逻辑的情况下缩短冗长的条件语句的任何潜在方法。在Python中,我可以这样做if (cardLength == 16) and (numberArray[0:2] in range(51,56)):在 C 中是否有任何特定的语法糖可以用于此目的?

if (cardLength == 15) &&
   (numberArray[0] == 3 && (numberArray[1] == 4 || numberArray[1] == 7))
{
    printf("AMEX\n");
}
else if (cardLength == 16) &&
        (numberArray[0] == 5 && (numberArray[1] == 1 || numberArray[1] == …
Run Code Online (Sandbox Code Playgroud)

c conditional-statements

6
推荐指数
1
解决办法
233
查看次数

查找至少有一行值大于 0.7 而所有其他行值小于 0.3 的列

我有一个数据框:

df = pd.DataFrame({
    'col1': [0.1, 0.8, 0.2, 0.12],
    'col2': [0.2, 0.9, 0.1, 0.1],
    'col3': [0.3, 0.2, 0.1, 0.2],
    'col4': [0.6, 0.7, 0.8, 0.9]
})

   col1  col2  col3  col4
0  0.10   0.2   0.3   0.6
1  0.80   0.9   0.2   0.7
2  0.20   0.1   0.1   0.8
3  0.12   0.1   0.2   0.9
Run Code Online (Sandbox Code Playgroud)

我想找到那些至少有一个行值大于 0.7 而其余所有行值小于 0.3 的列值。在上面的代码片段中,结果应该是:

['col1', 'col2']
Run Code Online (Sandbox Code Playgroud)

我尝试过以下代码:

selected_cols = []
for col in df.columns:
    values = df[col]
    if any(values > 0.7) and all(values < 0.3):
        selected_cols.append(col)

print(selected_cols)
Run Code Online (Sandbox Code Playgroud)

python conditional-statements dataframe pandas

6
推荐指数
1
解决办法
298
查看次数