Skip to content

Commit

Permalink
- Minimize permissions
Browse files Browse the repository at this point in the history
- Calculate in threads
- Do not include python home in jar
- Move data_analysis.py to correct location in vfs
  • Loading branch information
timfel committed Jan 13, 2025
1 parent 667bf2e commit bb78651
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
6 changes: 5 additions & 1 deletion graalpy/graalpy-apache-arrow-guide/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<package>pandas</package>
<package>pyarrow</package>
</packages>
<pythonHome>
<includes></includes>
<excludes>.*</excludes>
</pythonHome>
</configuration>
<goals>
<goal>process-graalpy-resources</goal>
Expand All @@ -73,4 +77,4 @@
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.Float8Vector;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.Value;
import org.graalvm.python.embedding.utils.GraalPyResources;

Expand All @@ -17,7 +18,13 @@ public class Main {
private static final Integer PASSING_RATE_COLUMN_INDEX = 3;

public static Context initContext() {
return GraalPyResources.contextBuilder().allowAllAccess(true).build();
return GraalPyResources.contextBuilder()
.option("python.PythonHome", "")
.option("python.WarnExperimentalFeatures", "false")
.allowHostAccess(HostAccess.ALL)
.allowHostClassLookup(c -> true)
.allowNativeAccess(true)
.build();
}

public static void initDataAnalysisPyModule(Context context) {
Expand All @@ -33,17 +40,21 @@ public static void main(String[] args) throws IOException, InterruptedException
Float8Vector jsVector = new Float8Vector("jsPassingRate", allocator)
) {
initDataAnalysisPyModule(context);
Thread pyThread = new Thread(() -> DownloadUtils.downloadAndStore(PYTHON_URL, PASSING_RATE_COLUMN_INDEX, pyVector));
Thread jsThread = new Thread(() -> DownloadUtils.downloadAndStore(JAVASCRIPT_URL, PASSING_RATE_COLUMN_INDEX, jsVector));
Thread pyThread = new Thread(() -> {
DownloadUtils.downloadAndStore(PYTHON_URL, PASSING_RATE_COLUMN_INDEX, pyVector);
System.out.println("Python mean: " + dataAnalysisPyModule.calculateMean(pyVector));
System.out.println("Python median: " + dataAnalysisPyModule.calculateMedian(pyVector));
});
Thread jsThread = new Thread(() -> {
DownloadUtils.downloadAndStore(JAVASCRIPT_URL, PASSING_RATE_COLUMN_INDEX, jsVector);
System.out.println("JS mean: " + dataAnalysisPyModule.calculateMean(jsVector));
System.out.println("JS median: " + dataAnalysisPyModule.calculateMedian(jsVector));
});
pyThread.start();
jsThread.start();
pyThread.join();
jsThread.join();

System.out.println("Python mean: " + dataAnalysisPyModule.calculateMean(pyVector));
System.out.println("Python median: " + dataAnalysisPyModule.calculateMedian(pyVector));
System.out.println("JS mean: " + dataAnalysisPyModule.calculateMean(jsVector));
System.out.println("JS median: " + dataAnalysisPyModule.calculateMedian(jsVector));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def calculateMean(valueVector: Float8Vector) -> float:

def calculateMedian(valueVector: Float8Vector) -> float:
series = pd.Series(valueVector, dtype="float64[pyarrow]")
return series.median()
return series.median()

0 comments on commit bb78651

Please sign in to comment.