dimanche 19 février 2023

Improving wrapper function when handling different embedded if-else scenarios

I wish to refactor this fugly function - running on an object collection robot - handling all possible sounds in response to stimuli of different data types.

How can I Pythonically improve the following?

'''
Produce different sounds based on various parameters comprising robot's
proximity to different objects & time taken for collecting said objects.

`obj` := nearest object captured within front-camera's field of view
`timeSinceAnyObjLastCollected` := global timer

`objPassThruInfraRedEntryGates` := returns bool
`cameraSensorWithinTwoFeetOfObj` := returns bool
'''
def soundManager(obj, timeSinceAnyObjLastCollected):
    ## Order of these if-statements determine sound-output precedence
    if timeSinceAnyObjLastCollected > 60:
        timeSinceAnyObjLastCollected = 0    ## Reset timer
        playSound("A")

    elif objPassThruInfraRedEntryGates(obj):
        timeSinceAnyObjLastCollected = 0    ## Reset timer
        if obj.color == "RED":
            playSound("B")
        elif obj.color == "GREEN":
            playSound("C")

    elif cameraSensorWithinTwoFeetOfObj(obj)
        if obj.color == "RED":
            playSound("D")
        elif obj.color == "GREEN":
            playSound("E")

    else:
        playSound("F")

Moreover, is implementing this "catch-all" soundManager() any more scalably elegant than separately calling playSound(), where those situations indented within these if-statements are actually encountered within the script?

Aucun commentaire:

Enregistrer un commentaire