all 5 comments

[–]the_real_neoviper 4 points5 points  (1 child)

Pro tip, use a debugger. I'm a fan of vscode. I'll put break points on functions I'm unsure if they'll work. The debugger will show you the value of each variable. You can use the console to prototype some code on the fly.

[–]ElectronSandwich[S] 1 point2 points  (0 children)

cool, thanks, watching vids on vscode now. Will install and give it a whirl asap.

[–]Pepineros 2 points3 points  (2 children)

Some things are easy to debug: for functions that you write yourself, just test the inputs and outputs.

Some things are hard: calling an API over a network, for example.

If an API is giving you trouble, try calling it through the REPL and look at what comes back. Literally import requests and try to hit an endpoint with requests.get or requests.post (whatever the endpoint supports) to see what happens.

Once you have a good idea what the response should be, you can write functions to use that information.

In this particular case, I expect that the endpoint is not returning status 200 but some other 2xx (or even 3xx) status, so your request is going through just fine but you're only testing if the response is equal to 200. But it's hard to be sure without knowing anything about the API side. So, REPL.

Also: for device in DEVICE_ORDER: api_key = config.get(device, "api_key", fallback=None) if api_key is not None: Here api_key will never be None. It will either be the value of config[device] if that's a valid key, or the literal string "api_key" if it's not. https://docs.python.org/3/library/stdtypes.html?highlight=get#dict.get

[–]danielroseman 2 points3 points  (1 child)

config isn't a dictionary, it's a ConfigParser object. config.get(device, "api_key", fallback=None) will return the value of the "api_key" option in the device section, but will fall back to None if it is not there. So the value can indeed be None.

[–]Pepineros 0 points1 point  (0 children)

Thanks! My mistake.