/usr/lib/python3/dist-packages/characteristic.py is in python3-characteristic 0.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | from __future__ import absolute_import, division, print_function
"""
Say 'yes' to types but 'no' to typing!
"""
__version__ = "0.1.0"
__author__ = "Hynek Schlawack"
__license__ = "MIT"
__copyright__ = "Copyright 2014 Hynek Schlawack"
def with_cmp(attrs):
"""
A class decorator that adds comparison methods based on *attrs*.
Two instances are compared as if the respective values of *attrs* were
tuples.
:param attrs: Attributes to work with.
:type attrs: `list` of native strings
"""
def attrs_to_tuple(obj):
"""
Create a tuple of all values of *obj*'s *attrs*.
"""
return tuple(getattr(obj, a) for a in attrs)
def eq(self, other):
if isinstance(other, self.__class__):
return attrs_to_tuple(self) == attrs_to_tuple(other)
else:
return NotImplemented
def ne(self, other):
result = eq(self, other)
if result is NotImplemented:
return NotImplemented
else:
return not result
def lt(self, other):
if isinstance(other, self.__class__):
return attrs_to_tuple(self) < attrs_to_tuple(other)
else:
return NotImplemented
def le(self, other):
if isinstance(other, self.__class__):
return attrs_to_tuple(self) <= attrs_to_tuple(other)
else:
return NotImplemented
def gt(self, other):
if isinstance(other, self.__class__):
return attrs_to_tuple(self) > attrs_to_tuple(other)
else:
return NotImplemented
def ge(self, other):
if isinstance(other, self.__class__):
return attrs_to_tuple(self) >= attrs_to_tuple(other)
else:
return NotImplemented
def hash_(self):
return hash(attrs_to_tuple(self))
def wrap(cl):
cl.__eq__ = eq
cl.__ne__ = ne
cl.__lt__ = lt
cl.__le__ = le
cl.__gt__ = gt
cl.__ge__ = ge
cl.__hash__ = hash_
return cl
return wrap
def with_repr(attrs):
"""
A class decorator that adds a human-friendly ``__repr__`` method that
returns a sensible representation based on *attrs*.
:param attrs: Attributes to work with.
:type attrs: Iterable of native strings.
"""
def repr_(self):
return "<{0}({1})>".format(
self.__class__.__name__,
", ".join(a + "=" + repr(getattr(self, a)) for a in attrs)
)
def wrap(cl):
cl.__repr__ = repr_
return cl
return wrap
def with_init(attrs, defaults=None):
"""
A class decorator that wraps the __init__ method of a class and sets
*attrs* first using keyword arguments.
:param attrs: Attributes to work with.
:type attrs: Iterable of native strings.
:param defaults: Default values if attributes are omitted on instantiation.
:type defaults: `dict` or `None`
"""
if defaults is None:
defaults = {}
def init(self, *args, **kw):
for a in attrs:
try:
v = kw.pop(a)
except KeyError:
try:
v = defaults[a]
except KeyError:
raise ValueError("Missing value for '{0}'.".format(a))
setattr(self, a, v)
self.__original_init__(*args, **kw)
def wrap(cl):
cl.__original_init__ = cl.__init__
cl.__init__ = init
return cl
return wrap
def attributes(attrs, defaults=None, create_init=True):
"""
A convenience class decorator that combines :func:`with_cmp`,
:func:`with_repr`, and optionally :func:`with_init` to avoid code
duplication.
:param attrs: Attributes to work with.
:type attrs: Iterable of native strings.
:param defaults: Default values if attributes are omitted on instantiation.
:type defaults: `dict` or `None`
:param create_init: Also apply :func:`with_init` (default: `True`)
:type create_init: `bool`
"""
def wrap(cl):
cl = with_cmp(attrs)(with_repr(attrs)(cl))
if create_init is True:
return with_init(attrs, defaults=defaults)(cl)
else:
return cl
return wrap
|