def flatten_dict(source, ancestors=None):
  """Flattens a dictionary into (key, value) tuples. Where key is a tuple of ancestor keys.

  :source: the root dictionary
  :ancestors: the tuple of ancestors for every key in :source:"""
  if not ancestors:
    ancestors = ()
  for key in source:
    if isinstance(source[key], dict):
      yield from flatten_dict(source[key], ancestors + (key, ))
    else:
      yield (ancestors + (key, ), source[key])