AWS Lambda Python 3.6 空值意外结果 (!= none:)



我有一个 Lambda Python 3.6 函数,用于检查 EC2 实例上是否存在特定标签和值。标记为"expenddate",该值将为空,或者日期格式为 mm/dd/yy 格式。我的目标是让函数检查标签是否存在,然后在满足两个条件时进行处理,1( 如果日期小于或等于当前日期,2( 值不为空(None(。这会根据日期正确处理,但当值为空时仍然会报告,但我不想要。

这是我代码的相关部分,特别是行"if (tag['Value']( <= mdy and (tag['Value']( != None:'。

for instance in ec2.instances.all():
if instance.tags is None:
continue
for tag in instance.tags:
if tag['Key'] == 'expenddate':
expiredInstances=[]
if (tag['Value']) <= mdy and (tag['Value']) != None:
print('Sending publish message')
sns_client.publish(
TopicArn = 'arn:aws:sns:us-east-1:704819628235:EOTSS-Monitor-Tag-Exceptions1',
Subject = '!!!! Tag Exception has Expired.',
Message = str("The tag exception for instance %s has expired in account %s" % (instance.id,acctnum)))
else:
print ("end")
return "sucess"

更改if条件以检查

if (tag['Value'](!= None 先,然后添加 if (tag['Value']( <= mdy。

参考短路评估

编辑: tag['Value'] 的返回类型是一个字符串,所以使用 None 进行比较是不好的,因为,

None 是 Python 中的一个特殊值,可用于表示变量没有可以有效使用的值 - 它没有有效的长度,不能用于计算等。

Null 或空字符串表示有一个字符串,但其内容为空,即 len(''(==0 对于 Python,首选术语"空字符串"。

因此,您的条件将是:(标签["值"](!= ''

最新更新