Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > string.Template question

Reply
Thread Tools

string.Template question

 
 
Wells Oliver
Guest
Posts: n/a
 
      04-05-2010
Can you use dicts with string.Template?

e.g. a structure like:

game = {
'home': {'team': row['home_team_full'], 'score': row['home_score'],
'record': '0-0', 'pitcher': {
'id': home_pitcher.attrib['id'], 'name':
home_pitcher.attrib['last_name'], 'wins': home_pitcher.attrib['wins'],
'losses': home_pitcher.attrib['losses']
}, 'win': home_win}
}

Then, in the template, 'game' is passed, but I want to access like
$home.pitcher.id

This doesn't seem to work, though. Is it possible? Or must everything
in the dict passed to string.Template be one-level deep string
variables?
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      04-05-2010
Wells Oliver wrote:

> Can you use dicts with string.Template?
>
> e.g. a structure like:
>
> game = {
> 'home': {'team': row['home_team_full'], 'score': row['home_score'],
> 'record': '0-0', 'pitcher': {
> 'id': home_pitcher.attrib['id'], 'name':
> home_pitcher.attrib['last_name'], 'wins': home_pitcher.attrib['wins'],
> 'losses': home_pitcher.attrib['losses']
> }, 'win': home_win}
> }
>
> Then, in the template, 'game' is passed, but I want to access like
> $home.pitcher.id
>
> This doesn't seem to work, though. Is it possible? Or must everything
> in the dict passed to string.Template be one-level deep string
> variables?


If you're unclear about the capabilities of a piece of python it's time to
have a look at the source code

My conclusion: you can make string.Template accept dotted variables and
nested dicts, but not without subclassing and a few lines of custom code.

$ cat extended_template.py
import string

class DotDict(object):
def __init__(self, d):
self._nested = d
def __getitem__(self, key):
result = self._nested
for k in key.split("."):
result = result[k]
return result

class Template(string.Template):
idpattern = r'[_a-z][_a-z0-9.]*'

def substitute(self, *args, **kw):
assert not kw
[d] = args
return string.Template.substitute(self, DotDict(d))

if __name__ == "__main__":
game = {"home": {"pitcher": {"id": 42}}}
print Template("home/pitcher/id is $home.pitcher.id").substitute(game)

$ python extended_template.py
home/pitcher/id is 42

Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments