Hi All
Looking to print a sorted list of device based on the device name, item['name'] field:
for item in response:
if value.lower() in item['model'] and item['name'] != '':
print(f"{item['serial']}: {item['name']}")
elif value.title() in item['model'] and item['name'] != '':
print(f"{item['serial']}: {item['name']}")
else:
if value.upper() in item['model'] and item['name'] != '':
print(f"{item['serial']}: {item['name']}")
I tried the field.sort(), sorted(field) but closes I got was it sorted literally every little in my list.
Please advise 🙂
Solved! Go to solution.
Just a guess, but if a device does not have a name assigned, many API calls will return an object called None instead of the name. None is not a string, it's type is NoneType.
I'd think this is high probability if you are using a call that returns inventory as unused devices will not have a name.
The same type of thing can happen with other calls/elements, you usually find out the hard way that an API call can do this!
In dashboard UI, it defaults missing names to the device MAC address, but the API generally does not do this, if there is no name, you get None.
So in your code, the error is because None is not acceptable.
I've not tested, but try this instead, it might work...
SortedListOfDevices = sorted(response, key=lambda d: d['mac'] if d['name'] is None else d['name'])
The reponse is usually a list, where each element is a dictionary. So you need to sort the list based on values that are further "down the chain", which are in a dictionary.
You can sort the list using the sorted() method and pass a lambda function as key;
newlist = sorted(list_to_be_sorted, key=lambda d: d['name'])
Thank you!
Not to be a nimrod but how would I do that with this scenario? Its really not a straightforward thing, at least to me. Would it be......oh and 'value' is the variable for the input I am searching for
newlist = sorted(value, key=lambda d: d['item(name)'])
or
newlist = sorted(value, key=lambda d: d['name']
Thoughts?
Assuming you're getting a list of all appliances in a single organization, and want to sort according to name, it'd be like this
organization_id = ""
response = dashboard.organizations.getOrganizationInventoryDevices(
organization_id,
total_pages='all',
productTypes = ['appliance']
)
SortedListOfDevices = sorted(response, key=lambda d: d['name'])
Great thank you much apricated!
Ok so I tried that but it is now giving me an error of
Traceback (most recent call last):
File "/mnt/m2ssd/python/meraki_get_devices.py", line 21, in <module>
SortedListOfDevices = sorted(response, key=lambda d: d['name'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '<' not supported between instances of 'NoneType' and 'str'
Thoughts?
response = dashboard.organizations.getOrganizationInventoryDevices(
organization_id, total_pages='all'
)
SortedListOfDevices = sorted(response, key=lambda d: d['name'])
value = input("Find: ")
for item in SortedListOfDevices:
if value.lower() in item['model'] and item['name'] != '':
print(f"{item['serial']}: {item['name']}")
elif value.title() in item['model'] and item['name'] != '':
print(f"{item['serial']}: {item['name']}")
else:
if value.upper() in item['model'] and item['name'] != '':
print(f"{item['serial']}: {item['name']}")
Just a guess, but if a device does not have a name assigned, many API calls will return an object called None instead of the name. None is not a string, it's type is NoneType.
I'd think this is high probability if you are using a call that returns inventory as unused devices will not have a name.
The same type of thing can happen with other calls/elements, you usually find out the hard way that an API call can do this!
In dashboard UI, it defaults missing names to the device MAC address, but the API generally does not do this, if there is no name, you get None.
So in your code, the error is because None is not acceptable.
I've not tested, but try this instead, it might work...
SortedListOfDevices = sorted(response, key=lambda d: d['mac'] if d['name'] is None else d['name'])
That is true. I do have 1 that is named NONE. I will give it at try. Thanks much!
That did it. Thanks.
Do you have devices in your inventory that aren't named? That's probably the reason.
Yeah, Sungod beat you to the answer.
So I am entering a value that I want to filter on, VALUE. That works as expected but now would like to sort on the item('name') field from my query. The data is a list of my switches. So the filtered list comes out as <serail#>:<name>. I understand the sorted() option but the way my fields are named I can't seen to get it to work. Here is my code again. I added the input line at the top this time.
value = input("Find: ")
for item in response:
if value.lower() in item['model'] and item['name'] != '':
print(f"{item['serial']}: {item['name']}")
elif value.title() in item['model'] and item['name'] != '':
print(f"{item['serial']}: {item['name']}")
else:
if value.upper() in item['model'] and item['name'] != '':
print(f"{item['serial']}: {item['name']}")