diff --git a/lib/hidapi/hidconsole.py b/lib/hidapi/hidconsole.py index 4547c5ec..9439f01f 100644 --- a/lib/hidapi/hidconsole.py +++ b/lib/hidapi/hidconsole.py @@ -23,10 +23,22 @@ except NameError: interactive = os.isatty(0) prompt = '?? Input: ' if interactive else '' +start_time = time.time() strhex = lambda d: hexlify(d).decode('ascii').upper() -is_string = lambda d: type(d) == (str if type(u'') == str else unicode) -start_time = time.time() +try: + unicode + # this is certanly Python 2 + is_string = lambda d: isinstance(d, unicode) or isinstance(d, str) + # no easy way to distinguish between b'' and '' :( + # or (isinstance(d, str) \ + # and not any((chr(k) in d for k in range(0x00, 0x1F))) \ + # and not any((chr(k) in d for k in range(0x80, 0xFF))) \ + # ) +except: + # this is certanly Python 3 + # In Py3, unicode and str are equal (the unicode object does not exist) + is_string = lambda d: isinstance(d, str) # # diff --git a/lib/logitech/unifying_receiver/common.py b/lib/logitech/unifying_receiver/common.py index 6af1d7b7..443787d9 100644 --- a/lib/logitech/unifying_receiver/common.py +++ b/lib/logitech/unifying_receiver/common.py @@ -6,9 +6,19 @@ from __future__ import absolute_import, division, print_function, unicode_litera from binascii import hexlify as _hexlify from struct import pack as _pack -# In Py3, unicode and str are equal (the unicode object does not exist) -is_string = lambda d: isinstance(d, str) or \ - (False if type(u'') == str else isinstance(d, unicode)) +try: + unicode + # this is certanly Python 2 + is_string = lambda d: isinstance(d, unicode) or isinstance(d, str) + # no easy way to distinguish between b'' and '' :( + # or (isinstance(d, str) \ + # and not any((chr(k) in d for k in range(0x00, 0x1F))) \ + # and not any((chr(k) in d for k in range(0x80, 0xFF))) \ + # ) +except: + # this is certanly Python 3 + # In Py3, unicode and str are equal (the unicode object does not exist) + is_string = lambda d: isinstance(d, str) class NamedInt(int): @@ -72,9 +82,10 @@ class NamedInts(object): def __init__(self, **kwargs): def _readable_name(n): if not is_string(n): - raise TypeError("expected string, got " + str(type(n))) + raise TypeError("expected (unicode) string, got " + str(type(n))) return n.replace('__', '/').replace('_', ' ') + # print (repr(kwargs)) values = {k: NamedInt(v, _readable_name(k)) for (k, v) in kwargs.items()} self.__dict__ = values self._values = sorted(list(values.values()))