Skip to content

Commit ee6ffac

Browse files
authored
Merge pull request #1387 from mathics/pyston-compatability
Make pyston 2.2 compatible
2 parents a1ec1bb + 2f30e2c commit ee6ffac

2 files changed

Lines changed: 139 additions & 130 deletions

File tree

mathics/builtin/datentime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def apply(self, evaluation):
126126
return SymbolInfinity
127127

128128

129-
if sys.platform != "win32":
129+
if sys.platform != "win32" and ("Pyston" not in sys.version):
130130

131131
class TimeConstrained(Builtin):
132132
"""

mathics/builtin/drawing/image.py

Lines changed: 138 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,151 +2413,160 @@ def apply(self, image, evaluation, options):
24132413
return String(text)
24142414

24152415

2416-
class WordCloud(Builtin):
2417-
"""
2418-
<dl>
2419-
<dt>'WordCloud[{$word1$, $word2$, ...}]'
2420-
<dd>Gives a word cloud with the given list of words.
2421-
<dt>'WordCloud[{$weight1$ -> $word1$, $weight2$ -> $word2$, ...}]'
2422-
<dd>Gives a word cloud with the words weighted using the given weights.
2423-
<dt>'WordCloud[{$weight1$, $weight2$, ...} -> {$word1$, $word2$, ...}]'
2424-
<dd>Also gives a word cloud with the words weighted using the given weights.
2425-
<dt>'WordCloud[{{$word1$, $weight1$}, {$word2$, $weight2$}, ...}]'
2426-
<dd>Gives a word cloud with the words weighted using the given weights.
2427-
</dl>
2428-
2429-
>> WordCloud[StringSplit[Import["ExampleData/EinsteinSzilLetter.txt"]]]
2430-
= -Image-
2431-
2432-
>> WordCloud[Range[50] -> ToString /@ Range[50]]
2433-
= -Image-
2434-
"""
2435-
2436-
requires = _image_requires + ("wordcloud",)
2437-
2438-
options = {"IgnoreCase": "True", "ImageSize": "Automatic", "MaxItems": "Automatic"}
2416+
import sys
2417+
2418+
if "Pyston" not in sys.version:
2419+
2420+
class WordCloud(Builtin):
2421+
"""
2422+
<dl>
2423+
<dt>'WordCloud[{$word1$, $word2$, ...}]'
2424+
<dd>Gives a word cloud with the given list of words.
2425+
<dt>'WordCloud[{$weight1$ -> $word1$, $weight2$ -> $word2$, ...}]'
2426+
<dd>Gives a word cloud with the words weighted using the given weights.
2427+
<dt>'WordCloud[{$weight1$, $weight2$, ...} -> {$word1$, $word2$, ...}]'
2428+
<dd>Also gives a word cloud with the words weighted using the given weights.
2429+
<dt>'WordCloud[{{$word1$, $weight1$}, {$word2$, $weight2$}, ...}]'
2430+
<dd>Gives a word cloud with the words weighted using the given weights.
2431+
</dl>
2432+
2433+
>> WordCloud[StringSplit[Import["ExampleData/EinsteinSzilLetter.txt"]]]
2434+
= -Image-
2435+
2436+
>> WordCloud[Range[50] -> ToString /@ Range[50]]
2437+
= -Image-
2438+
"""
2439+
2440+
requires = _image_requires + ("wordcloud",)
2441+
2442+
options = {
2443+
"IgnoreCase": "True",
2444+
"ImageSize": "Automatic",
2445+
"MaxItems": "Automatic",
2446+
}
24392447

2440-
# this is the palettable.colorbrewer.qualitative.Dark2_8 palette
2441-
default_colors = (
2442-
(27, 158, 119),
2443-
(217, 95, 2),
2444-
(117, 112, 179),
2445-
(231, 41, 138),
2446-
(102, 166, 30),
2447-
(230, 171, 2),
2448-
(166, 118, 29),
2449-
(102, 102, 102),
2450-
)
2448+
# this is the palettable.colorbrewer.qualitative.Dark2_8 palette
2449+
default_colors = (
2450+
(27, 158, 119),
2451+
(217, 95, 2),
2452+
(117, 112, 179),
2453+
(231, 41, 138),
2454+
(102, 166, 30),
2455+
(230, 171, 2),
2456+
(166, 118, 29),
2457+
(102, 102, 102),
2458+
)
24512459

2452-
def apply_words_weights(self, weights, words, evaluation, options):
2453-
"WordCloud[weights_List -> words_List, OptionsPattern[%(name)s]]"
2454-
if len(weights.leaves) != len(words.leaves):
2455-
return
2460+
def apply_words_weights(self, weights, words, evaluation, options):
2461+
"WordCloud[weights_List -> words_List, OptionsPattern[%(name)s]]"
2462+
if len(weights.leaves) != len(words.leaves):
2463+
return
24562464

2457-
def weights_and_words():
2458-
for weight, word in zip(weights.leaves, words.leaves):
2459-
yield weight.round_to_float(), word.get_string_value()
2465+
def weights_and_words():
2466+
for weight, word in zip(weights.leaves, words.leaves):
2467+
yield weight.round_to_float(), word.get_string_value()
24602468

2461-
return self._word_cloud(weights_and_words(), evaluation, options)
2469+
return self._word_cloud(weights_and_words(), evaluation, options)
24622470

2463-
def apply_words(self, words, evaluation, options):
2464-
"WordCloud[words_List, OptionsPattern[%(name)s]]"
2471+
def apply_words(self, words, evaluation, options):
2472+
"WordCloud[words_List, OptionsPattern[%(name)s]]"
24652473

2466-
if not words:
2467-
return
2468-
elif isinstance(words.leaves[0], String):
2474+
if not words:
2475+
return
2476+
elif isinstance(words.leaves[0], String):
24692477

2470-
def weights_and_words():
2471-
for word in words.leaves:
2472-
yield 1, word.get_string_value()
2478+
def weights_and_words():
2479+
for word in words.leaves:
2480+
yield 1, word.get_string_value()
24732481

2474-
else:
2475-
2476-
def weights_and_words():
2477-
for word in words.leaves:
2478-
if len(word.leaves) != 2:
2479-
raise ValueError
2480-
2481-
head_name = word.get_head_name()
2482-
if head_name == "System`Rule":
2483-
weight, s = word.leaves
2484-
elif head_name == "System`List":
2485-
s, weight = word.leaves
2486-
else:
2487-
raise ValueError
2482+
else:
24882483

2489-
yield weight.round_to_float(), s.get_string_value()
2484+
def weights_and_words():
2485+
for word in words.leaves:
2486+
if len(word.leaves) != 2:
2487+
raise ValueError
24902488

2491-
try:
2492-
return self._word_cloud(weights_and_words(), evaluation, options)
2493-
except ValueError:
2494-
return
2489+
head_name = word.get_head_name()
2490+
if head_name == "System`Rule":
2491+
weight, s = word.leaves
2492+
elif head_name == "System`List":
2493+
s, weight = word.leaves
2494+
else:
2495+
raise ValueError
24952496

2496-
def _word_cloud(self, words, evaluation, options):
2497-
ignore_case = self.get_option(options, "IgnoreCase", evaluation) == Symbol(
2498-
"True"
2499-
)
2497+
yield weight.round_to_float(), s.get_string_value()
25002498

2501-
freq = defaultdict(int)
2502-
for py_weight, py_word in words:
2503-
if py_word is None or py_weight is None:
2499+
try:
2500+
return self._word_cloud(weights_and_words(), evaluation, options)
2501+
except ValueError:
25042502
return
2505-
key = py_word.lower() if ignore_case else py_word
2506-
freq[key] += py_weight
25072503

2508-
max_items = self.get_option(options, "MaxItems", evaluation)
2509-
if isinstance(max_items, Integer):
2510-
py_max_items = max_items.get_int_value()
2511-
else:
2512-
py_max_items = 200
2504+
def _word_cloud(self, words, evaluation, options):
2505+
ignore_case = self.get_option(options, "IgnoreCase", evaluation) == Symbol(
2506+
"True"
2507+
)
25132508

2514-
image_size = self.get_option(options, "ImageSize", evaluation)
2515-
if image_size == Symbol("Automatic"):
2516-
py_image_size = (800, 600)
2517-
elif (
2518-
image_size.get_head_name() == "System`List" and len(image_size.leaves) == 2
2519-
):
2520-
py_image_size = []
2521-
for leaf in image_size.leaves:
2522-
if not isinstance(leaf, Integer):
2509+
freq = defaultdict(int)
2510+
for py_weight, py_word in words:
2511+
if py_word is None or py_weight is None:
25232512
return
2524-
py_image_size.append(leaf.get_int_value())
2525-
elif isinstance(image_size, Integer):
2526-
size = image_size.get_int_value()
2527-
py_image_size = (size, size)
2528-
else:
2529-
return
2513+
key = py_word.lower() if ignore_case else py_word
2514+
freq[key] += py_weight
25302515

2531-
# inspired by http://minimaxir.com/2016/05/wordclouds/
2532-
import random
2533-
import os
2516+
max_items = self.get_option(options, "MaxItems", evaluation)
2517+
if isinstance(max_items, Integer):
2518+
py_max_items = max_items.get_int_value()
2519+
else:
2520+
py_max_items = 200
2521+
2522+
image_size = self.get_option(options, "ImageSize", evaluation)
2523+
if image_size == Symbol("Automatic"):
2524+
py_image_size = (800, 600)
2525+
elif (
2526+
image_size.get_head_name() == "System`List"
2527+
and len(image_size.leaves) == 2
2528+
):
2529+
py_image_size = []
2530+
for leaf in image_size.leaves:
2531+
if not isinstance(leaf, Integer):
2532+
return
2533+
py_image_size.append(leaf.get_int_value())
2534+
elif isinstance(image_size, Integer):
2535+
size = image_size.get_int_value()
2536+
py_image_size = (size, size)
2537+
else:
2538+
return
25342539

2535-
def color_func(
2536-
word, font_size, position, orientation, random_state=None, **kwargs
2537-
):
2538-
return self.default_colors[random.randint(0, 7)]
2539-
2540-
font_base_path = os.path.dirname(os.path.abspath(__file__)) + "/../fonts/"
2541-
2542-
font_path = os.path.realpath(font_base_path + "AmaticSC-Bold.ttf")
2543-
if not os.path.exists(font_path):
2544-
font_path = None
2545-
2546-
from wordcloud import WordCloud
2547-
2548-
wc = WordCloud(
2549-
width=py_image_size[0],
2550-
height=py_image_size[1],
2551-
font_path=font_path,
2552-
max_font_size=300,
2553-
mode="RGB",
2554-
background_color="white",
2555-
max_words=py_max_items,
2556-
color_func=color_func,
2557-
random_state=42,
2558-
stopwords=set(),
2559-
)
2560-
wc.generate_from_frequencies(freq)
2540+
# inspired by http://minimaxir.com/2016/05/wordclouds/
2541+
import random
2542+
import os
2543+
2544+
def color_func(
2545+
word, font_size, position, orientation, random_state=None, **kwargs
2546+
):
2547+
return self.default_colors[random.randint(0, 7)]
2548+
2549+
font_base_path = os.path.dirname(os.path.abspath(__file__)) + "/../fonts/"
2550+
2551+
font_path = os.path.realpath(font_base_path + "AmaticSC-Bold.ttf")
2552+
if not os.path.exists(font_path):
2553+
font_path = None
2554+
2555+
from wordcloud import WordCloud
2556+
2557+
wc = WordCloud(
2558+
width=py_image_size[0],
2559+
height=py_image_size[1],
2560+
font_path=font_path,
2561+
max_font_size=300,
2562+
mode="RGB",
2563+
background_color="white",
2564+
max_words=py_max_items,
2565+
color_func=color_func,
2566+
random_state=42,
2567+
stopwords=set(),
2568+
)
2569+
wc.generate_from_frequencies(freq)
25612570

2562-
image = wc.to_image()
2563-
return Image(numpy.array(image), "RGB")
2571+
image = wc.to_image()
2572+
return Image(numpy.array(image), "RGB")

0 commit comments

Comments
 (0)