This will generate the entire list of keys (repeatedly) and do a list lookup; maybe on line 223 you could define the set of keys:
```python
the_keys = set(metadata[metadata.iteritems().next()[0]])
```
Then here you can do
```python
if cat not in the_keys:
    #etc
```