Skip to content

Commit

Permalink
utilities: add toposort_key
Browse files Browse the repository at this point in the history
function that produces a python sorting key given a dependency
dictionary
  • Loading branch information
kmantel committed Dec 14, 2023
1 parent 3d44440 commit e10cc07
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions psyneulink/core/globals/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@
import collections
import copy
import inspect
import itertools
import logging
import psyneulink
import re
import time
import warnings
import weakref
import toposort
import types
import typing
from beartype import beartype
Expand Down Expand Up @@ -2060,3 +2062,30 @@ def get_function_sig_default_value(
return sig.parameters[parameter].default
except KeyError:
return inspect._empty


def toposort_key(
dependency_dict: typing.Dict[typing.Hashable, typing.Iterable[typing.Any]]
) -> typing.Callable[[typing.Any], int]:
"""
Creates a key function for python sorting that causes all items in
**dependency_dict** to be sorted after their dependencies
Args:
dependency_dict (typing.Dict[typing.Hashable, typing.Iterable[typing.Any]]):
a dictionary where values are the dependencies of keys
Returns:
typing.Callable[[typing.Any], int]: a key function for python
sorting
"""
topo_ordering = list(toposort.toposort(dependency_dict))
topo_ordering = list(itertools.chain.from_iterable(topo_ordering))

def _generated_toposort_key(obj):
try:
return topo_ordering.index(obj)
except ValueError:
return -1

return _generated_toposort_key

0 comments on commit e10cc07

Please sign in to comment.