检查 class 对象的特定值并获取另一个键的值
Check for particular value from class object and get the value of another key
我已实现代码以使用 CLI 客户端访问数据库。
我可以根据需要获取值。
考虑以下代码:
# Establish the connection Cloudkitty
ck = client.get_client(kwargs_conn.get('cloudkitty_version'), **kwargs_conn)
list_services = ck.hashmap.services.list()
for services in list_services:
print services
print(type(services))
它将产生如下输出:
<hashmap.Service {u'service_id': u'2c6e0447-0cdb-4d12-8511-4544ba0d03b5', u'name': u'compute'}>
<class 'cloudkittyclient.v1.rating.hashmap.Service'>
<hashmap.Service {u'service_id': u'48297131-de33-48ad-b5b5-43d3a3177841', u'name': u'volume'}>
<class 'cloudkittyclient.v1.rating.hashmap.Service'>
被重新调整的输出曾经是一个 class 对象。
现在我需要检查返回的对象是否有特定的键值。准确地说,我想检查它是否具有 'name' 作为 'compute',如果是,我需要获得相同的 service_id。
有人告诉我我们如何才能达到同样的效果。
if services.get("name") == "compute":
id_ = services.get("service_id")
#assuming li = [] declaration above you could add that to a list
# of IDs
li.append(id_)
请注意,根据上面的打印,我假设 class 的行为类似于字典。
查看库中的一些 tests ,似乎您可以直接访问 service_id
:
等字段
for service in list_services:
if service.name == 'compute':
print(service.service_id)
或者,如果您想获取名称为 compute
的所有服务的服务 ID:
service_ids = [service.service_id for service in list_services if service.name == 'compute']
我已实现代码以使用 CLI 客户端访问数据库。
我可以根据需要获取值。
考虑以下代码:
# Establish the connection Cloudkitty
ck = client.get_client(kwargs_conn.get('cloudkitty_version'), **kwargs_conn)
list_services = ck.hashmap.services.list()
for services in list_services:
print services
print(type(services))
它将产生如下输出:
<hashmap.Service {u'service_id': u'2c6e0447-0cdb-4d12-8511-4544ba0d03b5', u'name': u'compute'}>
<class 'cloudkittyclient.v1.rating.hashmap.Service'>
<hashmap.Service {u'service_id': u'48297131-de33-48ad-b5b5-43d3a3177841', u'name': u'volume'}>
<class 'cloudkittyclient.v1.rating.hashmap.Service'>
被重新调整的输出曾经是一个 class 对象。
现在我需要检查返回的对象是否有特定的键值。准确地说,我想检查它是否具有 'name' 作为 'compute',如果是,我需要获得相同的 service_id。
有人告诉我我们如何才能达到同样的效果。
if services.get("name") == "compute":
id_ = services.get("service_id")
#assuming li = [] declaration above you could add that to a list
# of IDs
li.append(id_)
请注意,根据上面的打印,我假设 class 的行为类似于字典。
查看库中的一些 tests ,似乎您可以直接访问 service_id
:
for service in list_services:
if service.name == 'compute':
print(service.service_id)
或者,如果您想获取名称为 compute
的所有服务的服务 ID:
service_ids = [service.service_id for service in list_services if service.name == 'compute']