Skip to main content

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
  • Python (Python 2 & 3 will work) - This project was tested with Python 3.10.11
  • Maven - 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:

  1. Download the latest Py4J jar in your local machine. You can find the latest version of the jar here
  2. Clone this repository in your local machine
  3. Open a terminal or command prompt and navigate to the project's root directory
  4. Build the project using mvn clean package or mvn clean package -DskipTests
  5. Start the Py4J GatewayServer in the foreground by executing java -cp "/path/to/py4j.jar:target/Py4J-1.0-SNAPSHOT.jar" py4j.GatewayServer
  6. Optional: If you don't want to start the gateway server process in foreground. You can also add the py4j-jar as jarpath and your application's custom-jar as classpath here

Usage

  • From your terminal, open python

    python

  • Run the below code which will let you use the underlying Java subtract method in python

from python.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 python.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 existing Application.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

py4j-example-0.1.0.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

py4j_example-0.1.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file py4j-example-0.1.0.tar.gz.

File metadata

  • Download URL: py4j-example-0.1.0.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for py4j-example-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1e8aee0e39459f28f8f3c2e229d522461bd3aa2094a736557a9f6ac7c5bbe457
MD5 8cdc7eb7fdd311f98145cfa6e3a137aa
BLAKE2b-256 3a413a183cb1cb84df715a97ca8a76a17287d50ac407f5938417d94804f23b93

See more details on using hashes here.

File details

Details for the file py4j_example-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for py4j_example-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ceddb41df0b62660d97cb239e0b9da3c8e090ee5ca483deead2b32ce3b1c07f
MD5 c67d8bec20f14f57b29632e3abe8be98
BLAKE2b-256 d9cc12accb0d67d198245488314d218a8d28aeb086fa2768c838acbc91c1967f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page