CLN/BUG/TST: Fix and test multiple places using undefined names.

Pyflakes revealed that there are a number of places using undefined
names. I've tried to fix them where I can; however, those parts of the
code are also clearly untested (or tested with
`assertRaises(Exception,...)`) so they need some love.

The PyTables code had a bunch of places with undefined `TermValue`, as
did stats/misc.

Finally, I've also noted (or removed) various places where variables
were assigned but then not used.

cc @jreback