def toposort(data):
<EOL>if len(data) == <NUM_LIT:0>:<EOL><INDENT>return<EOL><DEDENT>data = data.copy()<EOL>for k, v in data.items():<EOL><INDENT>v.discard(k)<EOL><DEDENT>extra_items_in_deps = _reduce(set.union, data.values()) - set(data.keys())<EOL>data.update({item:set() for item in extra_items_in_deps})<EOL>while True:<EOL><INDENT>ordered = set(item for item, dep in data.items() if len(dep) == <NUM_LIT:0>)<EOL>if not ordered:<EOL><INDENT>break<EOL><DEDENT>yield ordered<EOL>data = {item: (dep - ordered)<EOL>for item, dep in data.items()<EOL>if item not in ordered}<EOL><DEDENT>if len(data) != <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format('<STR_LIT:U+002CU+0020>'.join(repr(x) for x in data.items())))<EOL><DEDENT>
Dependencies are expressed as a dictionary whose keys are items
and whose values are a set of dependent items. Output is a list of
sets in topological order. The first set consists of items with no
dependences, each subsequent set consists of items that depend upon
items in the preceeding sets.