0

I have a bunch of variables in a Python script like this:

pauseTime = 10
cameraSrc = "rtsp://cameraadress"
useCuda = True

But I'm getting from an API the new values for this variables and I'm trying to do the following:

for key,value in func.configJsonIot.items():
    if key == "debug":
        continue
    else:
        if debug:
            print("Sobrescrevendo valores locais pelos da API key: {} | value: {}".format(key, value))
        eval(str(key)) = value

this is returning me this error:

SyntaxError: cannot assign to function call

How can I change the global variables with the API values?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Jasar Orion
  • 626
  • 7
  • 26
  • 3
    Is there a reason you're not just using a dictionary? Then you can do `var_dict = {}; var_dict["useCuda"] = False; print(var_dict["useCuda"])`. – Carcigenicate Oct 22 '20 at 18:14
  • 1
    The proposed duplicate is not just indirectly on-point; it has answers describing the same `globals()` practice the OP's own answer uses. – Charles Duffy Oct 22 '20 at 20:09

2 Answers2

0

assign a variable to the function and then use that in the for loop.

so

json_items = func.confJsonIot.items()

then

for key,value in json_items: ...

this will give you a dict called json_items that you can use in the print statement.

Timus
  • 10,974
  • 5
  • 14
  • 28
0

i found the solution for this:

for key,value in func.configJsonIot.items():
    if key == "debug":
        continue
    else:
        if debug:
            print("Sobrescrevendo valores locais pelos da API key: {} | value: {}".format(key, value))
        globals()[key]=value

globals() function can acess all global variables running

Jasar Orion
  • 626
  • 7
  • 26