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

DX: define docnblive job in tox.ini #373

Merged
merged 3 commits into from
Dec 9, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
}
],
"livePreview.defaultPreviewPath": "docs/_build/html",
"multiDiffEditor.experimental.enabled": true,
"mypy-type-checker.args": ["--config-file=${workspaceFolder}/pyproject.toml"],
"mypy-type-checker.importStrategy": "fromEnvironment",
"notebook.gotoSymbols.showAllSymbols": true,
Expand Down
36 changes: 35 additions & 1 deletion docs/_extend_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
# pyright: reportMissingImports=false
from __future__ import annotations

import hashlib
import inspect
import logging
import pickle
import sys
import textwrap
from pathlib import Path
from typing import TYPE_CHECKING, Callable

import attrs
Expand All @@ -23,7 +27,13 @@
from ampform.kinematics.lorentz import FourMomentumSymbol, _ArraySize
from ampform.sympy._array_expressions import ArrayMultiplication

if sys.version_info < (3, 8):
from importlib_metadata import version as get_package_version
else:
from importlib.metadata import version as get_package_version
if TYPE_CHECKING:
from qrules.transition import ReactionInfo

from ampform.sympy import NumPyPrintable

logging.getLogger().setLevel(logging.ERROR)
Expand Down Expand Up @@ -523,7 +533,7 @@ def extend_formulate_isobar_wigner_d() -> None:
def __get_graphviz_state_transition_example(
formalism: str, transition_number: int = 0
) -> str:
reaction = qrules.generate_transitions(
reaction = __generate_transitions_cached(
initial_state=[("J/psi(1S)", [+1])],
final_state=[("gamma", [-1]), "f(0)(980)"],
formalism=formalism,
Expand Down Expand Up @@ -704,6 +714,30 @@ def _append_to_docstring(class_type: Callable | type, appended_text: str) -> Non
class_type.__doc__ += appended_text


def __generate_transitions_cached(
initial_state: list[tuple[str, list[float | int]] | str],
final_state: list[tuple[str, list[float | int]] | str],
formalism: str,
) -> ReactionInfo:
version = get_package_version("qrules")
obj = (initial_state, final_state, formalism)
h = hashlib.sha256(pickle.dumps(obj)).hexdigest()
docs_dir = Path(__file__).parent
file_name = docs_dir / ".cache" / f"reaction-qrules-v{version}-{h}.pickle"
file_name.parent.mkdir(exist_ok=True)
if file_name.exists():
with open(file_name, "rb") as f:
return pickle.load(f) # noqa: S301
reaction = qrules.generate_transitions(
initial_state,
final_state,
formalism=formalism,
)
with open(file_name, "wb") as f:
pickle.dump(reaction, f)
return reaction


def __print_imports(printer: NumPyPrinter) -> str:
code = ""
for module, items in printer.module_imports.items():
Expand Down
34 changes: 34 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ setenv =
FORCE_COLOR = yes
PYTHONHASHSEED = 0

[testenv:docnblive]
allowlist_externals =
sphinx-autobuild
commands =
sphinx-autobuild \
--open-browser \
--re-ignore .*/.ipynb_checkpoints/.* \
--re-ignore .*/__pycache__/.* \
--re-ignore .*\.gitignore \
--re-ignore .*\.tmp \
--re-ignore docs/.*\.csv \
--re-ignore docs/.*\.gif \
--re-ignore docs/.*\.gv \
--re-ignore docs/.*\.inv \
--re-ignore docs/.*\.json \
--re-ignore docs/.*\.pickle \
--re-ignore docs/.*\.png \
--re-ignore docs/.*\.svg \
--re-ignore docs/.*\.yaml \
--re-ignore docs/.*\.yml \
--re-ignore docs/_build/.* \
--re-ignore docs/_images/.* \
--re-ignore docs/api/.* \
--watch docs \
--watch src \
docs/ docs/_build/html
description =
Set up a server to directly preview changes to the HTML pages
passenv = *
setenv =
EXECUTE_NB = yes
FORCE_COLOR = yes
PYTHONHASHSEED = 0

[testenv:docnb-force]
allowlist_externals =
sphinx-build
Expand Down
Loading