Determining a replacement dictionary from scratch

D

Dan

Hello,

I'd like to be able to take a formatted string and determine the
replacement dictionary necessary to do string interpolation with it.
For example:

Notice how it would automatically fill in default values based on
type. I figure since python does this automatically maybe there is a
clever way to solve the problem. Otherwise I will just have to parse
the string myself. Any clever solutions to this?

Thanks
-dan
 
D

Dave Benjamin

I'd like to be able to take a formatted string and determine the
replacement dictionary necessary to do string interpolation with it.
For example:


Notice how it would automatically fill in default values based on
type. I figure since python does this automatically maybe there is a
clever way to solve the problem. Otherwise I will just have to parse
the string myself. Any clever solutions to this?

Here's a solution that uses a regular expression. It only handles strings
and doesn't cache the regular expression; I'll leave this up to you. =)

import re
def createdict(fmt):
keys = re.findall('%\(([A-Za-z]+)\)s', fmt)
return dict(zip(keys, [''] * len(keys)))

The regular expression works like this:

%\( - match a percent character and opening parenthesis
([A-Za-z]+) - match a sequence of one or more alpha characters as a GROUP
\)s - match a closing parenthesis and 's' character

For more details on "re", see the documentation:
http://python.org/doc/current/lib/module-re.html

HTH,
Dave
 
C

Christophe Delord

Hello,

I'd like to be able to take a formatted string and determine the
replacement dictionary necessary to do string interpolation with it.
For example:


Notice how it would automatically fill in default values based on
type. I figure since python does this automatically maybe there is a
clever way to solve the problem. Otherwise I will just have to parse
the string myself. Any clever solutions to this?

Thanks
-dan

You can use the % operator to look for such constructions. You just
need to define the __getitem__ method of a class to store the
variable names. What about this function:

def createdict(format):
class grab_variables:
def __init__(self):
self.variables = {}
def __getitem__(self, item):
self.variables[item] = ''
g = grab_variables()
format%g
return g.variables

print createdict('his name was %(name)s and i saw him %(years)s ago.')

{'name': '', 'years': ''}


Best regards,
Christophe.
 
J

Jeff Epler

Here's a solution that uses a regular expression. It only handles strings
and doesn't cache the regular expression; I'll leave this up to you. =)

Of course, since it uses regular expressions it's also wrong...
{'foo': ''}

This version might be more correct:

pat = re.compile("%\([^)]*\)s|%%")

def createdict(fmt):
keys = [x[2:-2] for x in pat.findall(fmt) if x != '%%']
return dict(zip(keys, [''] * len(keys)))

Here's another way, one I like better:
class D:
def __init__(self): self.l = []
def __getitem__(self, item): self.l.append(item)

def createdict(fmt):
d = D()
fmt % d
return dict(zip(d.l, [''] * len(d.l)))
... I like it better because it uses the exact same logic to determine
the needed names as it will when the string is actually formatted.

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top