changeset 326:f999da894391 trunk

Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
author cmlenz
date Thu, 02 Nov 2006 11:38:10 +0000
parents 6be95de7aa99
children 3e749eaa3100
files genshi/core.py genshi/eval.py genshi/input.py genshi/path.py genshi/tests/core.py
diffstat 5 files changed, 27 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/core.py
+++ b/genshi/core.py
@@ -221,7 +221,7 @@
     
     >>> attrs = Attrs([('href', '#'), ('title', 'Foo')])
     >>> attrs
-    [(u'href', '#'), (u'title', 'Foo')]
+    Attrs([(QName(u'href'), '#'), (QName(u'title'), 'Foo')])
     
     >>> 'href' in attrs
     True
@@ -232,17 +232,17 @@
     'Foo'
     >>> attrs.set(u'title', 'Bar')
     >>> attrs
-    [(u'href', '#'), (u'title', 'Bar')]
+    Attrs([(QName(u'href'), '#'), (QName(u'title'), 'Bar')])
     >>> attrs.remove(u'title')
     >>> attrs
-    [(u'href', '#')]
+    Attrs([(QName(u'href'), '#')])
     
     New attributes added using the `set()` method are appended to the end of
     the list:
     
     >>> attrs.set(u'accesskey', 'k')
     >>> attrs
-    [(u'href', '#'), (u'accesskey', 'k')]
+    Attrs([(QName(u'href'), '#'), (QName(u'accesskey'), 'k')])
     
     An `Attrs` instance can also be initialized with keyword arguments.
     
@@ -288,6 +288,11 @@
             if attr == name:
                 return True
 
+    def __repr__(self):
+        if not self:
+            return 'Attrs()'
+        return 'Attrs(%s)' % list.__repr__(self)
+
     def get(self, name, default=None):
         """Return the value of the attribute with the specified name, or the
         value of the `default` parameter if no such attribute is found.
@@ -482,7 +487,7 @@
     that namespace:
     
     >>> html.body
-    u'{http://www.w3.org/1999/xhtml}body'
+    QName(u'http://www.w3.org/1999/xhtml}body')
     >>> html.body.localname
     u'body'
     >>> html.body.namespace
@@ -492,7 +497,7 @@
     attribute names that are not valid Python identifiers:
     
     >>> html['body']
-    u'{http://www.w3.org/1999/xhtml}body'
+    QName(u'http://www.w3.org/1999/xhtml}body')
     
     A `Namespace` object can also be used to test whether a specific `QName`
     belongs to that namespace using the `in` operator:
@@ -559,14 +564,14 @@
     
     >>> qname = QName('foo')
     >>> qname
-    u'foo'
+    QName(u'foo')
     >>> qname.localname
     u'foo'
     >>> qname.namespace
     
     >>> qname = QName('http://www.w3.org/1999/xhtml}body')
     >>> qname
-    u'{http://www.w3.org/1999/xhtml}body'
+    QName(u'http://www.w3.org/1999/xhtml}body')
     >>> qname.localname
     u'body'
     >>> qname.namespace
@@ -589,3 +594,6 @@
 
     def __getnewargs__(self):
         return (self.lstrip('{'),)
+
+    def __repr__(self):
+        return 'QName(%s)' % unicode.__repr__(self.lstrip('{'))
--- a/genshi/eval.py
+++ b/genshi/eval.py
@@ -84,7 +84,7 @@
                                  lineno=lineno)
 
     def __repr__(self):
-        return '<Expression "%s">' % self.source
+        return 'Expression(%r)' % self.source
 
     def evaluate(self, data, nocall=False):
         """Evaluate the expression against the given data dictionary.
--- a/genshi/input.py
+++ b/genshi/input.py
@@ -64,8 +64,8 @@
     >>> parser = XMLParser(StringIO('<root id="2"><child>Foo</child></root>'))
     >>> for kind, data, pos in parser:
     ...     print kind, data
-    START (u'root', [(u'id', u'2')])
-    START (u'child', [])
+    START (QName(u'root'), Attrs([(QName(u'id'), u'2')]))
+    START (QName(u'child'), Attrs())
     TEXT Foo
     END child
     END root
@@ -242,8 +242,8 @@
     >>> parser = HTMLParser(StringIO('<UL compact><LI>Foo</UL>'))
     >>> for kind, data, pos in parser:
     ...     print kind, data
-    START (u'ul', [(u'compact', u'compact')])
-    START (u'li', [])
+    START (QName(u'ul'), Attrs([(QName(u'compact'), u'compact')]))
+    START (QName(u'li'), Attrs())
     TEXT Foo
     END li
     END ul
--- a/genshi/path.py
+++ b/genshi/path.py
@@ -155,7 +155,7 @@
         >>> for event in xml:
         ...     if test(event, {}, {}):
         ...         print event
-        ('START', (u'child', [(u'id', u'2')]), (None, 1, 34))
+        ('START', (QName(u'child'), Attrs([(QName(u'id'), u'2')])), (None, 1, 34))
         """
         paths = [(p, len(p), [0], [], [0] * len(p)) for p in self.paths]
 
--- a/genshi/tests/core.py
+++ b/genshi/tests/core.py
@@ -160,6 +160,11 @@
                           unpickled.namespace)
         self.assertEquals('elem', unpickled.localname)
 
+    def test_repr(self):
+        self.assertEqual("QName(u'elem')", repr(QName('elem')))
+        self.assertEqual("QName(u'http://www.example.org/namespace}elem')",
+                         repr(QName('http://www.example.org/namespace}elem')))
+
 
 def suite():
     suite = unittest.TestSuite()
Copyright (C) 2012-2017 Edgewall Software