Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   RE: replace %(word) in a string (http://www.velocityreviews.com/forums/t322589-re-replace-word-in-a-string.html)

Marc Boeren 09-17-2003 08:45 AM

RE: replace %(word) in a string
 

Hi,

> For example with the string: "hello, %(word1) %(word2) !" and the
> dictionary: {"word1" : "python", "word2" : "lovers"} the final string
> would be: "hello python lovers"


You mean
base_string % dictionary

>>> "hello, %(word1)s %(word2)s !" % {"word1" : "python", "word2" :

"lovers"}
'hello, python lovers !'

Notice the trailing s in %(word)s!

Cheerio, Marc.



Mirko Zeibig 09-17-2003 11:40 AM

Re: replace %(word) in a string
 
On Wed, 17 Sep 2003 at 08:45 GMT, Marc Boeren <M.Boeren@guidance.nl> wrote:
>
> Hi,
>
>> For example with the string: "hello, %(word1) %(word2) !" and the
>> dictionary: {"word1" : "python", "word2" : "lovers"} the final string
>> would be: "hello python lovers"

>
> You mean
> base_string % dictionary
>
>>>> "hello, %(word1)s %(word2)s !" % {"word1" : "python", "word2" :

> "lovers"}
> 'hello, python lovers !'
>
> Notice the trailing s in %(word)s!


Especially useful together with the locals() function, which returns a
dictionary of locally defined variables:

def foo():
a = "Hello"
b = "World"
print "%(a)s %(b)s" % locals()

foo()

Regards
Mirko

Fred Pacquier 09-17-2003 04:30 PM

Re: replace %(word) in a string
 
Mirko Zeibig <mirko-lists@zeibig.net> said :

> Especially useful together with the locals() function, which returns a
> dictionary of locally defined variables:
>
> def foo():
> a = "Hello"
> b = "World"
> print "%(a)s %(b)s" % locals()


Yes, that's a wonderful feature. Recently though, I've wondered a couple of
times : is there an easy way to substitute with both local AND global
variables ?...


--
YAFAP : http://www.multimania.com/fredp/

Peter Hansen 09-17-2003 04:41 PM

Re: replace %(word) in a string
 
Fred Pacquier wrote:
>
> Mirko Zeibig <mirko-lists@zeibig.net> said :
>
> > Especially useful together with the locals() function, which returns a
> > dictionary of locally defined variables:
> >
> > def foo():
> > a = "Hello"
> > b = "World"
> > print "%(a)s %(b)s" % locals()

>
> Yes, that's a wonderful feature. Recently though, I've wondered a couple of
> times : is there an easy way to substitute with both local AND global
> variables ?...


tempDict = locals()
tempDict.update(globals())

then just use tempDict for the dictionary...

You can also do it the other way around, of course, and you must decide
which you really want since the last-added dictionary will of course
mask any keys which are also in the first-added dictionary.

-Peter

Skip Montanaro 09-17-2003 05:43 PM

Re: replace %(word) in a string
 

Fred> Yes, that's a wonderful feature. Recently though, I've wondered a
Fred> couple of times : is there an easy way to substitute with both
Fred> local AND global variables ?...

Sure, search the list archives for something like EvalDict. Basically, you
want a class which behaves like a dict, but will search through multiple
dictionaries looking for matching keys.

Skip


Peter Hansen 09-17-2003 06:22 PM

Re: replace %(word) in a string
 
Peter Hansen wrote:
>
> Fred Pacquier wrote:
> >
> > Mirko Zeibig <mirko-lists@zeibig.net> said :
> >
> > > Especially useful together with the locals() function, which returns a
> > > dictionary of locally defined variables:
> > >
> > > def foo():
> > > a = "Hello"
> > > b = "World"
> > > print "%(a)s %(b)s" % locals()

> >
> > Yes, that's a wonderful feature. Recently though, I've wondered a couple of
> > times : is there an easy way to substitute with both local AND global
> > variables ?...

>
> tempDict = locals()
> tempDict.update(globals())


Hmmm... should that be locals().copy() ? Probably...

Max M 09-17-2003 07:01 PM

Re: replace %(word) in a string
 
Fred Pacquier wrote:

> Yes, that's a wonderful feature. Recently though, I've wondered a couple of
> times : is there an easy way to substitute with both local AND global
> variables ?...


It can often be a good idea to create an adapter/pattern for string
substitutions. Then the wrapper can "massage" the dict values before
they go into the string.

Here only a simple version is needed though (untested).

class Wrapper:

def __getitem__(self, key):
return locals().get(key, globals[key])


'Some string sub %(some_var)s' % Wrapper()


regards max M


Christophe Delord 09-17-2003 08:14 PM

Re: replace %(word) in a string
 
On 17 Sep 2003 16:30:08 GMT, Fred Pacquier wrote:

> Mirko Zeibig <mirko-lists@zeibig.net> said :
>
> > Especially useful together with the locals() function, which returns
> > a dictionary of locally defined variables:
> >
> > def foo():
> > a = "Hello"
> > b = "World"
> > print "%(a)s %(b)s" % locals()

>
> Yes, that's a wonderful feature. Recently though, I've wondered a
> couple of times : is there an easy way to substitute with both local
> AND global variables ?...
>


You can substitute local and global variables. But you can do much more.
You can write a python expression in %(...)s if the object on the right
side of % looks like a dictionnary and can evaluate its argument. For
example I often this piece of code:

class I:
def __getitem__(self, item):
frame = sys._getframe(1)
return eval(item, frame.f_globals, frame.f_locals)

As I only need one instance of I, I use:

I = I()

Then I can write thing like that:

word1 = "python"
word2 = "lovers"

print "Hello, %(word1)s %(word2)s !"%I

And even:

print "1 + 1 = %(1+1)s"%I

But you must keep in mind that the use of eval may not be safe.


Christophe.




--

(o_ Christophe Delord __o
//\ http://christophe.delord.free.fr/ _`\<,_
V_/_ mailto:christophe.delord@free.fr (_)/ (_)

William Trenker 09-17-2003 10:00 PM

Re: replace %(word) in a string
 
On Wed, 17 Sep 2003 21:01:57 +0200
Max M <maxm@mxm.dk> wrote regarding Re: replace %(word) in a string:

> class Wrapper:
>
> def __getitem__(self, key):
> return locals().get(key, globals[key])


Shouldn't that be:
return locals().get(key, globals()[key])
^^

Cheers,
Bill


Peter Otten 09-18-2003 06:38 AM

Re: replace %(word) in a string
 
Max M wrote:

> Fred Pacquier wrote:
>
>> Yes, that's a wonderful feature. Recently though, I've wondered a couple
>> of times : is there an easy way to substitute with both local AND global
>> variables ?...

>
> It can often be a good idea to create an adapter/pattern for string
> substitutions. Then the wrapper can "massage" the dict values before
> they go into the string.
>
> Here only a simple version is needed though (untested).
>
> class Wrapper:
>
> def __getitem__(self, key):
> return locals().get(key, globals[key])
>
>
> 'Some string sub %(some_var)s' % Wrapper()


I fear that the locals() you access with your wrapper class are the local
variables of the Wrapper.__getitem__() method. Also globals()[key] is
evaluated on *every* call of __getitem__() and thus fails if you try to
access a local variable that does not shade a global variable.
Assuming class Wrapper and client code are both in the same module, your
code is pretty much equivalent to

'Some string sub %(some_var)s' % globals()

Peter



All times are GMT. The time now is 01:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.