librelist archives

« back to archive

Suggestions

Suggestions

From:
Javier Garzon Aparicio
Date:
2011-11-01 @ 13:51
Greetings to all
I have a suggestion regarding the handling of attributes in the device
class, in the function __getitem__ on line 885

 def __getitem__(self, attribute):
        value = libudev.udev_device_get_sysattr_value(
            self.device, ensure_byte_string(attribute))
        if value is None:
            raise KeyError('No such attribute: {0}'.format(attribute))  <<<<<
        return value

I do not think it appropriate for a library just to close the program
because it dont find an attribute, I suggest that:

 def __getitem__(self, attribute):
        value = libudev.udev_device_get_sysattr_value(
            self.device, ensure_byte_string(attribute))
        if value is None:
            return None
        return value

Or define another hasattr function:

def hasattr(self, attribute):
        value = libudev.udev_device_get_sysattr_value(
            self.device, ensure_byte_string(attribute))
        if value is None:
            return False
        return True

That would be all.

PD:
I apologize for my bad English

--
Saludos a todos

Re: [pyudev] Suggestions

From:
Sebastian Wiesner
Date:
2011-11-01 @ 18:34
2011/11/1 Javier Garzon Aparicio <lanstat@gmail.com>:
> Greetings to all
> I have a suggestion regarding the handling of attributes in the device
> class
> […]
> I do not think it appropriate for a library just to close the program
> because it dont find an attribute

It doesn't close the program, it merely raises an exception and thus
uses the standard Python way of error reporting, which you should be
familiar with, if you're doing serious Python programming.  If not,
please read the official tutorial on exceptions [1].

[1] http://docs.python.org/tutorial/errors.html#exceptions

Re: [pyudev] Suggestions

From:
Javier Garzon Aparicio
Date:
2011-11-04 @ 00:15
Thank you very much for the answer.
I will document more about the exceptions


On Tue, Nov 1, 2011 at 4:34 PM, Sebastian Wiesner
<lunaryorn@googlemail.com> wrote:
> 2011/11/1 Javier Garzon Aparicio <lanstat@gmail.com>:
>> Greetings to all
>> I have a suggestion regarding the handling of attributes in the device
>> class
>> […]
>> I do not think it appropriate for a library just to close the program
>> because it dont find an attribute
>
> It doesn't close the program, it merely raises an exception and thus
> uses the standard Python way of error reporting, which you should be
> familiar with, if you're doing serious Python programming.  If not,
> please read the official tutorial on exceptions [1].
>
> [1] http://docs.python.org/tutorial/errors.html#exceptions
>



-- 
Saludos a todos