if data:
    raise ValueError('a cyclic dependency exists')

def toposort_wrap(data):
    dep_dict = {}
for d in data:
    for bef in d.get('before', ()):
        dep_dict.setdefault(bef, set()).add(d['value'])
dep_dict.setdefault(d['value'], set()).update(d.get('after', ()))
print(dep_dict)
result = list(toposort2(dep_dict))
return result
Building a list from positional relationships between the elements