Python 如何避免if/else语句中的重复代码?
在if/else语句中有两个完全相同的逻辑:Python 如何避免if/else语句中的重复代码?,python,algorithm,python-3.x,Python,Algorithm,Python 3.x,在if/else语句中有两个完全相同的逻辑: if alert.get('comment_time_created') is None: here-> args = {'is_comment_visible': 1, 'comment_time_created': current_comment_time} await self._db_alert.update_alert(alert['alert_id'], **args) else:
if alert.get('comment_time_created') is None:
here-> args = {'is_comment_visible': 1, 'comment_time_created': current_comment_time}
await self._db_alert.update_alert(alert['alert_id'], **args)
else:
first_comment_time_creation = datetime.strptime(alert['comment_time_created'], '%Y-%m-%dT%H:%M:%SZ')
current_comment_time = datetime.strptime(current_comment_time, '%Y-%m-%dT%H:%M:%SZ')
if current_comment_time > first_comment_time_creation:
await self._db_alert.update_alert(alert['alert_id'], is_comment_visible=1)
else:
here-> args = {'is_comment_visible': 1, 'comment_time_created': current_comment_time}
await self._db_alert.update_alert(alert['alert_id'], **args)
有没有办法一次执行此逻辑?您似乎在每个条件下都执行等待行,只是您的KWARG正在更改,您没有为一个特定条件创建
注释\u时间\u
参数。这可以简化为:
args = {'is_comment_visible': 1}
if alert.get('comment_time_created') is None:
args['comment_time_created'] = current_comment_time
else:
first_comment_time_creation = datetime.strptime(alert['comment_time_created'], '%Y-%m-%dT%H:%M:%SZ')
current_comment_time = datetime.strptime(current_comment_time, '%Y-%m-%dT%H:%M:%SZ')
if current_comment_time <= first_comment_time_creation:
args['comment_time_created']= current_comment_time
await self._db_alert.update_alert(alert['alert_id'], **args)
args={'is_comment_visible':1}
如果alert.get('comment\u time\u created')为无:
args['comment\u time\u created']=当前\u comment\u时间
其他:
first_comment_time_creation=datetime.strtime(警报['comment_time_created'],“%Y-%m-%dT%H:%m:%SZ”)
当前注释时间=datetime.strtime(当前注释时间,%Y-%m-%dT%H:%m:%SZ')
如果当前注释时间您似乎在每个条件下都执行等待行,那么您的KWARG正在更改,您没有为某个特定条件创建注释时间。这可以简化为:
args = {'is_comment_visible': 1}
if alert.get('comment_time_created') is None:
args['comment_time_created'] = current_comment_time
else:
first_comment_time_creation = datetime.strptime(alert['comment_time_created'], '%Y-%m-%dT%H:%M:%SZ')
current_comment_time = datetime.strptime(current_comment_time, '%Y-%m-%dT%H:%M:%SZ')
if current_comment_time <= first_comment_time_creation:
args['comment_time_created']= current_comment_time
await self._db_alert.update_alert(alert['alert_id'], **args)
args={'is_comment_visible':1}
如果alert.get('comment\u time\u created')为无:
args['comment\u time\u created']=当前\u comment\u时间
其他:
first_comment_time_creation=datetime.strtime(警报['comment_time_created'],“%Y-%m-%dT%H:%m:%SZ”)
当前注释时间=datetime.strtime(当前注释时间,%Y-%m-%dT%H:%m:%SZ')
如果当前注释时间则放弃该命令。仅在外部if结束后调用该方法。通过删除所有分支共有的表达式来简化块。
大概是这样的:
if alert.get('comment_time_created') is not None:
first_comment_time_creation = datetime.strptime(alert['comment_time_created'], '%Y-%m-%dT%H:%M:%SZ')
current_comment_time = datetime.strptime(current_comment_time, '%Y-%m-%dT%H:%M:%SZ')
if current_comment_time <= first_comment_time_creation:
comment_time_created = current_comment_time
await self._db_alert.update_alert(alert['alert_id'], is_comment_visible = 1, comment_time_created = comment_time_created)
如果alert.get('comment\u time\u created')不是None:
first_comment_time_creation=datetime.strtime(警报['comment_time_created'],“%Y-%m-%dT%H:%m:%SZ”)
当前注释时间=datetime.strtime(当前注释时间,%Y-%m-%dT%H:%m:%SZ')
如果当前注释时间则放弃该命令。仅在外部if结束后调用该方法。通过删除所有分支共有的表达式来简化块。
大概是这样的:
if alert.get('comment_time_created') is not None:
first_comment_time_creation = datetime.strptime(alert['comment_time_created'], '%Y-%m-%dT%H:%M:%SZ')
current_comment_time = datetime.strptime(current_comment_time, '%Y-%m-%dT%H:%M:%SZ')
if current_comment_time <= first_comment_time_creation:
comment_time_created = current_comment_time
await self._db_alert.update_alert(alert['alert_id'], is_comment_visible = 1, comment_time_created = comment_time_created)
如果alert.get('comment\u time\u created')不是None:
first_comment_time_creation=datetime.strtime(警报['comment_time_created'],“%Y-%m-%dT%H:%m:%SZ”)
当前注释时间=datetime.strtime(当前注释时间,%Y-%m-%dT%H:%m:%SZ')
如果当前注释时间,你不能将该字典保存在上面的变量中吗?这可能更适合@Carcigenicate,你能解释一下怎么做吗?@ipetr上面的if alert.get
在顶部,你不能只写args={'is\u comment\u visible':1,“comment\u time\u created”:current\u comment\u time}
,并删除其他args
行?@Carcigenicate是的,谢谢!!但是问题仍然存在:等待self.\u db\u alert.update\u alert(alert['alert\u id',**args)
这一行是重复的,你不可以把字典保存在上面的一个变量中吗?这可能更适合@Carcigenicate。你能解释一下怎么做吗?@ipetr在if alert上面。在顶部获取
,你不能只写args={'is_comment_visible':1,'comment_time_created':current_comment_time}
,然后删除其他args
行吗?@Carcigenicate是的,谢谢!!但问题仍然存在:wait self.\u db\u alert.update\u alert(alert['alert\u id',**args)
此行重复正确。在第一个条件之前,他似乎定义了当前注释时间
,请检查他对参数的首次初始化。只需删除第一个声明。本地变量comment\u time\u created
可能会在赋值之前被引用。我知道它可能会被引用。但正如@MohitC所说,我假设他已经指定了一个默认值。不正确。在第一个条件之前,他似乎定义了当前注释时间
,请检查他对参数的首次初始化。只需删除第一个声明。本地变量comment\u time\u created
可能会在赋值之前被引用。我知道它可能会被引用。但正如@MohitC所说,我假设他已经指定了一个默认值。