<commit_before>package com.metaweb.gridworks.expr;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyFunction;
import org.python.core.PyNone;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;

public class JythonEvaluable implements Evaluable {
	private static final String s_functionName = "___temp___";
	
    private static PythonInterpreter _engine = new PythonInterpreter();

    public JythonEvaluable(String s) {
        // indent and create a function out of the code
        String[] lines = s.split("\r\n|\r|\n");
        
        StringBuffer sb = new StringBuffer(1024);
        sb.append("def ");
        sb.append(s_functionName);
        sb.append("(value, cell, cells, row, rowIndex):");
        for (int i = 0; i < lines.length; i++) {
            sb.append("\n  ");
            sb.append(lines[i]);
        }

        _engine.exec(sb.toString());
    }
    
    public Object evaluate(Properties bindings) {
        try {
            // call the temporary PyFunction directly
            Object result = ((PyFunction)_engine.get(s_functionName)).__call__(
                new PyObject[] {
                    Py.java2py( bindings.get("value") ),
                    new JythonHasFieldsWrapper((HasFields) bindings.get("cell"), bindings),
                    new JythonHasFieldsWrapper((HasFields) bindings.get("cells"), bindings),
                    new JythonHasFieldsWrapper((HasFields) bindings.get("row"), bindings),
                    Py.java2py( bindings.get("rowIndex") )
                }
            );

            return unwrap(result);
        } catch (PyException e) {
            return new EvalError(e.getMessage());
        }
    }
    
    protected Object unwrap(Object result) {
        if (result != null) {
            if (result instanceof JythonObjectWrapper) {
                return ((JythonObjectWrapper) result)._obj;
            } else if (result instanceof JythonHasFieldsWrapper) {
                return ((JythonHasFieldsWrapper) result)._obj;
            } else if (result instanceof PyString) {
                return ((PyString) result).asString();
            } else if (result instanceof PyObject) {
                return unwrap((PyObject) result);
            }
        }
        
        return result;	    
    }
    
    protected Object unwrap(PyObject po) {
        if (po instanceof PyNone) {
            return null;
        } else if (po.isNumberType()) {
            return po.asDouble();
        } else if (po.isSequenceType()) {
            Iterator<PyObject> i = po.asIterable().iterator();
            
            List<Object> list = new ArrayList<Object>();
            while (i.hasNext()) {
                list.add(unwrap((Object) i.next()));
            }
            
            return list.toArray();
        } else {
            return po;
        }
    }
}
<commit_msg>Initialize the jython library with the custom lib/jython/ path if we're running as a packaged app on Mac OSX.<commit_after>package com.metaweb.gridworks.expr;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyFunction;
import org.python.core.PyNone;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;

public class JythonEvaluable implements Evaluable {
	private static final String s_functionName = "___temp___";
	
    private static PythonInterpreter _engine; 
    static {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("mac os x")) {
            File libPath = new File("lib/jython");
            if (libPath.getAbsolutePath().contains("/Gridworks.app/Contents/Resources/")) {
                Properties props = new Properties();
                props.setProperty("python.path", libPath.getAbsolutePath());
                
                PythonInterpreter.initialize(System.getProperties(),props, new String[] { "" });
            }
        }
        _engine = new PythonInterpreter();
    }

    public JythonEvaluable(String s) {
        // indent and create a function out of the code
        String[] lines = s.split("\r\n|\r|\n");
        
        StringBuffer sb = new StringBuffer(1024);
        sb.append("def ");
        sb.append(s_functionName);
        sb.append("(value, cell, cells, row, rowIndex):");
        for (int i = 0; i < lines.length; i++) {
            sb.append("\n  ");
            sb.append(lines[i]);
        }

        _engine.exec(sb.toString());
    }
    
    public Object evaluate(Properties bindings) {
        try {
            // call the temporary PyFunction directly
            Object result = ((PyFunction)_engine.get(s_functionName)).__call__(
                new PyObject[] {
                    Py.java2py( bindings.get("value") ),
                    new JythonHasFieldsWrapper((HasFields) bindings.get("cell"), bindings),
                    new JythonHasFieldsWrapper((HasFields) bindings.get("cells"), bindings),
                    new JythonHasFieldsWrapper((HasFields) bindings.get("row"), bindings),
                    Py.java2py( bindings.get("rowIndex") )
                }
            );

            return unwrap(result);
        } catch (PyException e) {
            return new EvalError(e.getMessage());
        }
    }
    
    protected Object unwrap(Object result) {
        if (result != null) {
            if (result instanceof JythonObjectWrapper) {
                return ((JythonObjectWrapper) result)._obj;
            } else if (result instanceof JythonHasFieldsWrapper) {
                return ((JythonHasFieldsWrapper) result)._obj;
            } else if (result instanceof PyString) {
                return ((PyString) result).asString();
            } else if (result instanceof PyObject) {
                return unwrap((PyObject) result);
            }
        }
        
        return result;	    
    }
    
    protected Object unwrap(PyObject po) {
        if (po instanceof PyNone) {
            return null;
        } else if (po.isNumberType()) {
            return po.asDouble();
        } else if (po.isSequenceType()) {
            Iterator<PyObject> i = po.asIterable().iterator();
            
            List<Object> list = new ArrayList<Object>();
            while (i.hasNext()) {
                list.add(unwrap((Object) i.next()));
            }
            
            return list.toArray();
        } else {
            return po;
        }
    }
}

{'fraction_non_alphanumeric': 0.11420982735723771, 'fraction_numerical': 0.00206581083075107, 'mean_word_length': 1.948238364506307, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4891499', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1798, 'n_words': 424}