我是python的新手,我需要简化这个检查器。我该如何改变:
...
if c == '>' and ( prevTime > currentTime ):
c2 = True
elif c == '>=' and ( prevTime >= currentTime ):
c2 = True
...
类似于:
if prevTime | condition | currentTime:
doSomething()
我尝试使用评估或编译,但在创建字符串期间,datetime对象与字符串之间存在转换( str 在datetime对象上)。例如:
>>> '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) )
答案 0 :(得分:8)
您可以尝试制作运算符地图,如下所示:
import operator
compares = {
'>': operator.gt,
'>=': operator.ge,
'<': operator.lt,
'<=': operator.le,
'==': operator.eq
}
def check(c, prev, current):
func = compares[c]
return func(prev, current)
print check('>', 5, 3) # prints: True
print check('>=', 5, 5) # prints: True
print check('<', 3, 5) # prints: True
print check('<=', 3, 3) # prints: True
print check('==', 7, 7) # prints: True
答案 1 :(得分:5)
人们做这样的事情:
result= { '=': lambda a, b: a == b,
'>': lambda a, b: a > b,
'>=': lambda a, b: a >= b,
etc.
}[condition]( prevTime, currentTime )
答案 2 :(得分:0)
您正在寻找类似的东西:
>>> eval('datetime.now() %s datetime.utcfromtimestamp(41)' % '>')
True
你的逃避失败了,因为你在eval之外做了太多的计算。
当然,评估策略本身很难看;你应该使用其他答案之一;)
答案 3 :(得分:0)
如果函数没有返回,则只设置c1 = True
,因此在函数结束时保证为True。把它排除在外。
此功能的输出将完全相同:
def checkEvent( prevEvent, currentEvent, prevTime, currentTime ):
def checkCondition( condition ):
#condition format
#tuple ( (oldEvent, newEvent), time, ip)
# eg: (('co', 'co'), '>=', '!=')
#check Event
if not (prevEvent == condition[0][0] and currentEvent == condition[0][1]):
return False
#check time
c = condition[1]
if not condition[1]:
return True
if c == '>' and ( prevTime > currentTime ):
return True
elif c == '>=' and ( prevTime >= currentTime ):
return True
elif c == '<' and ( prevTime < currentTime ):
return True
elif c == '<=' and ( prevTime <= currentTime ):
return True
elif c == '==' and ( prevTime == currentTime ):
return True
return False
注意:其他所有人的“函数词汇”方法都是Pythonic的方法。我只是想展示一个原始功能的清理版本,它以您已熟悉的方式工作,但流程更直接。