Home > front end >  Is there any way to lessen the amount of nested for loops/ if statements?
Is there any way to lessen the amount of nested for loops/ if statements?

Time:05-23

for node in nodes:
    if node.mesh is not None:
        for primitive in meshes[node.mesh].primitives:
            for i in range(max([e.value for e in attributes.Attributes])):
                if i == attributes.Attributes.POSITION.value:
                    accessor = accessors[primitive.attributes.POSITION]

                if i == attributes.Attributes.NORMAL.value:
                    accessor = accessors[primitive.attributes.NORMAL]

                if i == attributes.Attributes.TEXCOORD_0.value:
                    accessor = accessors[primitive.attributes.TEXCOORD_0]

                vectors = parse_accessors(resources, buffer_views, accessor)

                if i == attributes.Attributes.POSITION.value:
                    if node.translation:
                        vectors = node.translation   vectors
                    if node.rotation:
                        vectors = R.from_quat(node.rotation).apply(vectors)
                    if node.scale:
                        vectors = node.scale * vectors

                    vertices.append(vectors)

                if i == attributes.Attributes.NORMAL.value:
                    normals.append(vectors)

                if i == attributes.Attributes.TEXCOORD_0.value:
                    uvs.append(vectors)

            accessor = accessors[primitive.indices]

            vectors = parse_accessors(resources, buffer_views, accessor)

            indices.append(vectors)

            material = materials[primitive.material]

            images.append(
                textures[material.pbrMetallicRoughness.baseColorTexture.index].source
            )

I'm trying to improve my code and make it more readable/better to look at. One thing I'm trying to do is reduce redundancy as well as make my code flatter and more compact without making it more difficult to read. Are there any improvements that can be made? One thing that I don't like is how indented it is because of the nested for loops at the beginning. Any suggestions are appreciated.

CodePudding user response:

A couple of easy simplifications:

for node in nodes:
    # Avoid indentation by finding opportunities to interrupt the flow
    # of execution and continue outside of the if block.
    if node.mesh is None:
        continue

    for primitive in meshes[node.mesh].primitives:
        # You can iterate over an Enum directly, which saves you from 
        # a bunch of thrashing trying to match int values to enum values.
        for attribute in attributes.Attributes:
            accessor = accessors[attribute]

CodePudding user response:

You could look into utilizing Structural Pattern Matching if you are utilizing Python >=3.10, which is from PEP 622 What’s New In Python 3.10:

def http_error(status):
    match status:
        case 400:
            return "Bad request"
        case 401 | 403 | 404:
            return "Not allowed"
        case 405:
            return "Method not allowed"
        case _:
            return "the else case goes here"
  • Related