Py4J - A walkthrough
This project is a simple implementation of Py4J, a Python library that enables Python programs to dynamically access Java objects and methods. The goal of this project is to provide users with a guardrail to implement Py4J in their workflow.
Getting Started
To get started with Py4J, you will need to have the below in your machine.
- Java (Java 11 or above) - This project was tested with Java 11
- Add JAVA_HOME to your environment variables
- Python (Python 3.x) - This project was tested with Python 3.10.11
- Optional - Maven (only if you want to build from source) - This project's build was tested with Maven 3.9.4 You can find the installation instructions on their respective official websites.
Once you have Java and Python installed, you can proceed with the following steps:
- Install the package using pip
pip install py4j-example
Usage
-
From your terminal, open python
python -
Run the below code which will let you use the underlying Java subtract method in python
from py4j_example.app import ApplicationWrapper
wrapper = ApplicationWrapper()
result = wrapper.subtract(2, 1)
print(result) # will output 1
- In addition to the subtract method, you can access other Java objects and methods (native and custom) from Python.
For example, to call a Java method named
getArrayLengththat takes a list of integers as a parameter, you can do the following:
from py4j_example.app import ApplicationWrapper
wrapper = ApplicationWrapper()
result = wrapper.getArrayLength([1, 2, 3])
print(result) # will output 3
Note: Python doesn't have an equivalent data type for Java's int[], so I've constructed a Java array from Python using Py4J. Refer to this code.
Extending the application
You can extend the application by adding your own Java classes and methods.
- Create a new Java class in the
src/main/java/org/example/directory or modify the existingApplication.javaclass - Add your custom methods to the class
- Add unit test cases for the Java methods in the
src/test/java/org/example/directory - Build the project using
mvn clean package - Modify the
pyhton/app.pyfile to add your custom methods - Add unit tests for your Python methods in the
python/tests/directory - Run the tests using
python -m pytestfrom the project root directory
Contributing
Contributions to this project are welcome. If you have any suggestions, bug reports, or feature requests, please open an issue on the GitHub repository.
Metadata
Release files for py4j-example 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py4j-example-0.3.1.tar.gz | 126.7 kB | Without provenance | Details for py4j-example-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py4j_example-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for py4j_example-0.3.1-py3-none-any.whl |
Total release size: 251.7 kB
Release files / py4j-example-0.3.1.tar.gz
| Download URL | py4j-example-0.3.1.tar.gz |
|---|---|
| Size | 126.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
42358cf872970c317130993a87c9382e8b287dcd84f0f0cd88c9cf6c0a4eae47
|
|
BLAKE2b-256 checksum How to use checksums |
ad6e705efb0323b0ceb2cece4532746d9b2dc5f1c82e6ffc7bc6dff6a4d57a14
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.0
|
Release files / py4j_example-0.3.1-py3-none-any.whl
| Download URL | py4j_example-0.3.1-py3-none-any.whl |
|---|---|
| Size | 125.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7b5bb4065838b29db8f254fdaa7d225830b07f4f21fff08e24806965d0f263ea
|
|
BLAKE2b-256 checksum How to use checksums |
9001243f274eba85f680141e2a92a0609eaf00152935f9faa411ee23a89c7f79
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.0
|
Security for py4j-example 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.