Get dictionary-keys used to format a string 
Author Message
 Get dictionary-keys used to format a string

Hence the example from the python docs:

'%(language)s has %(count)03d quote types.' % vars()

I would like to extract the keys in the formatting string to a list.
Well I can parse it myself but is there a built-in function to
extract a list/tuple of the dictionary keys, e.g.
['language','count'] in this case?

Thanks.

Ciao, Michael.



Mon, 19 Aug 2002 03:00:00 GMT  
 Get dictionary-keys used to format a string

Quote:

> Hence the example from the Python docs:

> '%(language)s has %(count)03d quote types.' % vars()

> I would like to extract the keys in the formatting string to a list.
> Well I can parse it myself but is there a built-in function to
> extract a list/tuple of the dictionary keys, e.g.
> ['language','count'] in this case?

Don't think so, sorry.

Cheers,
M.

--
very few people approach me in real life and insist on proving they are
drooling idiots.                         -- Erik Naggum, comp.lang.lisp



Mon, 19 Aug 2002 03:00:00 GMT  
 Get dictionary-keys used to format a string

Quote:

> Hence the example from the Python docs:

> '%(language)s has %(count)03d quote types.' % vars()

> I would like to extract the keys in the formatting string to a list.
> Well I can parse it myself but is there a built-in function to
> extract a list/tuple of the dictionary keys, e.g.
> ['language','count'] in this case?
>>> s = '%(language)s has %(count)03d quote types.'
>>> import re
>>> re.findall(r'%\((\w+)\)', s)

['language', 'count']

hope this helps!

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->



Mon, 19 Aug 2002 03:00:00 GMT  
 Get dictionary-keys used to format a string

Quote:


>> Hence the example from the Python docs:

>> '%(language)s has %(count)03d quote types.' % vars()

>> I would like to extract the keys in the formatting string to a list.
>> Well I can parse it myself but is there a built-in function to
>> extract a list/tuple of the dictionary keys, e.g.
>> ['language','count'] in this case?

> Don't think so, sorry.

> Cheers,
> M.

How about this approach?

#!/usr/bin/env python

class Malleable:
    def __str__(self):
        return ""
    def __int__(self):
        return 0
    def __long__(self):
        return 0L
    def __float__(self):
        return 0.0

class KeyKeeper:
    m=Malleable()
    def __init__(self):
        self.keylist=[]
    def __getitem__(self,key):
        self.keylist.append(key)
        return m

k=KeyKeeper()
fmt_string="%(language)s has %(count)03d quote types."
fmt_string%k

print k.keylist



Mon, 19 Aug 2002 03:00:00 GMT  
 Get dictionary-keys used to format a string

Quote:

>'%(language)s has %(count)03d quote types.' % vars()

>I would like to extract the keys in the formatting string to a list.
>Well I can parse it myself but is there a built-in function to
>extract a list/tuple of the dictionary keys, e.g.
>['language','count'] in this case?

Try something like this:

------------
class GrabKeys:
    def __init__(self):
        self.keys = []
    def __getitem__(self, name):
        self.keys.append(name)
        return 0 # 0 is apparently compatible with all % format characters

gk = GrabKeys()

'%(language)s has %(count)03d quote types.' % gk

print gk.keys
------------

HTH,
Johannes



Mon, 19 Aug 2002 03:00:00 GMT  
 Get dictionary-keys used to format a string

Quote:
> class KeyKeeper:
>     m=Malleable()
>     def __init__(self):
>         self.keylist=[]
>     def __getitem__(self,key):
>         self.keylist.append(key)
>         return m

NameError: m

Change this to self.m, and you're in business

--

INTERNET: Learn what you know.
Share what you don't.



Mon, 19 Aug 2002 03:00:00 GMT  
 Get dictionary-keys used to format a string

Quote:


> > Hence the example from the Python docs:

> > '%(language)s has %(count)03d quote types.' % vars()

> > I would like to extract the keys in the formatting string to a list.
> > Well I can parse it myself but is there a built-in function to
> > extract a list/tuple of the dictionary keys, e.g.
> > ['language','count'] in this case?

> >>> s = '%(language)s has %(count)03d quote types.'

> >>> import re
> >>> re.findall(r'%\((\w+)\)', s)
> ['language', 'count']

> hope this helps!

Hmm, it doesn't deal with %% correctly:

Quote:
>>> s = "%%(regexps)s %(are)s %%%(tricky)d"
>>> re.findall(r'%\((\w+)\)', s)

['regexps', 'are', 'tricky']

This is better:

Quote:
>>> re.findall(r'(?:[^%]|^)(?:%%)*%\((\w+)\)', s)

['are', 'tricky']

I'm not sure whether it really covers all special cases, though, or
whether there's a simpler regexp.

--
Bernhard Herzog   | Sketch, a drawing program for Unix



Mon, 19 Aug 2002 03:00:00 GMT  
 Get dictionary-keys used to format a string

Quote:


> >'%(language)s has %(count)03d quote types.' % vars()

> >I would like to extract the keys in the formatting string to a list.

> Try something like this:

> ------------
> class GrabKeys:
>     def __init__(self):
>         self.keys = []
>     def __getitem__(self, name):
>         self.keys.append(name)
>         return 0 # 0 is apparently compatible with all % format

> gk = GrabKeys()

> '%(language)s has %(count)03d quote types.' % gk

> print gk.keys
> ------------

I like this approach since it seems to be the most complete
solution. With using re there's always some special case which isn't
covered by the regexp in the first and second try...

Thanks to all who replied.

Ciao, Michael.



Mon, 19 Aug 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. string formatting with missing dictionary key

2. Integer and String Dictionary Keys for Fast Access

3. String formatting with two dictionaries?

4. Getting a variable format specification, and character strings

5. Extracting list of keys from 2-key dictionary

6. Dictionary to string and back to dictionary??

7. Using STRING for binary primary keys??

8. Help with using format control strings

9. Using the Plus key as a tab key.

10. Memo field, using enter key and tab key.

11. CW2001.01 Search for number key using GET(file,key) PROBLEM

12. Using ENTER-key as TAB-key

 

 
Powered by phpBB® Forum Software