Mercurial > genshi > genshi-test
comparison genshi/core.py @ 857:24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
author | cmlenz |
---|---|
date | Thu, 12 Nov 2009 15:09:26 +0000 |
parents | 1e2be9fb3348 |
children | 61d37796da98 |
comparison
equal
deleted
inserted
replaced
856:1e2be9fb3348 | 857:24733a5854d9 |
---|---|
18 except NameError: | 18 except NameError: |
19 from functools import reduce | 19 from functools import reduce |
20 from itertools import chain | 20 from itertools import chain |
21 import operator | 21 import operator |
22 | 22 |
23 from genshi.util import plaintext, stripentities, striptags | 23 from genshi.util import plaintext, stripentities, striptags, stringrepr |
24 | 24 |
25 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Attrs', 'Namespace', | 25 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Attrs', 'Namespace', |
26 'QName'] | 26 'QName'] |
27 __docformat__ = 'restructuredtext en' | 27 __docformat__ = 'restructuredtext en' |
28 | 28 |
461 | 461 |
462 def __rmul__(self, num): | 462 def __rmul__(self, num): |
463 return Markup(num * unicode(self)) | 463 return Markup(num * unicode(self)) |
464 | 464 |
465 def __repr__(self): | 465 def __repr__(self): |
466 return '<%s %r>' % (self.__class__.__name__, unicode(self)) | 466 return "<%s %s>" % (self.__class__.__name__, unicode.__repr__(self)) |
467 | 467 |
468 def join(self, seq, escape_quotes=True): | 468 def join(self, seq, escape_quotes=True): |
469 """Return a `Markup` object which is the concatenation of the strings | 469 """Return a `Markup` object which is the concatenation of the strings |
470 in the given sequence, where this `Markup` object is the separator | 470 in the given sequence, where this `Markup` object is the separator |
471 between the joined elements. | 471 between the joined elements. |
598 | 598 |
599 A `Namespace` object is instantiated with the namespace URI. | 599 A `Namespace` object is instantiated with the namespace URI. |
600 | 600 |
601 >>> html = Namespace('http://www.w3.org/1999/xhtml') | 601 >>> html = Namespace('http://www.w3.org/1999/xhtml') |
602 >>> html | 602 >>> html |
603 <Namespace "http://www.w3.org/1999/xhtml"> | 603 Namespace('http://www.w3.org/1999/xhtml') |
604 >>> html.uri | 604 >>> html.uri |
605 u'http://www.w3.org/1999/xhtml' | 605 u'http://www.w3.org/1999/xhtml' |
606 | 606 |
607 The `Namespace` object can than be used to generate `QName` objects with | 607 The `Namespace` object can than be used to generate `QName` objects with |
608 that namespace: | 608 that namespace: |
609 | 609 |
610 >>> html.body | 610 >>> html.body |
611 QName(u'http://www.w3.org/1999/xhtml}body') | 611 QName('http://www.w3.org/1999/xhtml}body') |
612 >>> html.body.localname | 612 >>> html.body.localname |
613 u'body' | 613 u'body' |
614 >>> html.body.namespace | 614 >>> html.body.namespace |
615 u'http://www.w3.org/1999/xhtml' | 615 u'http://www.w3.org/1999/xhtml' |
616 | 616 |
617 The same works using item access notation, which is useful for element or | 617 The same works using item access notation, which is useful for element or |
618 attribute names that are not valid Python identifiers: | 618 attribute names that are not valid Python identifiers: |
619 | 619 |
620 >>> html['body'] | 620 >>> html['body'] |
621 QName(u'http://www.w3.org/1999/xhtml}body') | 621 QName('http://www.w3.org/1999/xhtml}body') |
622 | 622 |
623 A `Namespace` object can also be used to test whether a specific `QName` | 623 A `Namespace` object can also be used to test whether a specific `QName` |
624 belongs to that namespace using the ``in`` operator: | 624 belongs to that namespace using the ``in`` operator: |
625 | 625 |
626 >>> qname = html.body | 626 >>> qname = html.body |
663 | 663 |
664 def __hash__(self): | 664 def __hash__(self): |
665 return hash(self.uri) | 665 return hash(self.uri) |
666 | 666 |
667 def __repr__(self): | 667 def __repr__(self): |
668 return '<Namespace "%s">' % self.uri | 668 return 'Namespace(%s)' % stringrepr(self.uri) |
669 | 669 |
670 def __str__(self): | 670 def __str__(self): |
671 return self.uri.encode('utf-8') | 671 return self.uri.encode('utf-8') |
672 | 672 |
673 def __unicode__(self): | 673 def __unicode__(self): |
686 namespace URI can be obtained through the additional `namespace` attribute, | 686 namespace URI can be obtained through the additional `namespace` attribute, |
687 while the local name can be accessed through the `localname` attribute. | 687 while the local name can be accessed through the `localname` attribute. |
688 | 688 |
689 >>> qname = QName('foo') | 689 >>> qname = QName('foo') |
690 >>> qname | 690 >>> qname |
691 QName(u'foo') | 691 QName('foo') |
692 >>> qname.localname | 692 >>> qname.localname |
693 u'foo' | 693 u'foo' |
694 >>> qname.namespace | 694 >>> qname.namespace |
695 | 695 |
696 >>> qname = QName('http://www.w3.org/1999/xhtml}body') | 696 >>> qname = QName('http://www.w3.org/1999/xhtml}body') |
697 >>> qname | 697 >>> qname |
698 QName(u'http://www.w3.org/1999/xhtml}body') | 698 QName('http://www.w3.org/1999/xhtml}body') |
699 >>> qname.localname | 699 >>> qname.localname |
700 u'body' | 700 u'body' |
701 >>> qname.namespace | 701 >>> qname.namespace |
702 u'http://www.w3.org/1999/xhtml' | 702 u'http://www.w3.org/1999/xhtml' |
703 """ | 703 """ |
724 | 724 |
725 def __getnewargs__(self): | 725 def __getnewargs__(self): |
726 return (self.lstrip('{'),) | 726 return (self.lstrip('{'),) |
727 | 727 |
728 def __repr__(self): | 728 def __repr__(self): |
729 return 'QName(%s)' % unicode.__repr__(self.lstrip('{')) | 729 return 'QName(%s)' % stringrepr(self.lstrip('{')) |