jath03 / openrgb-python

A python client for the OpenRGB SDK
GNU General Public License v3.0
115 stars 22 forks source link

Add "get_device_by_name" helper #19

Closed shawnc722 closed 3 years ago

shawnc722 commented 3 years ago

Takes a string name, searches the devices for one matching that name, and returns that device. If given an invalid name, raises a ValueError.

jath03 commented 3 years ago

There will often be multiple devices with the same name (especially with RGB RAM), so I'd want this to return a list of devices, similar to the get_devices_by_type function. Otherwise, it would only ever return the first device of a certain name.

shawnc722 commented 3 years ago

I checked with my own setup and saw that all my names are unique (my RAM sticks are each zones of a shared device) and assumed it'd be that way for everyone. Updated the helper to make it "get_devices_by_name" so it returns a list, empty if there's no matches.

shawnc722 commented 3 years ago

Do you think it's worth adding a boolean to control whether or not an exact match is needed? I'm imagining something like:

def get_devices_by_name(self, name : str, exact : bool=True):
     if exact: return [device for device in self.devices if device.name == name]
     return [device for device in self.devices if name in device.name]

This would give it a bit more functionality but the only use cases for it I can see right now are already covered by get_devices_by_type.

jath03 commented 3 years ago

I like the idea, even if its only benefit is keeping me and other users from having to type in devices' full names. Also, maybe have the inexact version be case insensitive.

shawnc722 commented 3 years ago

Alright finally had time to get to this, everything looking good to you?