extracting keywords from %(keyword)s-based templates

Jeff Shannon jeff at ccvcorp.com
Fri Aug 31 19:04:28 EDT 2001


Andrew Dalke wrote:

> class KeywordTrap:
>   def __init__(self):
>      self.allwords = {}
>   def __getitem__(self, keyword):
>      self.allwords[keyword] = 1
>      return self
>   def __int__(self): return 0
>   def __str__(self): return ""
>   def __float__(self): return 0.0
>   def __oct__(self): return "0"
>   def __hex__(self): return "0"
>   def __long__(self): return 1L

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
    print trap.allwords.keys()

if __name__ == '__main__':
    test()

When I ran it, I got:

Traceback (most recent call last):
  File "c:\python20\pythonwin\pywin\framework\scriptutils.py", line 301,
in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Python20\Projects\TestScript1.py", line 24, in ?
    test()
  File "C:\Python20\Projects\TestScript1.py", line 19, in test
    "Testing ... %d %s %f %(one) %(two) %(test)" % trap
TypeError: not enough arguments for format string
>>>

So I tried getting rid of the %d %s etc, and just using the keyword
formats...

Traceback (most recent call last):
  File "c:\python20\pythonwin\pywin\framework\scriptutils.py", line 301,
in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Python20\Projects\TestScript1.py", line 23, in ?
    test()
  File "C:\Python20\Projects\TestScript1.py", line 18, in test
    "Testing ...  %(one) %(two) %(test)" % trap
ValueError: incomplete format
>>>

Bummer.  Maybe we'd have to inherit from UserDict?  I haven't messed
around with that at all...

(btw, this is on Python 2.0 under Win95)

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list