-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/partition-registry' into develop
Allow users to register custom partition functions with the `partition_registry.register` decorator.
- Loading branch information
Showing
12 changed files
with
381 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# registry.py | ||
|
||
from collections.abc import Mapping | ||
|
||
|
||
class Registry(Mapping): | ||
"""Generic registry for user-supplied functions. | ||
See ``pyphi.subsystem.PartitionRegistry`` and | ||
``pyphi.distance.MeasureRegistry`` for concrete usage examples. | ||
""" | ||
desc = '' | ||
|
||
def __init__(self): | ||
self.store = {} | ||
|
||
def register(self, name): | ||
"""Decorator for registering a function with PyPhi. | ||
Args: | ||
name (string): The name of the function | ||
""" | ||
def register_func(func): | ||
self.store[name] = func | ||
return func | ||
return register_func | ||
|
||
def all(self): | ||
"""Return a list of all registered functions""" | ||
return list(self) | ||
|
||
def __iter__(self): | ||
return iter(self.store) | ||
|
||
def __len__(self): | ||
return len(self.store) | ||
|
||
def __getitem__(self, name): | ||
try: | ||
return self.store[name] | ||
except KeyError: | ||
raise KeyError( | ||
'"{}" not found. Try using one of the installed {} {} or ' | ||
'register your own.'.format(name, self.desc, self.all())) |
Oops, something went wrong.