Use proper Attributes in all dunders

This commit is contained in:
Hynek Schlawack 2015-01-28 11:34:55 +01:00
parent 104e663551
commit 14c5485cf5
2 changed files with 8 additions and 6 deletions

View File

@ -11,12 +11,12 @@ def _attrs_to_tuple(obj, attrs):
"""
Create a tuple of all values of *obj*'s *attrs*.
"""
return tuple(getattr(obj, a) for a in attrs)
return tuple(getattr(obj, a.name) for a in attrs)
def _add_hash(cl, attrs=None):
if attrs is None:
attrs = [a.name for a in cl.__attrs_attrs__]
attrs = cl.__attrs_attrs__
def hash_(self):
"""
@ -30,7 +30,7 @@ def _add_hash(cl, attrs=None):
def _add_cmp(cl, attrs=None):
if attrs is None:
attrs = [a.name for a in cl.__attrs_attrs__]
attrs = cl.__attrs_attrs__
def attrs_to_tuple(obj):
"""
@ -105,7 +105,7 @@ def _add_cmp(cl, attrs=None):
def _add_repr(cl, attrs=None):
if attrs is None:
attrs = [a.name for a in cl.__attrs_attrs__]
attrs = cl.__attrs_attrs__
def repr_(self):
"""
@ -113,7 +113,8 @@ def _add_repr(cl, attrs=None):
"""
return "{0}({1})".format(
self.__class__.__name__,
", ".join(a + "=" + repr(getattr(self, a)) for a in attrs)
", ".join(a.name + "=" + repr(getattr(self, a.name))
for a in attrs)
)
cl.__repr__ = repr_
return cl

View File

@ -27,7 +27,8 @@ class Attribute(object):
)
_a = ["name", "default_value", "default_factory"]
_a = [Attribute(name=name, default_value=NOTHING, default_factory=NOTHING)
for name in ("name", "default_value", "default_factory",)]
Attribute = _add_cmp(_add_repr(Attribute, attrs=_a), attrs=_a)