extracting keywords from %(keyword)s-based templates
Andrew Dalke
dalke at dalkescientific.com
Fri Aug 31 19:17:58 EDT 2001
Jeff Shannon wrote:
>Well, I *did* test this... I threw the above class into a module with:
>
>def test():
> trap = KeywordTrap()
> "Testing ... %d %s %f %(one) %(two) %(test)" % trap
Technically, the subject is
'extracting keywords from %(keyword)s-based templates'
:)
The '%d' syntax only works with a single object or a tuple, and
only tuples work if you have more than one positional % term.
The '%()s' syntax works different.
>So I tried getting rid of the %d %s etc, and just using the keyword
>formats...
> "Testing ... %(one) %(two) %(test)" % trap
>ValueError: incomplete format
Should be
"Testing ... %(one)s %(two)d %(test)f" % trap
^^^ ^^^ ^^^
>Bummer. Maybe we'd have to inherit from UserDict? I haven't messed
>around with that at all...
Nope. The % operator used by a string only needs the __getitem__
when given a '%()s' syntax.
For real fun, explain why this should work:
>>> class Spam:
... def __getitem__(self, name):
... return len(name)
... def __int__(self):
... return 987
...
>>> spam = Spam()
>>> "%d %(four)s %(yummy)s" % spam
'987 4 5'
>>>
Now I'm wondering in 2.2 what happens if I derive from ()...
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list