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
Solution
PyPod 🔎
Terminology
podsdirectory stores all the pods that will be used in the current project.- A
podis a container that contains its own Python interpreter, dependencies, and apod.pyfile that exposes specific functions defined by the user. - A
pod clientis a file that calls the specific functions defined by the user. - A
pod protocolis a way to exchange data between the pod and pod client. PyPods uses binary json or bson.
How to use PyPods?
- Install pypods package via
pip install pypods - From the project's
rootdirectory, create a file (sayclient.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.
- 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
- You can define functions inside a placeholder defined in the
pod.pytemplate 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))
- Go back to
client.pyand add thefoofunction.
# 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
730a0136f11ecdb2399ee95d3b496a659f5d038d221a17e537d422640efa51b9
|
|
| MD5 |
8596afe6f51a9a400bc3a21d73a522b8
|
|
| BLAKE2b-256 |
34168f0e28add89e1f72ccaec02c29570a867e2fdf8b0721bd55e50b89ed2677
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
146c6735a44a550868a3a252badabd3f680d649cafeeb4621f1c02000fe94add
|
|
| MD5 |
2a84b3a047d961375f83488b8828fb72
|
|
| BLAKE2b-256 |
b7bb769deb0f0a77b62d2bdeeac615133b46614782e79b7f58501b996ed9359c
|