Skip to content

Commit 3f02739

Browse files
committed
switch to polyglot api for eval
1 parent 9cdf715 commit 3f02739

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

graalpython/benchmarks/src/benchmarks/interop/python_r_image_demo.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3636
# SOFTWARE.
3737

38+
import polyglot
3839
import sys
3940
import os
4041

@@ -44,21 +45,19 @@
4445

4546
from image_magix import Image
4647

47-
MIME_R = "application/x-r"
48-
49-
load_jpeg = eval(compile("""function(file.name) {
48+
load_jpeg = polyglot.eval(string="""function(file.name) {
5049
jimg <- read.csv(gzfile(file.name))
5150
return (jimg)
52-
}""", "", MIME_R))
51+
}""", language="R")
5352

5453
print("stage 1")
5554

5655
working_dir_parts.append("img.csv.gz")
5756
raw_data = load_jpeg(os.sep.join(working_dir_parts))
5857

5958
# the dimensions are R attributes; define function to access them
60-
getDim = eval(compile("function(v, pos) dim(v)[[pos]]", "", MIME_R))
61-
getDataRowMajor = eval(compile("function(v) as.vector(t(v))", "", MIME_R))
59+
getDim = polyglot.eval(string="function(v, pos) dim(v)[[pos]]", language="R")
60+
getDataRowMajor = polyglot.eval(string="function(v) as.vector(t(v))", language="R")
6261

6362
print("stage 2")
6463

@@ -77,17 +76,16 @@
7776

7877
print("stage 3")
7978

80-
draw = eval(compile("""function(processedImgObj) {
79+
draw = polyglot.eval(string="""function(processedImgObj) {
8180
require(grDevices)
8281
require(grid)
8382
mx <- matrix(processedImgObj$`@data`/255, nrow=processedImgObj$`@height`, ncol=processedImgObj$`@width`)
8483
cat("rows:", nrow(mx), "\n")
8584
grid.newpage()
8685
grid.raster(mx, height=unit(nrow(mx),"points"))
8786
cat("DONE\n")
88-
}""", "", MIME_R))
87+
}""", language="R")
8988

9089
draw(result)
9190

92-
eval(compile("dev.off()", "", MIME_R))
93-
91+
polyglot.eval(string="dev.off()", language="R")

0 commit comments

Comments
 (0)