Nick Coghlan wrote: > I found the following to be an interesting experiment: > > ------------- > from string import Template > > def format(*args, **kwds): > fmt = args[0] > kwds.update(("p%s" % idx, arg) for idx, arg in enumerate(args)) > return Template(fmt).substitute(**kwds)
I forgot to add the following concept: ------------- def printf(*args, **kwds): to = kwds.pop("to", sys.stdout) to.write(format(*args, **kwds)) Py> printf("$p1: $p2\n", 1, 2) 1: 2 Py> printf("$p1: $p2\n", 1, 2, to=sys.stderr) 1: 2 Py> printf("$p1: $p2$to\n", 1, 2, to=sys.stderr) Traceback (most recent call last): File "<stdin>", line 1, in ? File "<stdin>", line 3, in printf File "<stdin>", line 4, in format File "C:\Python24\lib\string.py", line 172, in substitute return self.pattern.sub(convert, self.template) File "C:\Python24\lib\string.py", line 162, in convert val = mapping[named] KeyError: 'to' ------------- If you're dealing with an existing template that uses the 'to' keyword, then it is possible to fall back to using: ------------- def printraw(*args, **kwds): to = kwds.pop("to", sys.stdout) for arg in args: to.write(arg) Py> printraw(format("$p1: $p2$to\n", 1, 2, to="There"), to=sys.stderr) 1: 2There ------------- Cheers, Nick. -- Nick Coghlan | [EMAIL PROTECTED] | Brisbane, Australia --------------------------------------------------------------- http://boredomandlaziness.blogspot.com _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com