David,
I finally had a chance to have a closer look at the quick patch I provided
several days ago. Attached is another patch which provides for unit tests for
utest_xml.py, as well as a more comprehensive patch for quoted.py that
resolves the issues uncovered in the tests I finally got round to writing. ;-)
diff -r 0daabcdc0340 quoted.py
--- a/quoted.py Tue Nov 10 10:39:47 2009 -0800
+++ b/quoted.py Wed Nov 11 03:09:38 2009 -0800
@@ -40,6 +40,9 @@
For values that are used as arguments to the % operator, this allows
str(value) and repr(value), if called as part of the formatting, to
produce quoted results.
+
+ Supports positional and named arguments, including attribute access,
+ provided by the string format() method introduced in Python 3.0 and 2.6.
"""
__slots__ = ['value']
@@ -55,6 +58,12 @@
def __getitem__(self, key):
return _quote_wrap(self.value[key])
+ def __getattr__(self, name):
+ return _quote_wrap(getattr(self.value, name))
+
+ def keys(self):
+ return self.value.keys()
+
def _quote_wrap(x):
"""(x) -> _xml_quote_wrapper | x
Not for outside code.
@@ -73,7 +82,7 @@
"""
__slots__ = []
- def __new__(klass, string=None, encoding=sys.getdefaultencoding(),
+ def __new__(klass, string=None, encoding=sys.getdefaultencoding(),
errors='strict'):
if string is None:
return xml('')
@@ -119,6 +128,11 @@
"""
return xml(unicode_str.join(self, (xml_quote(item) for item in items)))
+ def format(self, *args, **kwargs):
+ quoted_args = tuple(_quote_wrap(arg) for arg in args)
+ quoted_kwargs = _xml_quote_wrapper(kwargs)
+ return xml(unicode_str.format(self, *quoted_args, **quoted_kwargs))
+
join_xml = xml().join
xml.quote = staticmethod(xml_quote)
diff -r 0daabcdc0340 test/utest_xml.py
--- a/test/utest_xml.py Tue Nov 10 10:39:47 2009 -0800
+++ b/test/utest_xml.py Wed Nov 11 03:09:38 2009 -0800
@@ -72,6 +72,30 @@
assert xml("%(a)r %(b)0.2f") % dict(
a=unicode_string('<'), b=2) == "'<' 2.00"
assert type(xml("%(a)r %(b)0.2f")) is xml
+ # test support for string .format() method introduced in Python >= 2.6
+ class AttrTest(UTest): # base class not esp. relevant
+ num = 3
+ string = 'ABC'
+ tbq = '<'
+ assert xml("{0:.2f}").format(2) == '2.00'
+ assert type(xml("{0:.2f}").format(2)) is xml
+ assert xml("{a:s} {b:0.2f}").format(
+ a=unicode_string('<'), b=2) == '< 2.00'
+ assert xml("{a:s} {b:0.2f}").format(
+ a=unicode_string('<'), b=2) == '< 2.00'
+ attrtest= AttrTest()
+ assert xml("{a:s} {b:0.2f} {a_t.num:0.2f} {a_t.string} {a_t.tbq}").format(
+ a=unicode_string('<'), b=2, a_t=attrtest) == '< 2.00 3.00 ABC <'
+ if str is not unicode_string:
+ assert xml("{a:s} {b:0.2f}").format(
+ a=unicode_string(repr(u'<')), b=2) == "u'<' 2.00"
+ else:
+ assert xml("{a:s} {b:0.2f}").format(
+ a=unicode_string(repr('<')), b=2) == "'<' 2.00"
+ assert type(xml("{a:s} {b:0.2f}")) is xml
+ assert type(xml("{a:s} {b:0.2f} {a_t.num:0.2f} {a_t.string} {a_t.tbq}").format(
+ a=unicode_string('<'), b=2, a_t=attrtest)) is xml
+
def test_pickle(self):
from pickle import loads, dumps
_______________________________________________
QP mailing list
[email protected]
http://mail.mems-exchange.org/mailman/listinfo/qp