如何通过循环传递 isinstance 中的元组?
How can i pass tuples in isinstance through loop?
我的代码:
def validate_record_schema():
"""Validate that the 0 or more Payload dicts in record
use proper types"""
err_path = "root"
try:
for record in test1:
for device in record.get('Payload', []):
payload = device.get('Payload', None)
if payload is None:
continue
device = payload["Device"]
key_data = ((device["ManualAdded"],bool), (device["Location"],str))
for i in key_data:
if not isinstance(i):
return False
except KeyError as err_path:
print("missing key")
return False
return True
print(validate_record_schema())
我想像下面那样做,但我做不到。
key_data = ((device["ManualAdded"],bool), (device["Location"],str))
for i in key_data:
if not isinstance(i):
return False
如果我像下面那样做它正在工作
if not isinstance((device["ManualAdded"],bool)):
return False
但我需要像 above.How 那样做,我可以这样做吗?
Json数据
test1 = [{'Id': '12', 'Type': 'DevicePropertyChangedEvent', 'Payload': [{'DeviceType': 'producttype', 'DeviceId': 2, 'IsFast': False, 'Payload': {'DeviceInstanceId': 2, 'IsResetNeeded': False, 'ProductType': 'product'
, 'Product': {'Family': 'home'}, 'Device': {'DeviceFirmwareUpdate': {'DeviceUpdateStatus': None, 'DeviceUpdateInProgress': None, 'DeviceUpdateProgress': None, 'LastDeviceUpdateId': None}, 'ManualAdded': False,
'Name': {'Value': 'Jigital60asew', 'IsUnique': True}, 'State': None, 'Location': "dg", 'Serial': None, 'Version': '2.0.1.100'}}}]}]
您可以使用 *
运算符扩展元组并将其成员作为单独的参数传递。
key_data = (('This is a string', str), ('This is a string', bool))
for i in key_data:
if isinstance(*i):
print("yes")
else:
print("no")
我的代码:
def validate_record_schema():
"""Validate that the 0 or more Payload dicts in record
use proper types"""
err_path = "root"
try:
for record in test1:
for device in record.get('Payload', []):
payload = device.get('Payload', None)
if payload is None:
continue
device = payload["Device"]
key_data = ((device["ManualAdded"],bool), (device["Location"],str))
for i in key_data:
if not isinstance(i):
return False
except KeyError as err_path:
print("missing key")
return False
return True
print(validate_record_schema())
我想像下面那样做,但我做不到。
key_data = ((device["ManualAdded"],bool), (device["Location"],str))
for i in key_data:
if not isinstance(i):
return False
如果我像下面那样做它正在工作
if not isinstance((device["ManualAdded"],bool)):
return False
但我需要像 above.How 那样做,我可以这样做吗?
Json数据
test1 = [{'Id': '12', 'Type': 'DevicePropertyChangedEvent', 'Payload': [{'DeviceType': 'producttype', 'DeviceId': 2, 'IsFast': False, 'Payload': {'DeviceInstanceId': 2, 'IsResetNeeded': False, 'ProductType': 'product'
, 'Product': {'Family': 'home'}, 'Device': {'DeviceFirmwareUpdate': {'DeviceUpdateStatus': None, 'DeviceUpdateInProgress': None, 'DeviceUpdateProgress': None, 'LastDeviceUpdateId': None}, 'ManualAdded': False,
'Name': {'Value': 'Jigital60asew', 'IsUnique': True}, 'State': None, 'Location': "dg", 'Serial': None, 'Version': '2.0.1.100'}}}]}]
您可以使用 *
运算符扩展元组并将其成员作为单独的参数传递。
key_data = (('This is a string', str), ('This is a string', bool))
for i in key_data:
if isinstance(*i):
print("yes")
else:
print("no")