dimanche 12 juin 2016

key error when no dictionary is required


I have a function which sets the shutter on a camera and takes a float as input:

def changeShutter(value):
    global camera, shutter
    shutter['abs_value']+=value
    try:
        camera.set_property(**shutter)
    except:
        print "could not set shutter"

where shutter is a dictionary containing all the properties required for the shutter, and abs_value is the key whose value needs to be changed then set.

I can call this easily enough in a jupyter notebook I use for development with changeShutter(0.05) and it works just fine.

I then created a simple html button on a web page which sends a message to a flask-socket server containing the changeShutter function and, depending on the button pressed and the message therefore sent, it parses 0.05 or -0.05 like below:

@socketio.on('shutter request', namespace='/test')
def changeShutter(message):
    request = message['data']
    print 'Shutter request received: %s' %request
    if str(request) == "shutter increase":
        changeShutter(0.05)
    elif str(request) == "shutter decrease":
        changeShutter(-0.05)

I clearly receive one of the 2 possible options and correctly enter the correct if statement (I have tried debugging with extra print statements), but it throws a key error: 0.05 at me for some reason.

When the function does not require a dictionary input, why do I get a key error?


Aucun commentaire:

Enregistrer un commentaire