Skip to content

Commit

Permalink
updated find_instances
Browse files Browse the repository at this point in the history
  • Loading branch information
pi-r-squared committed Jul 23, 2024
1 parent 7b4af25 commit 187e095
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pirrtools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,16 @@ def get_base_package(module):
return module.__name__.split(".", maxsplit=1)[0]


def find_instances(cls, module, tracker_type=AttrDict):
def find_instances(cls, module, tracker_type=AttrDict, filter=None):
"""Find all instances of a class in a module or submodules."""
if filter is None:
filter = lambda *_: True
base_package = get_base_package(module)
tracker = tracker_type()
ModuleType = __types.ModuleType
for name, obj in vars(module).items():
if not filter(name, obj):
continue
if isinstance(obj, cls):
tracker[name] = obj
elif isinstance(obj, ModuleType) and get_base_package(obj) == base_package:
Expand Down

0 comments on commit 187e095

Please sign in to comment.