Skip to content

fixes required for deeper pymathics modules #1031

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

Merged
merged 5 commits into from
Aug 5, 2024
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
46 changes: 46 additions & 0 deletions mathics/core/load_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,41 @@ def get_module_names(builtin_path: str, exclude_files: set) -> list:
return [f for f in py_files if f not in exclude_files]


def get_submodule_names(obj) -> list:
"""Many builtins are organized into modules which, from a documentation
standpoint, are like Mathematica Online Guide Docs.

"List Functions", "Colors", or "Distance and Similarity Measures"
are some examples Guide Documents group group various Builtin Functions,
under submodules relate to that general classification.

Here, we want to return a list of the Python modules under a "Guide Doc"
module.

As an example of a "Guide Doc" and its submodules, consider the
module named mathics.builtin.colors. It collects code and documentation pertaining
to the builtin functions that would be found in the Guide documentation for "Colors".

The `mathics.builtin.colors` module has a submodule
`mathics.builtin.colors.named_colors`.

The builtin functions defined in `named_colors` then are those found in the
"Named Colors" group of the "Colors" Guide Doc.

So in this example then, in the list the modules returned for
Python module `mathics.builtin.colors` would be the
`mathics.builtin.colors.named_colors` module which contains the
definition and docs for the "Named Colors" Mathics Bultin
Functions.
"""
modpkgs = []
if hasattr(obj, "__path__"):
for _, modname, __ in pkgutil.iter_modules(obj.__path__):
modpkgs.append(modname)
modpkgs.sort()
return modpkgs


def import_and_load_builtins():
"""
Imports Builtin modules in mathics.builtin and add rules, and definitions from that.
Expand Down Expand Up @@ -306,6 +341,17 @@ def name_is_builtin_symbol(module: ModuleType, name: str) -> Optional[type]:
return module_object


def submodules(package):
"""Generator of the submodules in a package"""
package_folder = package.__file__[: -len("__init__.py")]
for _, module_name, __ in pkgutil.iter_modules([package_folder]):
try:
module = importlib.import_module(package.__name__ + "." + module_name)
except Exception:
continue
yield module


def update_display_operators_set(builtin_instance):
"""
If builtin_instance is an operator of some kind, add that
Expand Down
14 changes: 2 additions & 12 deletions mathics/doc/gather.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import Tuple, Union

from mathics.core.builtin import Builtin, check_requires_list
from mathics.core.load_builtin import get_submodule_names, submodules
from mathics.core.util import IS_PYPY
from mathics.doc.doc_entries import DocumentationEntry
from mathics.doc.structure import DocChapter, DocGuideSection, DocSection, DocSubsection
Expand Down Expand Up @@ -138,7 +139,7 @@ def gather_sections(chapter, module, builtins_by_module, section_class=None) ->
# converting the entries into `set`s.
#
visited = set()
for symbol_instance in builtins_by_module[module.__name__]:
for symbol_instance in builtins_by_module.get(module.__name__, []):
if skip_doc(symbol_instance, module):
continue
default_contexts = ("System`", "Pymathics`")
Expand Down Expand Up @@ -361,14 +362,3 @@ def sorted_modules(modules) -> list:
if hasattr(module, "sort_order")
else module.__name__,
)


def submodules(package):
"""Generator of the submodules in a package"""
package_folder = package.__file__[: -len("__init__.py")]
for _, module_name, __ in pkgutil.iter_modules([package_folder]):
try:
module = importlib.import_module(package.__name__ + "." + module_name)
except Exception:
continue
yield module
3 changes: 2 additions & 1 deletion mathics/eval/pymathics.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ def load_pymathics_module(definitions, module_name: str):
if var is not None:
instance = var(expression=False)
if isinstance(instance, Builtin):
submodule_name = var.__module__
if not var.context:
var.context = "Pymathics`"
symbol_name = instance.get_name()
builtins_by_module[loaded_module.__name__].append(instance)
builtins_by_module.setdefault(submodule_name, []).append(instance)
newsymbols[symbol_name] = instance

for name in newsymbols:
Expand Down
Loading