No project description provided
Project description
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
getArrayLength
that 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.java
class - 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.py
file to add your custom methods - Add unit tests for your Python methods in the
python/tests/
directory - Run the tests using
python -m pytest
from 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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file py4j-example-0.3.0.tar.gz
.
File metadata
- Download URL: py4j-example-0.3.0.tar.gz
- Upload date:
- Size: 126.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 567bfd9e49bf354d65fa08b368833495ff4feb474542fe69131e492a8c5eabe9 |
|
MD5 | 86a8607b376910a93bbfc50cb1fa1824 |
|
BLAKE2b-256 | 7a405b4af2a9093fbf15dc02b617944f081e15b69e7b63e847b71b82f6078c12 |
File details
Details for the file py4j_example-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: py4j_example-0.3.0-py3-none-any.whl
- Upload date:
- Size: 125.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15de30a1a87b5e2d5bc20c559927e95a0d38daa2ee14e1c6655afaab49282dc4 |
|
MD5 | 944ff74fd2d4e984746c2fccaedbe2b6 |
|
BLAKE2b-256 | bdb67d6ff6b7dc00d438340513ea7668f5b1aa62c35f4f847ead3c664cad5090 |