KeyError: '13'

Peter Otten __peter__ at web.de
Wed Jun 6 03:26:17 EDT 2012


Phanindra Ramesh Challa wrote:

> I am trying to run a python program. It is giving the KeyError: '13'. I
> have no clue regarding this error.
>  this is the python code relevant to the error:
> 
> dpi = 100
> bold = 0
> italic = 0
> mono = 0
> comment = ""
> dbname = "font.db"
> 
> for o, a in opts:
> if o == '-b': bold = 1
> if o == '-i': italic = 1
> if o == '-m': mono = 1
> if o == '-d': dpi = int (a)
> if o == '-c': comment = string.join (string.split (a), " ")
> if o == '-g': dbname = a
> 
> fontname = args[0]
> pixel_size = int (args[1])

pixel_size is now 13
 
> point_size = int (pixel_size * 72.27 / dpi + 0.5)
> 
> # Approximate average glyph width.
> avg_width = pixel_size * 17 / 24
> 
> db = anydbm.open (dbname, "r")

The above line opens a database (a Berkeley DB as the traceback reveals).

> codes = loads (db [str (pixel_size)])

str(pixel_size) converts 13 back to the string "13"

db[some_key]

looks up the record with some_key, "13" in this case. There is no record 
with the key "13" in the font.db database, and therefore the script fails 
with the aptly named KeyError. (The database interface is modeled after 
Python's dictionary, so the handling is similar)
 
> And the error is :
> python2 ../../../mk_bdf.py -c "Converted from fonts of Computer Modern
> family (C) 1979-1985 Donald E. Knuth and others." -b  'TeX Sans' 13 >
> tex09sb.bdf
> Traceback (most recent call last):
>   File "../../../mk_bdf.py", line 108, in <module>
>     codes = loads (db [str (pixel_size)])
>   File "/usr/lib/python2.7/bsddb/__init__.py", line 270, in __getitem__
>     return _DeadlockWrap(lambda: self.db[key])  # self.db[key]
>   File "/usr/lib/python2.7/bsddb/dbutils.py", line 68, in DeadlockWrap
>     return function(*_args, **_kwargs)
>   File "/usr/lib/python2.7/bsddb/__init__.py", line 270, in <lambda>
>     return _DeadlockWrap(lambda: self.db[key])  # self.db[key]
> KeyError: '13'

>  Anybody please help me in running the program.

For now you can ignore the code in the traceback which may be a bit 
intimidating to a newbie. It contains lines from the implementation of the 
database interface. Instead have a look at the keys that are in the database 
with

db = anydb.open("font.db")
for key in db: 
    print key

and then decide if you to need to change the lookup key or to add records to 
the database.




More information about the Python-list mailing list