473,545 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string.Template .delimiter cannot be overriden?

I've been trying to coax this class to use something other than the
default '$' but it seems setting it to something else has no discernible
effect. Is it necessary to inherit from the class to do this?

I've only been using Python for a couple of weeks so I'm not sure what
the best approach is here.

Thanks in advance.
Jun 27 '08 #1
4 3406
On Jun 16, 9:53*pm, kretik <kre...@yahoo.c omwrote:
I've been trying to coax this class to use something other than the
default '$' but it seems setting it to something else has no discernible
effect. Is it necessary to inherit from the class to do this?
Yes, subclassing is the intended way to produce variants of Template
with a different delimiter.
>>import string
class PercentTemplate (string.Templat e):
delimiter = "%"

>>s = PercentTemplate ('Move %piece to %position')
s.substitute( piece='pawn', position='K4')
'Move pawn to K4'
Raymond

Jun 27 '08 #2
Raymond Hettinger wrote:
On Jun 16, 9:53 pm, kretik <kre...@yahoo.c omwrote:
>I've been trying to coax this class to use something other than the
default '$' but it seems setting it to something else has no discernible
effect. Is it necessary to inherit from the class to do this?

Yes, subclassing is the intended way to produce variants of Template
with a different delimiter.
OK, that makes no sense but I guess it's some sort of pythonistic thing
or something =)

Thanks a lot for the response.
Jun 27 '08 #3
En Tue, 17 Jun 2008 02:20:23 -0300, Raymond Hettinger <py****@rcn.com >
escribió:
On Jun 16, 9:53*pm, kretik <kre...@yahoo.c omwrote:
>I've been trying to coax this class to use something other than the
default '$' but it seems setting it to something else has no discernible
effect. Is it necessary to inherit from the class to do this?

Yes, subclassing is the intended way to produce variants of Template
with a different delimiter.
Just out of curiosity, why was it done that way?
I'd say the "obvious" way to change the default delimiter would be to set
an instance attribute - so I guess this was a deliberate decision, but I
can't figure out why it is better this way...

--
Gabriel Genellina

Jun 27 '08 #4
[kretik]
>I've been trying to coax this class to use something other than the
default '$' but it seems setting it to something else has no discernible
effect. Is it necessary to inherit from the class to do this?
[raymond]
Yes, subclassing is the intended way to produce variants of Template
with a different delimiter.
[gabriel]
Just out of curiosity, why was it done that way?
I'd say the "obvious" way to change the default delimiter would be to set *
an instance attribute - so I guess this was a deliberate decision, but I *
can't figure out why it is better this way...
IIRC, Barry and Tim came-up with the metaclass because the delimiter
and
pattern decisions are less granular than the creating of individual
templates.
Typically, the former decision is made once per application, but there
may
be thousands of template instances.

An earlier version of the API looked like this:

s = Template('Move %piece to %position', delimiter='%')
t = Template('%sour ce takes %target', delimiter='%')

The repeated need to set the delimiter for every template was slow and
error-prone.

The newer API is:

class PercentTemplate (string.Templat e):
delimiter = '%'
s = PercentTemplate ('Move %piece to %position')
t = PercentTemplate ('%source takes %target')

So, basically it was just a factoring decision.

Instead of using a metaclass, one other possible choice would have
been to use a factory function:

PercentTemplate = template_maker( delimiter='%')
s = PercentTemplate ('Move %piece to %position')
t = PercentTemplate ('%source takes %target')

Raymond
Jun 27 '08 #5

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

Similar topics

5
1788
by: Christopher Benson-Manica | last post by:
The function in question follows: vector<string>& tokenize( const string& s, vector<string>& v, char delimiter=',' ) { int delim_idx, begin_idx=0, len=s.length(); for( delim_idx=s.find_first_of(delimiter,begin_idx) ;
3
2461
by: Old Wolf | last post by:
Hi all. G++ fails to compile the following: #include <string> int main() { std::string foo("abc=123"); std::string::const_iterator delimiter = std::find(foo.begin(), foo.end(), '=');
6
2184
by: Webgour | last post by:
How to go from string "abc" to string "a|b|c"?
35
2540
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C# newsgroup on 25 September. The regular expressions where in that newsgroup too involved. I told yesterday night, to Jay that I would test all 4...
7
8563
by: Felix85 | last post by:
I am trying to make a command interpreter for a mud that i am working on the problem i am having right now is that i cannot convert the string into a char array. This is the error I am getting now: In file included from src/mud.cpp:3: src/command.h: In static member function `static void command::getCommand(std::string)':...
10
6019
by: Bilal | last post by:
Hello, I'm trying to perform some string manipulations in my stylesheet and have gotten stuck on the issue below so hopefully can elicit some useful hints. Namely, the problem is that I need to convert an unqualified Xpath to a fully qualified Xpath in an identity transform, i.e. /AAA/BBB/CCC/@DDD
1
3042
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create a Visual Basic 2005 solution that manipulates strings. It will parse a string containing a list of items within a text box and put the individual...
7
1657
by: Samuel | last post by:
Hi, How would you implement a simple parser for the following string: --- In this string $variable1 is substituted, while \$variable2 is not. --- I know how to write a parser, but I am looking for an elegant (and lazy) way. Any idea?
8
284
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
0
7490
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...
0
7425
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...
0
7780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6009
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...
1
5351
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...
0
5069
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...
0
3479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.