473,667 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.substi tute.

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").subst itute(obj=("hel lo",)) 'hello'

[not '("hello",)']
T("$obj").subst itute(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=("hell o",)) "('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").subst itute(obj=("hel lo",))
and T("$obj").subst itute(obj="hell o") 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 1438
Michele Simionato wrote:
from string import Template as T T("$obj").subst itute(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.substi tute.

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 UnicodeEncodeEr ror:
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
2809
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> void Quicksort(X values, int first, int last); I am getting the following compilation error, error C2784: 'void Quicksort(X ,int,int)' : could not deduce
2
1192
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. Below I have a simple example. Is there another solution I should be using instead of declaring the type outside of the template? Thanks, JT
1
1128
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
1975
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 output the formatted string. So far everything process correctly (the method is called without any issue) but my problem is that the output is htmlencoded, it means that my method (wich work as intended in other contexts) is called but the...
4
1983
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
2148
by: praveenkumar.117 | last post by:
Hi All, What is the difference between import string and from string import * Regards, Praveen
6
2340
by: bogus1one | last post by:
Hi All Given the following: // NamespaceTemplate.cpp : Defines the entry point for the console application. // #include "stdafx.h"
6
5317
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. Here's a very simple example which should illustrate what I'm doing. #include <iostream> template <class T> class TestBase {
1
1375
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 my e-mail arrive after a few hours instead of instantly. Calling Godaddy did not help as they stated all is working fine (no delays) on their end and refuse to help with custom scripts.. My emails are also hosted by godaddy like my website. ...
0
8458
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8366
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8565
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.