以下脚本从不同的文件格式派生架构。能给我看一下这个吗?这些改进是什么样子的:
def _null_allowed_in_cols(self):
for field in self.fields:
field["type"] = self._field_type_converter(field["type"])
if type(field["type"]) == list:
if "null" not in field["type"]:
field["type"].insert(0, "null")
else:
field["type"] = ["null", field["type"]]
def _field_type_converter(self, field_type) -> Union[str, list]:
if type(field_type) is dict or type(field_type) is avro.schema.ImmutableDict:
return field_type["type"]
elif type(field_type) is list:
return list(map(lambda x: self._field_type_converter(x), field_type))
else:
return field_type发布于 2021-08-23 16:09:42
_col_type您可以在这里使用生成器表达式返回第一个匹配项或None:
# go from this
def _col_type(self, column_name):
for column in self.fields:
if column["name"] == column_name:
return column
return None
# to this
def _col_type(self, column_name):
return next((
col['name'] for col in self.fields if col['name'] == column_name
), None)使用isinstance而不是type(val) == cls:
# instead of this
if type(field["type"]) == list:
# do something
# or this
if type(val) is dict:
# do something
# do this instead
if isinstance(field['type'], list):
# do something
# or
if isinstance(field_type, (dict, avro.schema.ImmutableDict)):
# do something在本声明中:
list(map(lambda x: self._field_type_converter(x), field_type))你其实不需要兰布达:
list(map(self._field_type_converter, field_type))https://codereview.stackexchange.com/questions/266308
复制相似问题