[PATCH evemu 2/7] python: replace event_get_value with the C implementation

Peter Hutterer peter.hutterer at who-t.net
Thu Aug 14 20:28:38 PDT 2014


On Thu, Aug 14, 2014 at 02:00:40PM -0400, Benjamin Tissoires wrote:
> No functional change, but relying on an external lib removes the
> pain to maintain our own input.h implementation.
> 
> Signed-off-by: Benjamin Tissoires <benjamin.tissoires at gmail.com>
> ---
>  python/evemu/__init__.py | 27 ++++++++++++++++++++++++---
>  1 file changed, 24 insertions(+), 3 deletions(-)
> 
> diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
> index 02ff3a4..0797d73 100644
> --- a/python/evemu/__init__.py
> +++ b/python/evemu/__init__.py
> @@ -35,6 +35,8 @@ __all__ = ["Device",
>             "input_prop_get_value",
>             "input_prop_get_name"]
>  
> +_libevemu = evemu.base.LibEvemu()
> +
>  def event_get_value(event_type, event_code = None):
>      """
>      Return the integer-value for the given event type and/or code string
> @@ -44,10 +46,29 @@ def event_get_value(event_type, event_code = None):
>      If an event code is passed, the event type may be given as integer or
>      string.
>      """
> -    try:
> -        return evemu.event_names._event_get_value(event_type, event_code)
> -    except KeyError:
> +    t = -1
> +    c = -1
> +
> +    if type(event_type) == str:
> +        t = _libevemu.evemu_event_type_from_name(event_type)
> +    else:
> +        t = event_type

I had a chat with one of the python guys here about this (something has
always felt wrong about the type() checks) and the better solution here
seems to be:

if instanceof(event_type, int):
    t = event_type
else:
    t = _libevemu.evemu_event_type_from_name(str(event_type))

if event_code is None:
        if t < 0:
            return None
        else:
            return t
..

That should work as long as the stringified object resolves to something
useful, but catches the ints as well and doesn't have the ridgid type str
requirement.

It won't raise the type error for things like doubles or so, but then again
that's probably fine here anyway.

Cheers,
   Peter

PS: he also pointed out that "is None" and "is not None" is preferred over
== and !=. See second point in here:
http://legacy.python.org/dev/peps/pep-0008/#programming-recommendations

> +
> +    if event_code == None:
> +        if type(event_type) != str:
> +            raise TypeError("expected a string")
> +        if t < 0:
> +            return None
> +        return t
> +
> +    if type(event_code) != str:
> +        raise TypeError("expected a string")
> +
> +    c = _libevemu.evemu_event_code_from_name(t, event_code)
> +
> +    if c < 0:
>          return None
> +    return c
>  
>  def event_get_name(event_type, event_code = None):
>      """
> -- 
> 2.0.4
> 
> _______________________________________________
> Input-tools mailing list
> Input-tools at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/input-tools


More information about the Input-tools mailing list