Skip to content
Browse files

Merge fancyfancystrmixin-6063-2

Author: exarkun
Reviewer: itamarst
Fixes: #6063

Add more flexibility to the formatting done by `twisted.python.util.FancyStrMixin` by
supporting arbitrary callables to format attribute values.


git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@35964 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information...
1 parent e13a4f7 commit 95e0a8e8edf4bd8aaaa5a2ce709f704983e4900f exarkun committed Oct 5, 2012
Showing with 31 additions and 11 deletions.
  1. +13 −10 twisted/python/_utilpy3.py
  2. +17 −1 twisted/python/test/test_utilpy3.py
  3. +1 −0 twisted/topfiles/6063.feature
View
23 twisted/python/_utilpy3.py
@@ -36,7 +36,6 @@ def __ne__(self, other):
return not result
-
_idFunction = id
def setIDFunction(idFunction):
@@ -130,15 +129,17 @@ class FancyStrMixin:
Mixin providing a flexible implementation of C{__str__}.
C{__str__} output will begin with the name of the class, or the contents
- of the attribute C{fancybasename} if it set.
-
- The body of C{__str__} can be controlled by overriding C{showAttributes}
- in a subclass. Set C{showAttributes} to a sequence of strings naming
- attributes, or sequences of C{(attributeName, displayName,
- formatCharacter)}. In the latter case, the attribute is looked up using
- C{attributeName}, but the output uses C{displayName} instead, and renders
- the value of the attribute using C{formatCharacter}, e.g. C{"%.3f"} might
- be used for a float.
+ of the attribute C{fancybasename} if it is set.
+
+ The body of C{__str__} can be controlled by overriding C{showAttributes} in
+ a subclass. Set C{showAttributes} to a sequence of strings naming
+ attributes, or sequences of C{(attributeName, callable)}, or sequences of
+ C{(attributeName, displayName, formatCharacter)}. In the second case, the
+ callable is passed the value of the attribute and its return value used in
+ the output of C{__str__}. In the final case, the attribute is looked up
+ using C{attributeName}, but the output uses C{displayName} instead, and
+ renders the value of the attribute using C{formatCharacter}, e.g. C{"%.3f"}
+ might be used for a float.
"""
# Override in subclasses:
showAttributes = ()
@@ -150,6 +151,8 @@ def __str__(self):
for attr in self.showAttributes:
if isinstance(attr, str):
r.append(' %s=%r' % (attr, getattr(self, attr)))
+ elif len(attr) == 2:
+ r.append((' %s=' % (attr[0],)) + attr[1](getattr(self, attr[0])))
else:
r.append((' %s=' + attr[2]) % (attr[1], getattr(self, attr[0])))
r.append('>')
View
18 twisted/python/test/test_utilpy3.py
@@ -331,12 +331,28 @@ class Foo(util.FancyStrMixin):
self.assertEqual(str(Foo()), "<Foo first=1 second='hello'>")
+ def test_formatter(self):
+ """
+ If C{showAttributes} has an item that is a 2-tuple, C{__str__} renders
+ the first item in the tuple as a key and the result of calling the
+ second item with the value of the attribute named by the first item as
+ the value.
+ """
+ class Foo(util.FancyStrMixin):
+ showAttributes = (
+ "first",
+ ("second", lambda value: repr(value[::-1])))
+ first = "hello"
+ second = "world"
+ self.assertEqual("<Foo first='hello' second='dlrow'>", str(Foo()))
+
+
def test_override(self):
"""
If C{showAttributes} has an item that is a 3-tuple, C{__str__} renders
the second item in the tuple as a key, and the contents of the
attribute named in the first item are rendered as the value. The value
- is formated using the third item in the tuple.
+ is formatted using the third item in the tuple.
"""
class Foo(util.FancyStrMixin):
showAttributes = ("first", ("second", "2nd", "%.1f"))
View
1 twisted/topfiles/6063.feature
@@ -0,0 +1 @@
+twisted.python.util.FancyStrMixin now supports arbitrary callables to format attribute values.

0 comments on commit 95e0a8e

Please sign in to comment.
Something went wrong with that request. Please try again.