The KeyedDict object keeps a list of approved key values. If you try to assigned an unapproved key to a value, an exception is thrown.
When you create the object, you give it a sequence (tuple, list, etc.) of key objects and then, when you set an item with a key, it will make sure that the key is in the "approved" list. If you try to set an item with a key that isn't in your approved list, you get an TypeError exception.
Python, 28 lines
I was using the DictWriter object in the csv module and got caught when the keys in my dictionary did not match the key list I used when I created the DictWriter object. I whipped up this class to help me solve the issue and, though simple, it was very useful for that purpose and perhaps many more.
Using a Set to keep the list of keys lets you use a large number of keys with minimal (yet existent) performance impact.
Instead of throwing the error, the KeyedDict could just do nothing and keep on going. That said, you could just do an "except: pass" as well.