Update flag sort cmp to be key-style func
Don't write directly to file buffer, no bueno on py2

Also probably just dumb.
Fix boog in flag_key found in context test module