[issue13173] Default values for string.Template

2016-05-06 Thread Serhiy Storchaka

Changes by Serhiy Storchaka :


--
resolution:  -> wont fix
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2013-11-10 Thread Barry A. Warsaw

Barry A. Warsaw added the comment:

I'm looking at this issue again with an eye toward Python 3.4.

Raymond describes what I think is a reasonable way to use defaults:

 x = Template('$foo $bar')
 defaults = dict(foo='one', bar='two')
 x.substitute(defaults)
'one two'
 x.substitute(defaults, bar='three')
'one three'
 x.substitute(defaults, foo='nine', bar='three')
'nine three'

(The implementation actually uses ChainMap.)

Now, to address Bfontaine's complaint about passing around tuples, observe that 
Template instances are Just Instances, so you can always do this:

 x = Template('$foo $bar')
 x.defaults = defaults
 x.substitute(x.defaults, foo='nine', bar='three')
'nine three'

IOW, just stash your defaults on the instance and pass the instance around.  
Does the Template class actually need more built-in support for defaults?  I'm 
inclined to close this as Won't Fix.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2013-01-07 Thread Serhiy Storchaka

Changes by Serhiy Storchaka storch...@gmail.com:


--
versions: +Python 3.4 -Python 3.3

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2012-06-25 Thread Serhiy Storchaka

Serhiy Storchaka storch...@gmail.com added the comment:

If the Template defaults should be attached directly to the template instance, 
then they should be attached directly to the string instance, for classic and 
modern string formatting. And this obviously is absurd.

I agree with Éric, to mention ChainMap class in the string formatting 
documentation will be enough.

--
nosy: +storchaka

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2012-06-24 Thread Éric Araujo

Éric Araujo mer...@netwok.org added the comment:

It looks like a doc update to mention the excellent ChainMap class would be a 
good thing.

Bfontaine, are you happy with using a Template subclass or changing your code 
slightly to have defaults outside of the Template objects, or do you still 
think the class should be changed?  We could run this by the python-ideas 
mailing list.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2011-11-07 Thread Barry A. Warsaw

Barry A. Warsaw ba...@python.org added the comment:

When I need defaults, I make them part of the mapping that gets passed into 
.substitute() and .safe_substitute().  It doesn't feel to me like it's 
necessary to attach them to the Template instance.  Also, couldn't you just 
subclass string.Template if you wanted defaults?  OTOH, since it can be done in 
a backward compatible way, I guess I'm -0 on the change.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2011-10-29 Thread Raymond Hettinger

Raymond Hettinger raymond.hettin...@gmail.com added the comment:

Barry, any thoughts?

--
assignee: rhettinger - barry
nosy: +barry

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2011-10-19 Thread Bfontaine

Bfontaine bati...@yahoo.fr added the comment:

When you are using a lot of string templates like I am doing, I think it's 
better if the defaults is attached directly to the template instance.

This:
[Template0, Template1, Template2, Template3, ...]
is easier to use than:
[(Template0, defaults0), (Template1, defaults1), ...]

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2011-10-18 Thread Bfontaine

Bfontaine bati...@yahoo.fr added the comment:

diff updated

--
Added file: 
http://bugs.python.org/file23454/string_template_default_values2.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2011-10-18 Thread Bfontaine

Bfontaine bati...@yahoo.fr added the comment:

A light version of diff added (rewrapped content is replaced by 
[rewrapping])

--
Added file: 
http://bugs.python.org/file23455/string_template_default_values2-light.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2011-10-18 Thread Raymond Hettinger

Raymond Hettinger raymond.hettin...@gmail.com added the comment:

This looks like a reasonable use case.  That being said, I question whether the 
defaults should be attached directly to the template instance or whether they 
should be part of the substitution method.

FWIW, there already have a couple of other ways to do it:

 from string import Template
 s = Template(${user} made me a ${flavor} cake.)
 default = {user:Dennis}
 s.substitute(default, flavor='vanilla')
'Dennis made me a vanilla cake.'
 s.substitute(default, user='Ken', flavor='vanilla')
'Ken made me a vanilla cake.'
 

 from collections import ChainMap
 s.substitute(ChainMap(dict(flavor='vanilla'), default))
'Dennis made me a vanilla cake.'

--
assignee:  - rhettinger
nosy: +rhettinger

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2011-10-17 Thread Éric Araujo

Éric Araujo mer...@netwok.org added the comment:

Thanks for the patch.  It mostly looks good; a detailed review follow.
 
+   The constructor takes two arguments, the first is the template string and
+   the second (optional) is a dictionary object which specify which values
+   should be used as default values, if no provided.
Just say “a dictionary”, or even “a mapping”.  There’s also a grammar typo and 
a bit of redundancy, so I propose: “is a mapping which gives default values”.  
What do you think about adding a small example in the examples section later in 
the file?  It would probably be clearer than English.

   Like :meth:`substitute`, except that if placeholders are missing from
-  *mapping* and *kwds*, instead of raising a :exc:`KeyError` exception, the
-  original placeholder will appear in the resulting string intact.  Also,
-  unlike with :meth:`substitute`, any other appearances of the ``$`` will
-  simply return ``$`` instead of raising :exc:`ValueError`.
+  *mapping*, *kwds* and *default*, instead of raising a :exc:`KeyError`
default is not an argument, so *foo* markup is misleading.  Either use “the 
default values given to the constructor” or just “self.default”.

+  exception, the original placeholder will appear in the resulting string
+  intact.  Also, unlike with :meth:`substitute`, any other appearances of
+  the ``$`` will simply return ``$`` instead of raising :exc:`ValueError`.
If you don’t mind, I prefer if you have a few very short or too long lines if 
that helps reducing diff noise (i.e. lines that appear in the diff but are not 
changed, only rewrapped).

+   .. attribute:: default
+
+  This is the optional dictionary object passed to the constructor's
+  *template* argument.
I’m not a native English speaker, but “passed to” seems wrong here (and in the 
other attribute’s doc).  I’d say “passed as the *default* argument”.
 
-def __init__(self, template):
+def __init__(self, template, default={}):
Binding a mutable object to a local name at compile time is not good: All 
instances created without *default* argument will share the same dict, so 
editions to onetemplate.default will change anothertemplate.default too.  The 
common idiom is to use None as default value and add this:

self.default = default if default is not None else {}

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2011-10-17 Thread Éric Araujo

Éric Araujo mer...@netwok.org added the comment:

Adding Georg, maintainer of the string module, to the nosy list.  If he 
approves the idea, you can go ahead and complete your patch.

--
nosy: +georg.brandl

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2011-10-14 Thread Éric Araujo

Éric Araujo mer...@netwok.org added the comment:

Thanks for your interest in Python.  Can you repost your code as a diff?  (See 
the devguide for more info)

--
nosy: +eric.araujo
versions: +Python 3.3 -Python 3.2

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2011-10-14 Thread Bfontaine

Bfontaine bati...@yahoo.fr added the comment:

Here is my code as a diff

--
keywords: +patch
Added file: 
http://bugs.python.org/file23408/string_template_default_values.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13173] Default values for string.Template

2011-10-13 Thread Bfontaine

New submission from Bfontaine bati...@yahoo.fr:

This patch allows you to define default values for a string.Template, which is 
useful when you need to use a lot some values, but sometimes other values.

for example:

 from string import Template
 s = Template(${user} made me a ${flavor} cake., default={user:Dennis})
 s.substitute(flavor=vanilla)
'Dennis made me a vanilla cake.'
 s.substitute(user=Ken, flavor=chocolate)
'Ken made me chocolate cake.'

--
components: Library (Lib)
files: string_template_default_values.tar
messages: 145485
nosy: nitupho
priority: normal
severity: normal
status: open
title: Default values for string.Template
type: feature request
versions: Python 3.2
Added file: http://bugs.python.org/file23398/string_template_default_values.tar

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13173
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com