我有一本结构如下的字典:
D = {
'rows': 11,
'cols': 13,
(i, j): {
'meta': 'random string',
'walls': {
'E': True,
'O': False,
'N': True,
'S': True
}
}
}
# i ranging in {0 .. D['rows']-1}
# j ranging in {0 .. D['cols']-1}
我想写一个函数,它接受一个任意对象作为参数,并检查它是否具有该结构。这是我写的:
def well_formed(L):
if type(L) != dict:
return False
if 'rows' not in L:
return False
if 'cols' not in L:
return False
nr, nc = L['rows'], L['cols']
# I should also check the int-ness of nr and nc ...
if len(L) != nr*nc + 2:
return False
for i in range(nr):
for j in range(nc):
if not ((i, j) in L
and 'meta' in L[i, j]
and 'walls' in L[i, j]
and type(L[i, j]['meta']) == str
and type(L[i, j]['walls']) == dict
and 'E' in L[i, j]['walls']
and 'N' in L[i, j]['walls']
and 'O' in L[i, j]['walls']
and 'S' in L[i, j]['walls']
and type(L[i, j]['walls']['E']) == bool
and type(L[i, j]['walls']['N']) == bool
and type(L[i, j]['walls']['O']) == bool
and type(L[i, j]['walls']['S']) == bool):
return False
return True
虽然它很有效,但我一点也不喜欢它。有没有Pythonic式的方法来做到这一点?
我只被允许使用标准库。
发布于 2017-05-06 21:56:41
首先,我认为更“Pythonic”式的事情可能是请求宽恕而不是许可-当你需要一个属性时,检查数据结构是否有这个属性。
但是另一方面,如果你被要求创建一些东西来检查它是否格式良好,那么这并没有什么帮助。:)
因此,如果需要检查,可以使用the schema library之类的东西来定义数据结构应该是什么样子,然后根据该模式检查其他数据结构。
发布于 2017-05-07 00:50:57
您可以像这样编写验证(来自Scala提取器的想法)。优点是验证器结构类似于要测试的结构。
一个缺点是,过多的函数调用可能会使它变得更慢。
class Mapping:
def __init__(self, **kwargs):
self.key_values = [KeyValue(k, v) for k, v in kwargs.items()]
def validate(self, to_validate):
if not isinstance(to_validate, dict):
return False
for validator in self.key_values:
if not validator.validate(to_validate):
return False
return True
class KeyValue:
def __init__(self, key, value):
self.key = key
self.value = value
def validate(self, to_validate):
return self.key in to_validate and self.value.validate(to_validate[self.key])
class Boolean:
def validate(self, to_validate):
return isinstance(to_validate, bool)
class Integer:
def validate(self, to_validate):
return isinstance(to_validate, int)
class String:
def validate(self, to_validate):
return isinstance(to_validate, str)
class CustomValidator:
def validate(self, to_validate):
if not Mapping(rows=Integer(), cols=Integer()).validate(to_validate):
return False
element_validator = Mapping(meta=String(), walls=Mapping(**{k: Boolean() for k in "EONS"}))
for i in range(to_validate['rows']):
for j in range(to_validate['cols']):
if not KeyValue((i, j), element_validator).validate(to_validate):
return False
return True
d = {
'rows': 11,
'cols': 13,
}
d.update({(i, j): {
'meta': 'random string',
'walls': {
'E': True,
'O': False,
'N': True,
'S': True
}
} for i in range(11) for j in range(13)})
assert CustomValidator().validate(d)
覆盖isinstance(使用Python3.5测试)也是如此
class IsInstanceCustomMeta(type):
def __instancecheck__(self, instance):
return self.validate(instance)
def create_custom_isinstance_class(f):
class IsInstanceCustomClass(metaclass=IsInstanceCustomMeta):
validate = f
return IsInstanceCustomClass
def Mapping(**kwargs):
key_values = [KeyValue(k, v) for k, v in kwargs.items()]
def validate(to_validate):
if not isinstance(to_validate, dict):
return False
for validator in key_values:
if not isinstance(to_validate, validator):
return False
return True
return create_custom_isinstance_class(validate)
def KeyValue(key, value):
return create_custom_isinstance_class(lambda to_validate: key in to_validate and isinstance(to_validate[key], value))
def my_format_validation(to_validate):
if not isinstance(to_validate, Mapping(rows=int, cols=int)):
return False
element_validator = Mapping(meta=str, walls=Mapping(**{k: bool for k in "EONS"}))
for i in range(to_validate['rows']):
for j in range(to_validate['cols']):
if not isinstance(to_validate, KeyValue((i, j), element_validator)):
return False
return True
MyFormat = create_custom_isinstance_class(my_format_validation)
d = {
'rows': 11,
'cols': 13,
}
d.update({(i, j): {
'meta': 'random string',
'walls': {
'E': True,
'O': False,
'N': True,
'S': True
}
} for i in range(11) for j in range(13)})
assert isinstance(d, MyFormat)
发布于 2017-05-06 22:20:17
如果您的格式更简单,我会同意其他答案/注释,使用现有的模式验证库,如schema和voluptuous。但是,考虑到您必须检查具有元组关键字的字典的特定情况,以及那些元组的值取决于您的字典中其他成员的值,我认为您最好编写自己的验证器,而不是试图诱使模式适合您的格式。
https://stackoverflow.com/questions/43821195
复制相似问题