Skip to content

Commit

Permalink
added documentation of dispatch methods
Browse files Browse the repository at this point in the history
in see also section

Signed-off-by: Nick Papior <[email protected]>
  • Loading branch information
zerothi committed Jan 16, 2024
1 parent fa65b5e commit cce3ac3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/api/basic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ In particular `oplist` is useful when calculating averages in Brillouin zones (s
.. autosummary::
:toctree: generated/

~sisl.oplist.oplist
oplist
~sisl.utils.PropertyDict

15 changes: 15 additions & 0 deletions docs/api/core.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.. _core:

**********
Functional
**********

.. currentmodule:: sisl

sisl provides simple functionality that may be used by various
sisl objects.

.. autosummary::
:toctree: generated/

tile
1 change: 1 addition & 0 deletions docs/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ All methods and submodules are listed :ref:`here <genindex>` and
viz/index
unit_constant
utilities
core

.. toctree::
:maxdepth: 1
Expand Down
28 changes: 27 additions & 1 deletion src/sisl/_ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

from functools import singledispatch, wraps

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'wraps' is not used.
from textwrap import dedent
from typing import Callable, Optional

from sisl.messages import SislError, warn
Expand Down Expand Up @@ -46,6 +47,21 @@ def decorator(func):
return decorator


def _append_doc_dispatch(method: Callable, cls: type):
"""Append to the doc-string of the dispatch method retrieved by `method` that the `cls` class can be used"""
global _registry

# get method name
name = method.__name__

# retrieve dispatch method
method_registry = _registry[name]

# Append to doc string
doc = f"\n{cls.__name__}.{name}: when first argument is of type '{cls.__name__}'"
method_registry.__doc__ += doc


def register_sisl_dispatch(
module: Optional[str] = None, method: Optional[Callable] = None
):
Expand All @@ -65,9 +81,18 @@ def register_sisl_dispatch(
if name not in _registry:

def method_registry(*args, **kwargs):
f"""{name} dispatch method"""
"""Dispatch method"""
raise SislError

doc = dedent(
f"""\
Dispatcher for '{name}'
See also
--------
"""
)
method_registry.__doc__ = doc
method_registry.__name__ = name
method_registry.__module__ = "sisl"
_registry[name] = singledispatch(method_registry)
Expand All @@ -84,6 +109,7 @@ def method_registry(*args, **kwargs):
new_registry = set(keys)

for cls in new_registry - old_registry:
_append_doc_dispatch(method, cls)
register_sisl_function(name, cls)(method)

return method
Expand Down

0 comments on commit cce3ac3

Please sign in to comment.