我需要对pysphere'sVIServer.get_datastore
返回的字典进行排序。它应该按数据存储上的可用空间排序,我们可以通过以下方式获得:
from pysphere import VIServer, VIProperty
if __name__ == "__main__":
server = VIServer()
print "Connecting to vSphere..."
server.connect(VSPHERE_IP, VSPHERE_USER, VSPHERE_PASS)
datastores = server.get_datastores()
for ds, name in datastores.iteritems():
props = VIProperty(server, ds)
freeSpace = props.summary.freeSpace
我知道我可以使用sorted(iterable[, cmp[, key[, reverse]]])并创建一个自定义排序函数,但我不知道该函数的参数将是哪种类型。你知道吗
我不能使用OrderedDict,因为我绑定到Python2.6(不要问为什么:()
只需在键函数中查找可用空间:
这将生成来自
datastores
的键的排序列表;您可以使用该列表按特定顺序进行迭代。我以为你想要的数据存储空间最大的列在第一位。你知道吗也可以获得排序的键值对,分别用
item
和item[0]
替换ds
:相关问题 更多 >
编程相关推荐