Skip to content

Commit

Permalink
Small changes mentioned in PR
Browse files Browse the repository at this point in the history
* module -> mathics3_builtins_modules
* Bump version to 7.0.0dev0
* More type annotations
* Add in comments debug code to time loading builtin functions

I see 2.2secs out of 7 secs to run: mathics -e "1+1"
  • Loading branch information
rocky committed Jul 15, 2023
1 parent 72488a4 commit 09e43b6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 30 deletions.
9 changes: 5 additions & 4 deletions mathics/core/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from mathics.core.convert.expression import to_mathics_list
from mathics.core.element import fully_qualified_symbol_name
from mathics.core.expression import Expression
from mathics.core.load_builtin import definition_contribute, modules
from mathics.core.load_builtin import definition_contribute, mathics3_builtins_modules
from mathics.core.symbols import Atom, Symbol, strip_context
from mathics.core.systemsymbols import SymbolGet
from mathics.settings import ROOT_DIR
Expand Down Expand Up @@ -139,10 +139,12 @@ def __init__(
self.timing_trace_evaluation = False

if add_builtin:

loaded = False
if builtin_filename is not None:
builtin_dates = [get_file_time(module.__file__) for module in modules]
builtin_dates = [
get_file_time(module.__file__)
for module in mathics3_builtins_modules
]
builtin_time = max(builtin_dates)
if get_file_time(builtin_filename) > builtin_time:
builtin_file = open(builtin_filename, "rb")
Expand Down Expand Up @@ -769,7 +771,6 @@ def __init__(
builtin=None,
is_numeric=False,
) -> None:

super(Definition, self).__init__()
self.name = name

Expand Down
26 changes: 17 additions & 9 deletions mathics/core/load_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@
import os.path as osp
import pkgutil
from glob import glob
from types import ModuleType
from typing import List, Optional

from mathics.core.pattern import pattern_objects
from mathics.core.symbols import Symbol
from mathics.eval.makeboxes import builtins_precedence
from mathics.settings import ENABLE_FILES_MODULE

# List of Mathics3 Builtin modules.
# This is initialized via below import_builtins modules
modules = []
# List of Python modules contain Mathics3 Builtins.
# This list used outside to gather documentation,
# and test module consistency. It is
# is initialized via below import_builtins modules
mathics3_builtins_modules: List[ModuleType] = []

_builtins = {}
builtins_by_module = {}
display_operators_set = set()


# The fact that are importing inside here, suggests add_builtins
# should get moved elsewhere.
def add_builtins(new_builtins):
Expand Down Expand Up @@ -56,7 +60,7 @@ def add_builtins(new_builtins):
_builtins.update(dict(new_builtins))


def add_builtins_from_builtin_modules(modules):
def add_builtins_from_builtin_modules(modules: List[ModuleType]):
# This can be put at the top after mathics.builtin.__init__
# cleanup is done.
from mathics.builtin.base import Builtin
Expand Down Expand Up @@ -129,7 +133,7 @@ def import_and_load_builtins():
)
exclude_files = {"codetables", "base"}
module_names = get_module_names(builtin_path, exclude_files)
import_builtins(module_names, modules)
import_builtins(module_names, mathics3_builtins_modules)

# Get import modules in subdirectories of this directory of Python
# modules that contain Mathics3 Builtin class definitions.
Expand All @@ -140,16 +144,20 @@ def import_and_load_builtins():
disable_file_module_names = set() if ENABLE_FILES_MODULE else {"files_io"}

subdirectories = next(os.walk(builtin_path))[1]
import_builtin_subdirectories(subdirectories, disable_file_module_names, modules)
import_builtin_subdirectories(
subdirectories, disable_file_module_names, mathics3_builtins_modules
)

add_builtins_from_builtin_modules(modules)
add_builtins_from_builtin_modules(mathics3_builtins_modules)
initialize_display_operators_set()


# TODO: When we drop Python 3.7,
# module_names can be a List[Literal]
def import_builtins(
module_names: List[str], modules: list, submodule_name: Optional[str] = None
module_names: List[str],
modules: List[ModuleType],
submodule_name: Optional[str] = None,
):
"""
Imports the list of Mathics3 Built-in modules so that inside
Expand Down Expand Up @@ -210,7 +218,7 @@ def initialize_display_operators_set():
display_operators_set.add(operator)


def name_is_builtin_symbol(module, name: str) -> Optional[type]:
def name_is_builtin_symbol(module: ModuleType, name: str) -> Optional[type]:
"""
Checks if ``name`` should be added to definitions, and return
its associated Builtin class.
Expand Down
13 changes: 4 additions & 9 deletions mathics/doc/common_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from mathics.core.evaluation import Message, Print
from mathics.core.load_builtin import (
builtins_by_module as global_builtins_by_module,
modules as mathics3_builtin_modules,
mathics3_builtins_modules,
)
from mathics.core.util import IS_PYPY
from mathics.doc.utils import slugify
Expand Down Expand Up @@ -114,7 +114,8 @@
SUBSECTION_RE = re.compile('(?s)<subsection title="(.*?)">')

TESTCASE_RE = re.compile(
r"""(?mx)^ # re.MULTILINE (multi-line match) and re.VERBOSE (readable regular expressions
r"""(?mx)^ # re.MULTILINE (multi-line match)
# and re.VERBOSE (readable regular expressions
((?:.|\n)*?)
^\s+([>#SX])>[ ](.*) # test-code indicator
((?:\n\s*(?:[:|=.][ ]|\.).*)*) # test-code results"""
Expand All @@ -125,12 +126,6 @@
test_result_map = {}


def _replace_all(text, pairs):
for i, j in pairs:
text = text.replace(i, j)
return text


def get_module_doc(module: ModuleType) -> tuple:
doc = module.__doc__
if doc is not None:
Expand Down Expand Up @@ -638,7 +633,7 @@ def gather_doctest_data(self):
for title, modules, builtins_by_module, start in [
(
"Reference of Built-in Symbols",
mathics3_builtin_modules,
mathics3_builtins_modules,
global_builtins_by_module,
True,
)
Expand Down
15 changes: 10 additions & 5 deletions mathics/doc/latex_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
DocText,
Documentation,
XMLDoc,
_replace_all,
gather_tests,
get_results_by_test,
post_sub,
Expand Down Expand Up @@ -104,7 +103,7 @@ def repl_python(match):

text, post_substitutions = pre_sub(PYTHON_RE, text, repl_python)

text = _replace_all(
text = replace_all(
text,
[
("\\", "\\\\"),
Expand All @@ -120,7 +119,7 @@ def repl_python(match):
def repl(match):
text = match.group(1)
if text:
text = _replace_all(text, [("\\'", "'"), ("^", "\\^")])
text = replace_all(text, [("\\'", "'"), ("^", "\\^")])
escape_char = get_latex_escape_char(text)
text = LATEX_RE.sub(
lambda m: "%s%s\\codevar{\\textit{%s}}%s\\lstinline%s"
Expand Down Expand Up @@ -163,7 +162,7 @@ def repl_list(match):
text = LIST_RE.sub(repl_list, text)

# FIXME: get this from MathicsScanner
text = _replace_all(
text = replace_all(
text,
[
("$", r"\$"),
Expand Down Expand Up @@ -313,7 +312,7 @@ def repl_subsection(match):
def escape_latex_output(text) -> str:
"""Escape Mathics output"""

text = _replace_all(
text = replace_all(
text,
[
("\\", "\\\\"),
Expand Down Expand Up @@ -448,6 +447,12 @@ def repl_nonasy(match):
return OUTSIDE_ASY_RE.sub(repl_nonasy, result)


def replace_all(text, pairs):
for i, j in pairs:
text = text.replace(i, j)
return text


def strip_system_prefix(name):
if name.startswith("System`"):
stripped_name = name[len("System`") :]
Expand Down
4 changes: 4 additions & 0 deletions mathics/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
from mathics.core.symbols import SymbolNull, strip_context
from mathics.timing import show_lru_cache_statistics

# from mathics.timing import TimeitContextManager
# with TimeitContextManager("import_and_load_builtins()"):
# import_and_load_builtins()

import_and_load_builtins()


Expand Down
2 changes: 1 addition & 1 deletion mathics/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# well as importing into Python. That's why there is no
# space around "=" below.
# fmt: off
__version__="6.1.0dev0" # noqa
__version__="7.0.0dev0" # noqa
4 changes: 2 additions & 2 deletions test/consistency-and-style/test_duplicate_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest

from mathics.builtin.base import Builtin
from mathics.core.load_builtin import modules, name_is_builtin_symbol
from mathics.core.load_builtin import mathics3_builtins_modules, name_is_builtin_symbol


@pytest.mark.skipif(
Expand All @@ -18,7 +18,7 @@
def test_check_duplicated():
msg = ""
builtins_by_name = {}
for module in modules:
for module in mathics3_builtins_modules:
vars = dir(module)
for name in vars:
var = name_is_builtin_symbol(module, name)
Expand Down

0 comments on commit 09e43b6

Please sign in to comment.