
prerna.ds.py.PyUtils Maven / Gradle / Ivy
The newest version!
package prerna.ds.py;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang.SystemUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import prerna.auth.User;
import prerna.om.Insight;
import prerna.util.Constants;
import prerna.util.Settings;
import prerna.util.Utility;
public class PyUtils {
private static final Logger classLogger = LogManager.getLogger(PyUtils.class.getName());
public static final String PY_COMMAND_SEPARATOR = ";";
private static Boolean pyEnabled = null;
private static PyUtils instance;
public Map userTupleMap = new Hashtable<>();
public Map userProcessMap = new Hashtable<>();
private PyUtils() {
}
public static PyUtils getInstance() throws IllegalArgumentException {
if(instance == null) {
setPyEnabled();
if(pyEnabled) {
instance = new PyUtils();
}
}
return instance;
}
/**
* Method to set internally for this class if python is enabled
*/
private static void setPyEnabled() {
if(pyEnabled == null) {
pyEnabled = false;
String usePythonStr = Utility.getDIHelperProperty(Constants.USE_PYTHON);
if(usePythonStr != null) {
pyEnabled = Boolean.parseBoolean(usePythonStr);
}
}
}
/**
* Getter if python is enabled
* @return
*/
public static boolean pyEnabled() {
if(pyEnabled == null) {
pyEnabled = false;
String usePythonStr = Utility.getDIHelperProperty(Constants.USE_PYTHON);
if(usePythonStr != null) {
pyEnabled = Boolean.parseBoolean(usePythonStr);
}
}
return pyEnabled;
}
// this is good for python dictionaries but also for making sure we can easily construct
// the logs into model inference python list, since everything is python at this point.
public static String constructPyDictFromMap(Map theMap) {
StringBuilder theDict = new StringBuilder("{");
boolean isFirstElement = true;
for (Entry entry : theMap.entrySet()) {
if (!isFirstElement) {
theDict.append(",");
} else {
isFirstElement = false;
}
theDict.append(determineStringType(entry.getKey())).append(":").append(determineStringType(entry.getValue()));
//theDict.append(determineStringType(entry.getKey())).append(":").append(determineStringType(entry.getValue())).append(",");
}
theDict.append("}");
return theDict.toString();
}
/* This is basically a utility method that attempts to generate the python code (string) for a java object.
* It currently only does base types.
* Potentially move it in the future but just keeping it here for now
*/
@SuppressWarnings("unchecked")
public static String determineStringType(Object obj) {
if (obj instanceof Integer || obj instanceof Double || obj instanceof Long) {
return String.valueOf(obj);
} else if (obj instanceof Map) {
return constructPyDictFromMap((Map) obj);
} else if (obj instanceof ArrayList || obj instanceof Object[] || obj instanceof List) {
StringBuilder theList = new StringBuilder("[");
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy