Home > Software design >  Python selecting variables based inside function
Python selecting variables based inside function

Time:11-02

I'm trying to create function that can pull information from an API library and I am having a hard time figuring out how to pass a variable selection into the function without being read as a value.

code example:

def get_list(api, val = None):
    response =[]
    list = api
    for i in list:
        response.append(f'i.{val}')
    return (response)


devices = get_list(api.devices.all(), 'name')

print(devices )

Give me a long list of "i.name"

I need to resolve i.name as a variable selection and not as an actual value

I have tired:

response.append(vars()[f'i.{val}']) # locals(), globals()

But I I get the error: "KeyError: 'i.name'"

I think the problem is the 'i.name' doesn't really exist as a variable within the function.

CodePudding user response:

When you call this function with devices, you are passing the argument 'name' as your "val" which you then loop over and append to results.

It would be helpful if you could also include what your desired result here would be since that is not clear at the moment.

CodePudding user response:

I'm using a LibrenmsAPI library to read data from LibreNMS

regular GET request would look like:

ip_list []
for device in lnms.devices.all():
    ip_list.append(device.ip)

name_list []
for device in lnms.devices.all():
    ip_list.append(device.sysName)

So in order not have to make every variation of an api request I need, the idea would be to have a "list" function.

CodePudding user response:

From what I understand is that you want to write a function so you will not have to repeat the for

ip_list = []
for device in lnms.devices.all():
    ip_list.append(device.ip)

name_list = []
for device in lnms.devices.all():
    ip_list.append(device.sysName)

First of all on your code, you are using list as a variable which you should not cause list is a built-in keyword.

This comment explains why your code doesn't work

This is how it would work

def get_data(devices, key):
    response = []
    for device in devices:
        response.append(device[key])
    return response

Or even shorter using list comprehension

def get_data(devices, key):
    return [device[key] for device in devices]

and then you can do

ip_list = []
ip_list = get_data(lnms.devices.all(), 'ip')

name_list = []
name_list = get_data(lnms.devices.all(), 'sysName')

And if you don want to repeat lnms.devices.all() You can write the functions like

def get_data_from_devices(key):
    response = []
    for device in lnms.devices.all():
        response.append(device[key])
    return response

Or with list comprehension

def get_data_from_devices(key):
    return [device[key] for device in lnms.devices.all()]

And then you would call them as

ip_list = []
ip_list = get_data_from_devices('ip')

name_list = []
name_list = get_data_from_devices('sysName')

PS: Needs better names for functions

  • Related