Skip to content

Commit 638d6f1

Browse files
committed
Merge remote-tracking branch 'upstream/master' into improvestringsplit
2 parents c4a055f + 365774a commit 638d6f1

File tree

6 files changed

+121
-90
lines changed

6 files changed

+121
-90
lines changed

mathics/benchmark.py

Lines changed: 70 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,39 @@
2626

2727
# Mathics expressions to benchmark
2828
BENCHMARKS = {
29-
'Arithmetic': ['1 + 2', '5 * 3'],
30-
'Plot': [
31-
'Plot[0, {x, -3, 3}]',
32-
'Plot[x^2 + x + 1, {x, -3, 3}]',
33-
'Plot[Sin[Cos[x^2]], {x, -3, 3}]',
34-
'Plot[Sin[100 x], {x, -3, 3}]'],
35-
'Plot3D': [
36-
'Plot3D[0, {x, -1, 1}, {y, -1, 1}]',
37-
'Plot3D[x + y^2, {x, -3, 3}, {y, -2, 2}]',
38-
'Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -3, 3}]',
39-
'Plot3D[Sin[100 x + 100 y ^ 2], {x, 0, 1}, {y, 0, 1}]'],
40-
'DensityPlot': [
41-
'DensityPlot[x + y^2, {x, -3, 3}, {y, -2, 2}]'],
42-
'Trig': [
43-
'Sin[RandomReal[]]', 'ArcTan[RandomReal[]]'],
44-
'Random': [
45-
'RandomInteger[{-100, 100}, 100]',
46-
'RandomInteger[10, {10, 10}]',
47-
'RandomInteger[{0,1}, {5, 5, 5}]',
48-
'RandomReal[1, 100]', 'RandomReal[{-1, 1}, 100]',
49-
'RandomComplex[2 + I, 50]',
50-
'RandomComplex[{-1 - I, 1 + I}, {10, 10}]'],
51-
'Expand': [
52-
'Expand[(a1+a2)^200]', 'Expand[(a1+a2+a3)^25]',
53-
'Expand[(a1+a2+a3+a4+a5+a6+a7)^3]'],
54-
'Matrix': [
55-
'RandomInteger[{0,1}, {10,10}] . RandomInteger[{0,1}, {10,10}]',
56-
'RandomInteger[{0,10}, {10,10}] + RandomInteger[{0,10}, {10,10}]'],
29+
"Arithmetic": ["1 + 2", "5 * 3"],
30+
"Plot": [
31+
"Plot[0, {x, -3, 3}]",
32+
"Plot[x^2 + x + 1, {x, -3, 3}]",
33+
"Plot[Sin[Cos[x^2]], {x, -3, 3}]",
34+
"Plot[Sin[100 x], {x, -3, 3}]",
35+
],
36+
"Plot3D": [
37+
"Plot3D[0, {x, -1, 1}, {y, -1, 1}]",
38+
"Plot3D[x + y^2, {x, -3, 3}, {y, -2, 2}]",
39+
"Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -3, 3}]",
40+
"Plot3D[Sin[100 x + 100 y ^ 2], {x, 0, 1}, {y, 0, 1}]",
41+
],
42+
"DensityPlot": ["DensityPlot[x + y^2, {x, -3, 3}, {y, -2, 2}]"],
43+
"Trig": ["Sin[RandomReal[]]", "ArcTan[RandomReal[]]"],
44+
"Random": [
45+
"RandomInteger[{-100, 100}, 100]",
46+
"RandomInteger[10, {10, 10}]",
47+
"RandomInteger[{0,1}, {5, 5, 5}]",
48+
"RandomReal[1, 100]",
49+
"RandomReal[{-1, 1}, 100]",
50+
"RandomComplex[2 + I, 50]",
51+
"RandomComplex[{-1 - I, 1 + I}, {10, 10}]",
52+
],
53+
"Expand": [
54+
"Expand[(a1+a2)^200]",
55+
"Expand[(a1+a2+a3)^25]",
56+
"Expand[(a1+a2+a3+a4+a5+a6+a7)^3]",
57+
],
58+
"Matrix": [
59+
"RandomInteger[{0,1}, {10,10}] . RandomInteger[{0,1}, {10,10}]",
60+
"RandomInteger[{0,10}, {10,10}] + RandomInteger[{0,10}, {10,10}]",
61+
],
5762
}
5863

5964
DEPTH = 300
@@ -63,10 +68,10 @@
6368
";".join(map(str, range(1, DEPTH))),
6469
"/".join(map(str, range(1, DEPTH))),
6570
"^".join(map(str, range(1, DEPTH))),
66-
"! " * DEPTH + 'expr',
67-
"!" * DEPTH + 'expr',
68-
'expr' + "& " * DEPTH,
69-
"Sin[" * DEPTH + '0.5' + "]" * DEPTH,
71+
"! " * DEPTH + "expr",
72+
"!" * DEPTH + "expr",
73+
"expr" + "& " * DEPTH,
74+
"Sin[" * DEPTH + "0.5" + "]" * DEPTH,
7075
]
7176

7277
definitions = Definitions(add_builtin=True)
@@ -108,14 +113,17 @@ def timeit(func, repeats=None):
108113

109114
times.append(time.process_time())
110115

111-
times = [times[i+1] - times[i] for i in range(repeats)]
116+
times = [times[i + 1] - times[i] for i in range(repeats)]
112117

113118
average_time = format_time_units(mean(times))
114119
best_time = format_time_units(min(times))
115120
median_time = format_time_units(median(times))
116121

117-
print(" {0:5n} loops, avg: {1}, best: {2}, median: {3} per loop".format(
118-
repeats, average_time, best_time, median_time))
122+
print(
123+
" {0:5n} loops, avg: {1}, best: {2}, median: {3} per loop".format(
124+
repeats, average_time, best_time, median_time
125+
)
126+
)
119127

120128

121129
def truncate_line(string):
@@ -133,16 +141,17 @@ def benchmark_parse_file(fname):
133141
try:
134142
import urllib.request
135143
except ImportError:
136-
print('install urllib for Combinatorica parsing test')
144+
print("install urllib for Combinatorica parsing test")
137145
return
138146
print(" '{0}'".format(truncate_line(fname)))
139147
with urllib.request.urlopen(fname) as f:
140-
code = f.read().decode('utf-8')
148+
code = f.read().decode("utf-8")
141149

142150
def do_parse():
143151
feeder = MathicsMultiLineFeeder(code)
144152
while not feeder.empty():
145153
parse(definitions, feeder)
154+
146155
timeit(do_parse)
147156

148157

@@ -151,7 +160,8 @@ def benchmark_parser():
151160
for expression_string in PARSING_BENCHMARKS:
152161
benchmark_parse(expression_string)
153162
benchmark_parse_file(
154-
'http://www.cs.uiowa.edu/~sriram/Combinatorica/NewCombinatorica.m')
163+
"http://www.cs.uiowa.edu/~sriram/Combinatorica/NewCombinatorica.m"
164+
)
155165

156166

157167
def benchmark_format(expression_string):
@@ -181,30 +191,37 @@ def benchmark_all_sections():
181191

182192
def main():
183193
global evaluation, TESTS_PER_BENCHMARK
184-
parser = ArgumentParser(
185-
description="Mathics benchmark suite.", add_help=False)
194+
parser = ArgumentParser(description="Mathics benchmark suite.", add_help=False)
186195

187196
parser.add_argument(
188-
'--help', '-h', help='show this help message and exit', action='help')
197+
"--help", "-h", help="show this help message and exit", action="help"
198+
)
189199

190200
parser.add_argument(
191-
'--version', '-v', action='version',
192-
version='%(prog)s ' + mathics.__version__)
201+
"--version", "-v", action="version", version="%(prog)s " + mathics.__version__
202+
)
193203

194204
parser.add_argument(
195-
'--section', '-s', dest="section", metavar="SECTION",
196-
help="only test SECTION")
205+
"--section", "-s", dest="section", metavar="SECTION", help="only test SECTION"
206+
)
197207

198-
parser.add_argument(
199-
'-p', '--parser', action='store_true', help="only test parser")
208+
parser.add_argument("-p", "--parser", action="store_true", help="only test parser")
200209

201210
parser.add_argument(
202-
'--expression', '-e', dest="expression", metavar="EXPRESSION",
203-
help="benchmark a valid Mathics expression")
211+
"--expression",
212+
"-e",
213+
dest="expression",
214+
metavar="EXPRESSION",
215+
help="benchmark a valid Mathics expression",
216+
)
204217

205218
parser.add_argument(
206-
'--number', '-n', dest="repeat", metavar="REPEAT",
207-
help="loop REPEAT number of times")
219+
"--number",
220+
"-n",
221+
dest="repeat",
222+
metavar="REPEAT",
223+
help="loop REPEAT number of times",
224+
)
208225

209226
args = parser.parse_args()
210227

@@ -221,5 +238,6 @@ def main():
221238
benchmark_all_sections()
222239
benchmark_parser()
223240

224-
if __name__ == '__main__':
241+
242+
if __name__ == "__main__":
225243
main()

mathics/main.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ def main() -> int:
249249
"multiple times)",
250250
)
251251

252-
argparser.add_argument("--colors", nargs="?", help="interactive shell colors. Use value 'NoColor' or 'None' to disable ANSI color decoration")
252+
argparser.add_argument(
253+
"--colors",
254+
nargs="?",
255+
help="interactive shell colors. Use value 'NoColor' or 'None' to disable ANSI color decoration",
256+
)
253257

254258
argparser.add_argument(
255259
"--no-completion", help="disable tab completion", action="store_true"
@@ -378,5 +382,6 @@ def main() -> int:
378382
shell.reset_lineno()
379383
return exit_rc
380384

385+
381386
if __name__ == "__main__":
382387
sys.exit(main())

mathics/profile_mathics.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
def prepare():
1515
pass
1616

17+
1718
result = None
1819

1920

@@ -30,20 +31,21 @@ def run():
3031
# 'Plus@@f/@Symbol/@StringJoin/@Tuples[CharacterRange["a","z"],2]')
3132
# prompt = 'FullForm[Nest[1+Sqrt[1+#]&, x, 20]]'
3233
# prompt = '1+2'
33-
prompt = 'DensityPlot[x*y,{x,-1,1},{y,-1,1}]'
34-
evaluation = Evaluation(definitions, format='xml')
34+
prompt = "DensityPlot[x*y,{x,-1,1},{y,-1,1}]"
35+
evaluation = Evaluation(definitions, format="xml")
3536
result = evaluation.parse_evaluate(prompt)
3637
except KeyboardInterrupt:
37-
result = 'INTERRUPTED'
38+
result = "INTERRUPTED"
3839

3940

4041
def _profile():
4142
global result
4243
prepare()
43-
cProfile.run('run()', 'profile')
44-
p = pstats.Stats('profile')
45-
p.sort_stats('cumulative').print_stats(50)
44+
cProfile.run("run()", "profile")
45+
p = pstats.Stats("profile")
46+
p.sort_stats("cumulative").print_stats(50)
4647
p.print_callees(20)
4748

48-
if __name__ == '__main__':
49+
50+
if __name__ == "__main__":
4951
_profile()

mathics/session.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
class MathicsSession:
88
def __init__(self, add_builtin=True, catch_interrupt=False, form="InputForm"):
99
self.definitions = Definitions(add_builtin)
10-
self.evaluation = Evaluation(definitions=self.definitions, catch_interrupt=catch_interrupt)
10+
self.evaluation = Evaluation(
11+
definitions=self.definitions, catch_interrupt=catch_interrupt
12+
)
1113
self.form = form
1214
self.last_result = None
1315

14-
def evaluate(self, str_expression, timeout = None, form=None):
16+
def evaluate(self, str_expression, timeout=None, form=None):
1517
expr = parse(self.definitions, MathicsSingleLineFeeder(str_expression))
1618
if form is None:
1719
form = self.form
@@ -20,11 +22,9 @@ def evaluate(self, str_expression, timeout = None, form=None):
2022

2123
def format_result(self, str_expression=None, timeout=None, form=None):
2224
if str_expression:
23-
self.evaluate(str_expression, timeout = None, form=None)
25+
self.evaluate(str_expression, timeout=None, form=None)
2426

2527
res = self.last_result
2628
if form is None:
2729
form = self.form
2830
return self.last_result.do_format(self.evaluation, form)
29-
30-

mathics/settings.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,35 @@
3030
# historically 10000 was used on public mathics servers
3131
MAX_STORED_SIZE = 10000
3232

33-
ADMINS = (
34-
('Admin', '[email protected]'),
35-
)
33+
ADMINS = (("Admin", "[email protected]"),)
3634
MANAGERS = ADMINS
3735

38-
ROOT_DIR = pkg_resources.resource_filename('mathics', '')
39-
if sys.platform.startswith('win'):
40-
DATA_DIR = os.environ['APPDATA'].replace(os.sep, '/') + '/Python/Mathics/'
36+
ROOT_DIR = pkg_resources.resource_filename("mathics", "")
37+
if sys.platform.startswith("win"):
38+
DATA_DIR = os.environ["APPDATA"].replace(os.sep, "/") + "/Python/Mathics/"
4139
else:
42-
DATA_DIR = path.expanduser('~/.local/var/mathics/')
40+
DATA_DIR = path.expanduser("~/.local/var/mathics/")
4341
# if not path.exists(DATA_DIR):
4442
# os.makedirs(DATA_DIR)
4543

46-
DOC_DIR = os.path.join(ROOT_DIR, 'doc/documentation/')
47-
DOC_TEX_DATA = os.path.join(ROOT_DIR, 'doc/tex/data')
48-
DOC_XML_DATA = os.path.join(ROOT_DIR, 'doc/xml/data')
49-
DOC_LATEX_FILE = os.path.join(ROOT_DIR, 'doc/tex/documentation.tex')
44+
DOC_DIR = os.path.join(ROOT_DIR, "doc/documentation/")
45+
DOC_TEX_DATA = os.path.join(ROOT_DIR, "doc/tex/data")
46+
DOC_XML_DATA = os.path.join(ROOT_DIR, "doc/xml/data")
47+
DOC_LATEX_FILE = os.path.join(ROOT_DIR, "doc/tex/documentation.tex")
5048

5149
# Local time zone for this installation. Choices can be found here:
5250
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
5351
# although not all choices may be available on all operating systems.
5452
# If running in a Windows environment this must be set to the same as your
5553
# system time zone.
56-
TIME_ZONE = 'Europe/Vienna'
54+
TIME_ZONE = "Europe/Vienna"
5755

5856
# Set this True if you prefer 12 hour time to be the default
5957
TIME_12HOUR = False
6058

6159
# Language code for this installation. All choices can be found here:
6260
# http://www.i18nguy.com/unicode/language-identifiers.html
63-
LANGUAGE_CODE = 'en-us'
61+
LANGUAGE_CODE = "en-us"
6462

6563
SITE_ID = 1
6664

@@ -72,4 +70,4 @@
7270
# whatever it is that setting this thing did.
7371
default_pymathics_modules = []
7472

75-
SYSTEM_CHARACTER_ENCODING = "UTF-8" if sys.getdefaultencoding()=='utf-8' else 'ASCII'
73+
SYSTEM_CHARACTER_ENCODING = "UTF-8" if sys.getdefaultencoding() == "utf-8" else "ASCII"

0 commit comments

Comments
 (0)