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 PyPI version

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.3.tar.gz (7.2 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.3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pypods-0.1.3.tar.gz
  • Upload date:
  • Size: 7.2 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.3.tar.gz
Algorithm Hash digest
SHA256 730a0136f11ecdb2399ee95d3b496a659f5d038d221a17e537d422640efa51b9
MD5 8596afe6f51a9a400bc3a21d73a522b8
BLAKE2b-256 34168f0e28add89e1f72ccaec02c29570a867e2fdf8b0721bd55e50b89ed2677

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypods-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.3 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 146c6735a44a550868a3a252badabd3f680d649cafeeb4621f1c02000fe94add
MD5 2a84b3a047d961375f83488b8828fb72
BLAKE2b-256 b7bb769deb0f0a77b62d2bdeeac615133b46614782e79b7f58501b996ed9359c

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