-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy path__init__.py
More file actions
executable file
·286 lines (229 loc) · 8.36 KB
/
__init__.py
File metadata and controls
executable file
·286 lines (229 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# -*- coding: utf-8 -*-
"""
Mathics Built-in Functions and Variables.
Mathics has over a thousand Built-in Functions and variables, all of which are defined here.
"""
from typing import Optional
import glob
import importlib
import inspect
import pkgutil
import re
import os.path as osp
from mathics.builtin.base import (
Builtin,
SympyObject,
Operator,
PatternObject,
)
from mathics.settings import ENABLE_FILES_MODULE
from mathics.version import __version__ # noqa used in loading to check consistency.
from typing import List
# Set this to True to print all the builtins that do not have
# a summary_text. In the future, we can set this to True
# and raise an error if a new builtin is added without
# this property or if it does not fulfill some other conditions.
RUN_SANITY_TEST = False
# Get a list of files in this directory. We'll exclude from the start
# files with leading characters we don't want like __init__ with its leading underscore.
__py_files__ = [
osp.basename(f[0:-3])
for f in glob.glob(osp.join(osp.dirname(__file__), "[a-z]*.py"))
]
def add_builtins(new_builtins):
for var_name, builtin in new_builtins:
name = builtin.get_name()
if hasattr(builtin, "python_equivalent"):
# print("XXX0", builtin.python_equivalent)
mathics_to_python[name] = builtin.python_equivalent
if isinstance(builtin, SympyObject):
mathics_to_sympy[name] = builtin
for sympy_name in builtin.get_sympy_names():
# print("XXX1", sympy_name)
sympy_to_mathics[sympy_name] = builtin
if isinstance(builtin, Operator):
builtins_precedence[name] = builtin.precedence
if isinstance(builtin, PatternObject):
pattern_objects[name] = builtin.__class__
_builtins.update(dict(new_builtins))
def builtins_dict():
return {
builtin.get_name(): builtin
for modname, builtins in builtins_by_module.items()
for builtin in builtins
}
def contribute(definitions):
# let MakeBoxes contribute first
_builtins["System`MakeBoxes"].contribute(definitions)
for name, item in _builtins.items():
if name != "System`MakeBoxes":
item.contribute(definitions)
from mathics.core.expression import ensure_context
from mathics.core.parser import all_operator_names
from mathics.core.definitions import Definition
# All builtins are loaded. Create dummy builtin definitions for
# any remaining operators that don't have them. This allows
# operators like \[Cup] to behave correctly.
for operator in all_operator_names:
if not definitions.have_definition(ensure_context(operator)):
op = ensure_context(operator)
definitions.builtin[op] = Definition(name=op)
def get_module_doc(module):
doc = module.__doc__
if doc is not None:
doc = doc.strip()
if doc:
title = doc.splitlines()[0]
text = "\n".join(doc.splitlines()[1:])
else:
title = module.__name__
for prefix in ("mathics.builtin.", "mathics.optional."):
if title.startswith(prefix):
title = title[len(prefix) :]
title = title.capitalize()
text = ""
return title, text
def import_builtins(module_names: List[str], submodule_name=None) -> None:
"""
Imports the list of Mathics Built-in modules so that inside
Mathics we have these Builtin Functions, like Plus[], List[] are defined.
"""
def import_module(module_name: str, import_name: str):
try:
module = importlib.import_module(import_name)
except Exception as e:
print(e)
print(f" Not able to load {module_name}. Check your installation.")
print(f" mathics.builtin loads from {__file__[:-11]}")
return None
if module:
modules.append(module)
if submodule_name:
import_module(submodule_name, f"mathics.builtin.{submodule_name}")
for module_name in module_names:
import_name = (
f"mathics.builtin.{submodule_name}.{module_name}"
if submodule_name
else f"mathics.builtin.{module_name}"
)
import_module(module_name, import_name)
def name_is_builtin_symbol(module, name: str) -> Optional[type]:
"""
Checks if ``name`` should be added to definitions, and return
its associated Builtin class.
Return ``None`` if the name should not get added to definitions.
"""
if name.startswith("_"):
return None
module_object = getattr(module, name)
# Look only at Class objects.
if not inspect.isclass(module_object):
return None
# FIXME: tests involving module_object.__module__ are fragile and
# Python implementation specific. Figure out how to do this
# via the inspect module which is not implementation specific.
# Skip those builtins defined in or imported from another module.
if module_object.__module__ != module.__name__:
return None
# Skip objects in module mathics.builtin.base.
if module_object.__module__ == "mathics.builtin.base":
return None
# Skip those builtins that are not submodules of mathics.builtin.
if not module_object.__module__.startswith("mathics.builtin."):
return None
# If it is not a subclass of Builtin, skip it.
if not issubclass(module_object, Builtin):
return None
# Skip Builtin classes that were explicitly marked for skipping.
if module_object in getattr(module, "DOES_NOT_ADD_BUILTIN_DEFINITION", []):
return None
return module_object
def sanity_check(cls, module):
if not RUN_SANITY_TEST:
return True
if not hasattr(cls, "summary_text"):
print(
"In ",
module.__name__,
cls.__name__,
" does not have a summary_text.",
)
return False
return True
# FIXME: redo using importlib since that is probably less fragile.
exclude_files = set(("codetables", "base"))
module_names = [
f for f in __py_files__ if re.match("^[a-z0-9]+$", f) if f not in exclude_files
]
modules = []
import_builtins(module_names)
_builtins = []
builtins_by_module = {}
disable_file_module_names = (
[] if ENABLE_FILES_MODULE else ["files_io.files", "files_io.importexport"]
)
for subdir in (
"arithfns",
"assignments",
"atomic",
"binary",
"box",
"colors",
"distance",
"drawing",
"fileformats",
"files_io",
"forms",
"functional",
"intfns",
"list",
"matrices",
"numbers",
"quantum_mechanics",
"specialfns",
"statistics",
"string",
"vectors",
):
import_name = f"{__name__}.{subdir}"
if import_name in disable_file_module_names:
continue
builtin_module = importlib.import_module(import_name)
submodule_names = [
modname
for importer, modname, ispkg in pkgutil.iter_modules(builtin_module.__path__)
]
# print("XXX3", submodule_names)
import_builtins(submodule_names, subdir)
for module in modules:
builtins_by_module[module.__name__] = []
module_vars = dir(module)
for name in module_vars:
builtin_class = name_is_builtin_symbol(module, name)
if builtin_class is not None:
instance = builtin_class(expression=False)
if isinstance(instance, Builtin):
# This set the default context for symbols in mathics.builtins
if not type(instance).context:
type(instance).context = "System`"
assert sanity_check(
builtin_class, module
), f"In {module.__name__} Builtin <<{builtin_class.__name__}>> did not pass the sanity check."
_builtins.append((instance.get_name(), instance))
builtins_by_module[module.__name__].append(instance)
mathics_to_sympy = {} # here we have: name -> sympy object
mathics_to_python = {} # here we have: name -> string
sympy_to_mathics = {}
pattern_objects = {}
builtins_precedence = {}
new_builtins = _builtins
# FIXME: some magic is going on here..
_builtins = {}
add_builtins(new_builtins)
display_operators_set = set()
for modname, builtins in builtins_by_module.items():
for builtin in builtins:
# name = builtin.get_name()
operator = builtin.get_operator_display()
if operator is not None:
display_operators_set.add(operator)