更简单的方法来检查条件?
我是 python 新手,我需要简化这个检查器。我如何将:更改
...
if c == '>' and ( prevTime > currentTime ):
c2 = True
elif c == '>=' and ( prevTime >= currentTime ):
c2 = True
...
为:
if prevTime | condition | currentTime:
doSomething()
我尝试使用 evaluate 或 compile,但在创建字符串期间,日期时间对象到字符串之间存在转换( >str 日期时间对象)。例如:
>>> 'result = %s %s %s' % (datetime.now(), '>', datetime.utcfromtimestamp(41))
'result = 2011-04-07 14:13:34.819317 > 1970-01-01 00:00:41'
并且不能比较。
有人可以帮我解决这个问题吗?下面的工作示例:
def checkEvent( prevEvent, currentEvent, prevTime, currentTime ):
def checkCondition( condition ):
#condition format
#tuple ( (oldEvent, newEvent), time, ip)
# eg: (('co', 'co'), '>=', '!=')
c1 = c2 = False
#check Event
if prevEvent == condition[0][0] and currentEvent == condition[0][1]:
c1 = True
else:
return False
#check time
if condition[1]:
c = condition[1]
if c == '>' and ( prevTime > currentTime ):
c2 = True
elif c == '>=' and ( prevTime >= currentTime ):
c2 = True
elif c == '<' and ( prevTime < currentTime ):
c2 = True
elif c == '<=' and ( prevTime <= currentTime ):
c2 = True
elif c == '==' and ( prevTime == currentTime ):
c2 = True
else:
c2 = True
return c1 and c2
def add():
print 'add'
def changeState():
print 'changeState'
def finish():
print 'finish'
def update():
print 'update'
conditions = (\
( ( ( 're', 'co' ), None ), ( add, changeState ) ),
( ( ( 'ex', 'co' ), None ), ( add, changeState ) ),
( ( ( 'co', 'co' ), '<' ), ( add, changeState ) ),
( ( ( 'co', 'co' ), '>=' ), ( add, changeState, finish ) ),
( ( ( 'co', 'co' ), '>=' ), ( update, ) ),
( ( ( 'co', 're' ), '>=' ), ( changeState, finish ) ),
( ( ( 'co', 'ex' ), '>=' ), ( changeState, finish ) )
)
for condition in conditions:
if checkCondition( condition[0] ):
for cmd in condition[1]:
cmd()
from datetime import datetime
checkEvent( 'co', 'co', datetime.utcfromtimestamp(41), datetime.now() )
checkEvent( 'ex', 'co', datetime.utcfromtimestamp(41), datetime.now() )
checkEvent( 'co', 'co', datetime.utcfromtimestamp(41), datetime.utcfromtimestamp(40) )
I'm new to python and I need to simplify this checker. How can I change:
...
if c == '>' and ( prevTime > currentTime ):
c2 = True
elif c == '>=' and ( prevTime >= currentTime ):
c2 = True
...
to something like:
if prevTime | condition | currentTime:
doSomething()
I've tried to use either evaluate or compile, but during creation of string there is conversion between datetime object to string ( str on datetime object). For example:
>>> 'result = %s %s %s' % (datetime.now(), '>', datetime.utcfromtimestamp(41))
'result = 2011-04-07 14:13:34.819317 > 1970-01-01 00:00:41'
and it can't be compared.
Can someone help me with this? Below working example:
def checkEvent( prevEvent, currentEvent, prevTime, currentTime ):
def checkCondition( condition ):
#condition format
#tuple ( (oldEvent, newEvent), time, ip)
# eg: (('co', 'co'), '>=', '!=')
c1 = c2 = False
#check Event
if prevEvent == condition[0][0] and currentEvent == condition[0][1]:
c1 = True
else:
return False
#check time
if condition[1]:
c = condition[1]
if c == '>' and ( prevTime > currentTime ):
c2 = True
elif c == '>=' and ( prevTime >= currentTime ):
c2 = True
elif c == '<' and ( prevTime < currentTime ):
c2 = True
elif c == '<=' and ( prevTime <= currentTime ):
c2 = True
elif c == '==' and ( prevTime == currentTime ):
c2 = True
else:
c2 = True
return c1 and c2
def add():
print 'add'
def changeState():
print 'changeState'
def finish():
print 'finish'
def update():
print 'update'
conditions = (\
( ( ( 're', 'co' ), None ), ( add, changeState ) ),
( ( ( 'ex', 'co' ), None ), ( add, changeState ) ),
( ( ( 'co', 'co' ), '<' ), ( add, changeState ) ),
( ( ( 'co', 'co' ), '>=' ), ( add, changeState, finish ) ),
( ( ( 'co', 'co' ), '>=' ), ( update, ) ),
( ( ( 'co', 're' ), '>=' ), ( changeState, finish ) ),
( ( ( 'co', 'ex' ), '>=' ), ( changeState, finish ) )
)
for condition in conditions:
if checkCondition( condition[0] ):
for cmd in condition[1]:
cmd()
from datetime import datetime
checkEvent( 'co', 'co', datetime.utcfromtimestamp(41), datetime.now() )
checkEvent( 'ex', 'co', datetime.utcfromtimestamp(41), datetime.now() )
checkEvent( 'co', 'co', datetime.utcfromtimestamp(41), datetime.utcfromtimestamp(40) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试制作操作员地图,如下所示:
You can try making a map of operators, like this:
人们做这样的事情:
Folks do things like this:
您是否正在寻找类似的内容:
您的评估失败了,因为您在评估之外进行了太多计算。
当然,eval 策略本身是丑陋的;你应该使用其他答案之一;)
Are you looking for something like:
Your evals are failing because you're doing too much of the computation outside the eval.
Of course, the eval strategy itself is ugly; you should use one of the other answers ;)
仅当函数未返回时才设置
c1 = True
,因此保证在函数末尾为 True。将其分解出来。该函数的输出将是相同的:
注意:其他人的“函数字典”方法是执行此操作的 Python 方法。我只是想演示您的原始函数的清理版本,它以您已经熟悉的方式工作,但流程更简单。
You're only setting
c1 = True
if the function doesn't return, so it's guaranteed to be True at the end of the function. Factor it out.The output of this function will be identical:
Note: everyone else's "dict of functions" approach is the Pythonic way to do this. I just wanted to demonstrate a cleaned-up version of your original function that works in a way already familiar to you but with a more straightforward flow.