'How to refactor multiple ifs inside for loop to address code complexity error at build stage
I've recursive function with multiple ifs inside for loop and it's failing on build because reported complexity is higher than allowed. I guess one way to fix this error is by refactoring it and taking if logic and for loop apart but I'm not sure how to implement it.
def build_specs(schema: StructType, path_prefix=None, custom_input_datatypes={}):
"""
schema: is glue_dynamic_dataframe.schema()
path_prefix: we're passing it as None
custom_input_datatypes: is dict object as {'id': 'int', 'created_at': 'timestamp', 'salary': 'long'}
"""
specs = []
for field in schema:
path_name = build_path_name(path_prefix, field.name)
if field.name in custom_input_datatypes:
spec = (path_name, f"cast:{custom_input_datatypes[field.name]}")
specs.append(spec)
elif isinstance(field.dataType, ChoiceType):
datatype = decide_datatype(field.dataType.choices)
spec = (path_name, f"cast:{datatype}")
specs.append(spec)
elif isinstance(field.dataType, StructType):
specs.extend(build_specs(field.dataType, path_name))
return specs
Can someone please suggest how to refactor it ensuring recursive function does't break?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
