def toposort(data):
<EOL>if len(data) == <NUM_LIT:0>:<EOL><INDENT>return<EOL><DEDENT>data = data.copy()<EOL>for k, v in list(data.items()):<EOL><INDENT>v.discard(k)<EOL><DEDENT>extra_items_in_deps = functools.reduce(<EOL>set.union, list(data.values())<EOL>) - set(data.keys())<EOL>data.update(dict((item, set()) for item in extra_items_in_deps))<EOL>while True:<EOL><INDENT>ordered = set(item for item, dep in list(data.items()) if len(dep) == <NUM_LIT:0>)<EOL>if not ordered:<EOL><INDENT>break<EOL><DEDENT>yield ordered<EOL>data = dict(<EOL>(item, (dep - ordered))<EOL>for item, dep in list(data.items())<EOL>if item not in ordered<EOL>)<EOL><DEDENT>if len(data) != <NUM_LIT:0>:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'<EOL>.format('<STR_LIT:U+002CU+0020>'.join(repr(x) for x in list(data.items())))<EOL>)<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.