在不同时间从同一来源获取Dict值并进行比较

时间:2018-12-29 08:54:32

标签: python dictionary ceph

我正在开发一个ceph mgr插件。我有一个从self.get('health')得到的字典。 dict中的键和值根据系统的运行状况而变化。

我想实现的目标是在python中编写一个函数,该函数每1分钟获取一次self.get('health')的值,并将其与先前的值(即oldDictValuenewDictValue进行比较,其中{ {1}}是1分钟后获得的dict值。

newDictValueoldDictValue是在不同时间保存来自newDictValue的值的变量。

1 个答案:

答案 0 :(得分:0)

以下是我希望实现的拟议伪代码。我发现很难将其隐藏到可行的解决方案中

```  
   fun diff_health(old_health, new_health):
       old = {}
       new = {}
       updated = {}
      for code, item in new_health['checks'].iteritems():
          if code not in old_health['checks']:
              # it's a new alert
              new[code] = item
          else:
             old_item = old_health['checks'][code]
        if item['severity'] != old_item['severity'] or item['summary']['message']    != old_item['usmmary']['message']:
            # changed
            updated[code] = item
       for code, item in old_health['checks'].iteritems():
           if code not in new_health['checks']:
             # health alert has resolved
        ...
      return old, new, updated
```

new_health和old_health是在不同时间来自self.get('heatth')的值