473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

issue with string.Template

This is somewhat in between a bug report and a feature request.
I was using the new string.Template class and I have run into a
few issues that I traced back to the usage of the idiom

'%s' % val

in the 'convert' helper function in Template.substitute.

I do not understand why '%s' % val was used instead of just
str(val).

'%s' % val in unfortunate since it results in the following
surprising (for me) behavior:
from string import Template as T T("$obj").substitute(obj=("hello",)) 'hello'

[not '("hello",)']
T("$obj").substitute(obj=()) TypeError: not enough arguments for format string

[not '()']

Is this intended behavior? It is surprising since it is
different from what one would expect from old-fashioned string
interpolation
"%(obj)s" % dict(obj=("hello",)) "('hello',)" "%(obj)s" % dict(obj=())

'()'

which is the behaviour I find most useful. Also, I do not like
that different expressions such as
T("$obj").substitute(obj=("hello",))
and T("$obj").substitute(obj="hello") give the same output
(this potentially hides type bugs).

So, take this as a bug report if the behavior is not intended and
as a feature request if the current behaviour is the intended
one ;)

Michele Simionato

P.S. at the end, the problem is that string interpolation with
positional arguments is somewhat of a hack, but fixing this will
have to wait until Python 3000 ...

Sep 11 '05 #1
2 1427
Michele Simionato wrote:
from string import Template as T T("$obj").substitute(obj=())
TypeError: not enough arguments for format string

So, take this as a bug report if the behavior is not intended and
as a feature request if the current behaviour is the intended
one ;)


I vote for bug report. The need to habitually wrap any tuple arguments into
another 1-tuple is clearly at odds with the goal to simplify string
interpolation.

Peter
Sep 11 '05 #2
Michele Simionato wrote:
This is somewhat in between a bug report and a feature request.
I was using the new string.Template class and I have run into a
few issues that I traced back to the usage of the idiom

'%s' % val

in the 'convert' helper function in Template.substitute.

I do not understand why '%s' % val was used instead of just
str(val).
The reason is written in the code comments:

"""
# We use this idiom instead of str() because the latter will
# fail if val is a Unicode containing non-ASCII characters.
"""
P.S. at the end, the problem is that string interpolation with
positional arguments is somewhat of a hack, but fixing this will
have to wait until Python 3000 ...
The plan for Py3.0 is to have a formatting function that doesn't have
the same tuple vs scalar issue.
So, take this as a bug report if the behavior is not intended and
as a feature request if the current behaviour is the intended
one ;)


Feel free to post a SF report. If Barry wants to alter the behavior,
it is easy enough to do:

try:
return str(s)
except UnicodeEncodeError:
return unicode(s)
Raymond Hettinger

Sep 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Vespasian | last post by:
I create an templated Array class. My constructor declaration is as follows, template<class X> Array<X>::Array(int s); // Constructor I also have templated sorting function, template<class X>...
2
by: JT | last post by:
I have a compiler error when using a non-dependent type declared in a template, if I use the type in function definitions. I think it is a parsing issues related to confusion with a typename. ...
1
by: ChasW | last post by:
Consider the following 2 templates. template <class T> T fromString (const std::string& s) { std::istringstream i (s); T x; i >> x; return x; }
4
by: Gauthier | last post by:
Hi, I've a simple issue with the use of extension objects. I'm trying to call a text formating method from an object that I add to my arguments collection, this method take an input string and...
4
by: sebastien NO Maraux SPAM | last post by:
I am using Ghost Lib 4.0, which is SDK for Phantom haptic device. this lib does not compile under .net, seemingly because of a union of this type : union A { union A* aList; char b; };
3
by: praveenkumar.117 | last post by:
Hi All, What is the difference between import string and from string import * Regards, Praveen
6
by: bogus1one | last post by:
Hi All Given the following: // NamespaceTemplate.cpp : Defines the entry point for the console application. // #include "stdafx.h"
6
by: Lawrence Spector | last post by:
I ran into a problem using g++. Visual Studio 2005 never complained about this, but with g++ I ran into this error. I can't figure out if I've done something wrong or if this is a compiler bug. ...
1
by: worldofrugs | last post by:
I'm hosted on a shared server with Godaddy.com... I have several forms on my website that all use the same ASP file to send out my forms.. It worked fine for a long time, but now the forms send to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.