Merge pull request #37 from Lekensteyn/py3-compat
Py3 compat: replace use of "unicode" (pwr/Solaar#32) The `u''` syntax fails in Python 3.2; will fix is_string after the merge.
This commit is contained in:
commit
db4c088ce9
|
@ -6,6 +6,9 @@ from __future__ import absolute_import, division, print_function, unicode_litera
|
||||||
|
|
||||||
from binascii import hexlify as _hexlify
|
from binascii import hexlify as _hexlify
|
||||||
from struct import pack as _pack
|
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))
|
||||||
|
|
||||||
|
|
||||||
class NamedInt(int):
|
class NamedInt(int):
|
||||||
|
@ -15,7 +18,7 @@ class NamedInt(int):
|
||||||
(case-insensitive)."""
|
(case-insensitive)."""
|
||||||
|
|
||||||
def __new__(cls, value, name):
|
def __new__(cls, value, name):
|
||||||
assert isinstance(name, str) or isinstance(name, unicode)
|
assert is_string(name)
|
||||||
obj = int.__new__(cls, value)
|
obj = int.__new__(cls, value)
|
||||||
obj.name = str(name)
|
obj.name = str(name)
|
||||||
return obj
|
return obj
|
||||||
|
@ -30,8 +33,12 @@ class NamedInt(int):
|
||||||
return int(self) == int(other) and self.name == other.name
|
return int(self) == int(other) and self.name == other.name
|
||||||
if isinstance(other, int):
|
if isinstance(other, int):
|
||||||
return int(self) == int(other)
|
return int(self) == int(other)
|
||||||
if isinstance(other, str) or isinstance(other, unicode):
|
if is_string(other):
|
||||||
return self.name.lower() == other.lower()
|
return self.name.lower() == other.lower()
|
||||||
|
# this should catch comparisons with bytes in Py3
|
||||||
|
if other is not None:
|
||||||
|
raise TypeError("Unsupported type " + str(type(other)))
|
||||||
|
|
||||||
|
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
return not self.__eq__(other)
|
return not self.__eq__(other)
|
||||||
|
@ -64,8 +71,8 @@ class NamedInts(object):
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
def _readable_name(n):
|
def _readable_name(n):
|
||||||
if not isinstance(n, str) and not isinstance(n, unicode):
|
if not is_string(n):
|
||||||
raise TypeError("expected string, got " + type(n))
|
raise TypeError("expected string, got " + str(type(n)))
|
||||||
return n.replace('__', '/').replace('_', ' ')
|
return n.replace('__', '/').replace('_', ' ')
|
||||||
|
|
||||||
values = {k: NamedInt(v, _readable_name(k)) for (k, v) in kwargs.items()}
|
values = {k: NamedInt(v, _readable_name(k)) for (k, v) in kwargs.items()}
|
||||||
|
@ -100,7 +107,7 @@ class NamedInts(object):
|
||||||
self._values = sorted(self._values + [value])
|
self._values = sorted(self._values + [value])
|
||||||
return value
|
return value
|
||||||
|
|
||||||
elif isinstance(index, str) or isinstance(index, unicode):
|
elif is_string(index):
|
||||||
if index in self.__dict__:
|
if index in self.__dict__:
|
||||||
return self.__dict__[index]
|
return self.__dict__[index]
|
||||||
|
|
||||||
|
@ -135,7 +142,7 @@ class NamedInts(object):
|
||||||
if isinstance(name, NamedInt):
|
if isinstance(name, NamedInt):
|
||||||
assert int(index) == int(name), repr(index) + ' ' + repr(name)
|
assert int(index) == int(name), repr(index) + ' ' + repr(name)
|
||||||
value = name
|
value = name
|
||||||
elif isinstance(name, str) or isinstance(name, unicode):
|
elif is_string(name):
|
||||||
value = NamedInt(index, name)
|
value = NamedInt(index, name)
|
||||||
else:
|
else:
|
||||||
raise TypeError('name must be a string')
|
raise TypeError('name must be a string')
|
||||||
|
@ -152,7 +159,7 @@ class NamedInts(object):
|
||||||
def __contains__(self, value):
|
def __contains__(self, value):
|
||||||
if isinstance(value, int):
|
if isinstance(value, int):
|
||||||
return value in self._indexed
|
return value in self._indexed
|
||||||
if isinstance(value, str) or isinstance(value, unicode):
|
elif is_string(value):
|
||||||
return value in self.__dict__
|
return value in self.__dict__
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
|
|
Loading…
Reference in New Issue