Skip to main content

A lightweight solution to execute Python dependencies in an isolated fashion.

Project description

PyPods

A lightweight solution to execute Python dependencies in an isolated fashion.

This documentation will follow the classic philosophy of A picture is worth a thousand words

Problem

problem

Solution

solution

PyPod 🔎

solution-magnified

Terminology

  1. pods directory stores all the pods that will be used in the current project.
  2. A pod is a container that contains its own Python interpreter, dependencies, and a pod.py file that exposes specific functions defined by the user.
  3. A pod client is a file that calls the specific functions defined by the user.
  4. A pod protocol is a way to exchange data between the pod and pod client. PyPods uses binary json or bson.

How to use PyPods?

  1. Install pypods package via pip install pypods
  2. From the project's root directory, create a file (say client.py) and paste the following code.
# client.py will communicate with the hello_world_pod pod
from pypods.pods import PodLoader

# name of the pod, and namespace to inject pod's functions.
pl = PodLoader("hello_world_pod", globals())
pl.load_pod()   # Load pod's namespace
pl.unload_pod() # Unload pod's namespace

if hello_world_pod does not exist then PodLoader will create a hello_world_pod pod in the pods/hello_world_pod/ directory.

  1. Navigate to pods/hello_world_pod/ directory and observe the file structure. This is your pod.
hello_world_pod/
│
├── venv/
│   ├── bin/          (or Scripts/ on Windows)   ├── include/
│   ├── lib/
│   └── pyvenv.cfg
│
├── pod.py
├── requirements.txt
  1. You can define functions inside a placeholder defined in the pod.py template file. Please don't change anything else in this file!
# Template pod.py file inside the hello_world_pod pod.
"""
Write your module's functions in this area.
"""
def foo(x, y):
    return x + y

# Don't change anything here!
if __name__ == "__main__":
    from pypods.pods import PodListener
    pl = PodListener()  # PodListener will send output back to the pod client.
    msg = (
        pl.read_stdin()
    )  # Pod client writes function name and parameters to pod's stdin.
    if msg:
        # Unpack stdin to get function data
        function_name, args, kwargs = msg["name"], msg["args"], msg["kwargs"]
        try:
            # Check if function exists in pod module's namespace.
            # If yes, execute the function and send output back to the pod client.
            # If no, send error back to the pod client.
            if function_name in globals():
                function_output = globals()[function_name](*args, **kwargs)
                pl.write_stdout(function_output)
            else:
                pl.write_stderr(f"Function {function_name} does not exist in pod")
        except Exception as e:
            # Any error that occurs while calling the function is sent back to pod client.
            pl.write_stderr(str(e))
  1. Go back to client.py and add the foo function.
# client.py will communicate with the hello_world_pod pod
from pypods.pods import PodLoader

# name of the pod, and namespace to inject pod's functions.
pl = PodLoader("hello_world_pod", globals())
pl.load_pod()   # Load pod's namespace (This will now load the foo function).
foo_output = foo(1, 2) # Expected output = 1 + 2 = 3.
pl.unload_pod() # Unload pod's namespace

This demo shows how PyPod works. You ran a pod function foo without importing it into the client.py file! See example/ directory for a project example.

Author

Rohan Deshpande, PyPods 2024. Inspired by the idea of Babashka pods.

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

pypods-0.1.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pypods-0.1.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file pypods-0.1.1.tar.gz.

File metadata

  • Download URL: pypods-0.1.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.6

File hashes

Hashes for pypods-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0364fb19ee9c892fa5ba3718c10a423c1f11a1fa317099c7f50100eda88cc619
MD5 4215c66ab0a65be1b231647f57fe5da2
BLAKE2b-256 2a535d5c5225f0757cca2b82098124f3ea6be625d30cc828feea8acde2d90bc7

See more details on using hashes here.

File details

Details for the file pypods-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pypods-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.6

File hashes

Hashes for pypods-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d98887ced7a39d9cf1e89dc015b37f8c03841afeb3a56350dd9353bd9260fd66
MD5 1888f8b0810fc023fd72978e2c1ce7a1
BLAKE2b-256 79eb038fb0b168617bafb570a34ade2462c9564859ae2b93e0e19e5de1309d50

See more details on using hashes here.

Supported by

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