Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registry #231

Merged
merged 6 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,14 @@ addopts = --cov=solcore --cov-report=html:htmlcov -p no:warnings -n "auto" -v
[bumpversion:file:solcore/solcore_config.txt]

[bumpversion:file:docs/source/conf.py]

[flake8]
max-line-length = 88
exclude = .venv/
extend-ignore =
E203,

[isort]
line_length = 88
multi_line_output = 3
include_trailing_comma = true
50 changes: 50 additions & 0 deletions solcore/registries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Registries are dictionaries that put together Solcore's functionality that share the
same purpose, eg. calculate solar cell properties, solve the electrical properties of
junctions or solve their optical properties. They can also perform validation and be
used to extend Solcore's functionality without the need of modifying the source code.

The main advantage of registries is that they allow you to define your own functionality
outside of the solcore code base, simplifying the process of trying new things:

>>> from solcore import registries
>>> @registries.register_action("pre-process")
... def pre_process_cell(*args, **kwargs):
... pass
>>> "pre-process" in registries.ACTIONS_REGISTRY
True

After this, `pre-process` will be one of the `task` that you can run when executing the
`solar_cell_solver` funciton.

You can also overwrite existing functionality in case you want to implement your own
alternative to an existing approach:

>>> from solcore import registries
>>> @registries.register_action("optics", overwrite=True)
... def custom_solve_optics(*args, **kwargs):
... pass
>>> registries.ACTIONS_REGISTRY["optics"] == custom_solve_optics
True

"""
from typing import Callable, Dict

from .solar_cell import SolarCell
from .state import State

ACTIONS_SIGNATURE = Callable[[SolarCell, State], None]
ACTIONS_REGISTRY: Dict[str, ACTIONS_SIGNATURE] = {}


def register_action(name: str, overwrite: bool = False) -> Callable:
if name in ACTIONS_REGISTRY and not overwrite:
raise ValueError(
f"Action '{name}' already exist in the registry."
"Give it another name or set `overwrite = True`."
)

def wrap(func: ACTIONS_SIGNATURE) -> ACTIONS_SIGNATURE:
ACTIONS_REGISTRY[name] = func
return func

return wrap