No project description provided
Project description
OaaS-SDK2
This library helps you develop a runtime that can be run in a Object as a Service (OaaS) serverless. For more information on the OaaS model, visit https://github.com/hpcclab/OaaS.
Table of Contents
Installation
To install oaas-sdk2-py, you can use pip:
pip install oaas-sdk2-py
Or, if you are using uv:
# For adding/installing packages with uv, the command is 'uv pip install'
uv add oaas-sdk2-py
Features
- Define Classes and Objects: Easily define classes and create persistent objects.
- Remote Procedure Calls (RPC): Invoke methods on objects remotely.
- Data Persistence: Object data is persisted and can be retrieved.
- Asynchronous Support: Built with
async/awaitfor non-blocking operations. - Mocking Framework: Includes a mocking utility for testing your OaaS applications without needing a live environment.
- Typed Interactions: Leverages Pydantic for data validation and serialization.
- Rust-Powered Core: High-performance core components written in Rust for speed and efficiency.
Examples
Note (Sync/Async API): The following examples demonstrate synchronous operations. To use async/await features, initialize Oparaca with oaas = Oparaca(async_mode=True). You would then use methods like get_data_async, set_data_async, commit_async, and define your object methods with async def.
Note (Remote object): When an object is created without the local=True flag (the default behavior for mock_oaas.create_object unless specified, and standard for objects managed by a live OaaS environment), method calls decorated with @your_cls_meta.func() on that object instance will result in a Remote Procedure Call (RPC) to the OaaS platform.
Basic Usage
First, define your class and its methods using the OaaS SDK decorators.
# In your_module.py
from pydantic import BaseModel
from oaas_sdk2_py import Oparaca, BaseObject
# Initialize Oparaca (default is synchronous mode)
oaas = Oparaca()
# For asynchronous operations, use: oaas = Oparaca(async_mode=True)
# Define a class metadata
sample_cls_meta = oaas.new_cls("MySampleClass")
class Msg(BaseModel):
content: str
class Result(BaseModel):
status: str
message: str
@sample_cls_meta
class SampleObj(BaseObject):
def get_intro(self) -> str:
raw = self.get_data(0) # Key 0 for intro data
return raw.decode("utf-8") if raw is not None else "No intro set."
def set_intro(self, data: str):
self.set_data(0, data.encode("utf-8"))
@sample_cls_meta.func()
def greet(self) -> str:
intro = self.get_intro()
return f"Hello from SampleObj! Intro: {intro}"
@sample_cls_meta.func("process_message")
def process(self, msg: Msg) -> Result:
# Process the message
processed_message = f"Processed: {msg.content}"
self.set_intro(processed_message) # Example of updating object state
self.commit()
return Result(status="success", message=processed_message)
Interacting with Objects
from oaas_sdk2_py import Oparaca
# Assuming your_module defines oaas, sample_cls_meta, SampleObj, Msg
from your_module import oaas, sample_cls_meta, SampleObj, Msg
def main():
# For local testing, you can use the mock
mock_oaas = oaas.mock()
# Create an object
# obj_id can be any unique identifier, e.g., 1
my_object: SampleObj = mock_oaas.create_object(sample_cls_meta, 1)
# Set initial data
my_object.set_intro("My first OaaS object!")
my_object.commit() # Persist changes (synchronous)
# Invoke a simple RPC method
greeting = my_object.greet()
print(greeting)
# Invoke an RPC method with input and output
response = my_object.process(Msg(content="Important data"))
print(f"Processing Response: {response.status} - {response.message}")
# Verify data was updated
updated_greeting = my_object.greet()
print(updated_greeting)
# Load an existing object
loaded_object: SampleObj = mock_oaas.load_object(sample_cls_meta, 1)
intro = loaded_object.get_intro()
print(f"Loaded object intro: {intro}")
if __name__ == "__main__":
main()
Using the Mock Framework for Tests
The SDK provides a oaas.mock() utility that allows you to test your object logic without connecting to a live OaaS environment. This is particularly useful for unit and integration tests.
# In your tests/test_my_sample_class.py
import unittest
# import asyncio # Not needed for synchronous example
from oaas_sdk2_py import Oparaca
# Assuming your_module defines oaas, sample_cls_meta, SampleObj, Msg
from your_module import oaas, sample_cls_meta, SampleObj, Msg
class TestMySampleClass(unittest.TestCase): # Changed from IsolatedAsyncioTestCase
def test_greeting_with_mock(self): # Changed from async def
mock_oaas = oaas.mock()
obj: SampleObj = mock_oaas.create_object(sample_cls_meta, 1)
obj.set_intro("Mocked Intro")
obj.commit() # In mock, this updates the in-memory store (synchronous)
result = obj.greet()
self.assertEqual(result, "Hello from SampleObj! Intro: Mocked Intro")
def test_process_message_with_mock(self): # Changed from async def
mock_oaas = oaas.mock()
obj: SampleObj = mock_oaas.create_object(sample_cls_meta, 2)
response = obj.process(Msg(content="Test Message"))
self.assertEqual(response.status, "success")
self.assertEqual(response.message, "Processed: Test Message")
# Verify state change
intro = obj.get_intro()
self.assertEqual(intro, "Processed: Test Message")
Refer to tests/test_mock.py and tests/sample_cls.py for more detailed examples of synchronous and asynchronous object definitions and mock usage.
Reference
Oparaca
The main entry point for interacting with the OaaS SDK.
Initialization:
Oparaca(async_mode: bool = False)- Initializes the Oparaca client.
async_mode: IfTrue, enables asynchronous operations forBaseObjectmethods (e.g.,get_data_async,commit_async) andMockOaasmethods (e.g.,create_object_async). Defaults toFalse(synchronous mode).
Methods:
new_cls(name: str) -> ClsMetadata- Creates metadata for a new class that will be registered with OaaS.
name: The name of the class.- Returns a
ClsMetadataobject, which is used as a decorator for your class and its methods.
mock() -> MockOaas- Returns a
MockOaasinstance for local testing and development without a live OaaS environment.
- Returns a
ClsMetadata
Returned by Oparaca.new_cls(). Used as a decorator to define OaaS-compatible classes and their remotely callable methods.
Usage as Class Decorator:
oaas = Oparaca()
my_class_meta = oaas.new_cls("MyClassName")
@my_class_meta
class MyObject(BaseObject):
# ... object implementation ...
Methods (used as decorators for methods within a BaseObject subclass):
-
func(name: Optional[str] = None)- Decorator to mark a method within a
BaseObjectsubclass as a remotely callable function (RPC). name: Optional. If provided, this name will be used to register the function in OaaS. IfNone, the method's original name is used.- RPC Behavior: When a method decorated with
funcis called on an instance ofBaseObjectthat is not local (i.e.,obj.remoteisTrue, which is the default unlesslocal=Trueis specified during object creation withoaas.mock().create_object()or if the object is managed by a live OaaS environment), the SDK will automatically handle the serialization of inputs, perform an RPC to the OaaS platform, and deserialize the response. For local objects (obj.remoteisFalse), the method is executed directly within the current process.
@my_class_meta class MyObject(BaseObject): @my_class_meta.func() def my_method(self, arg: str) -> str: return f"Processed: {arg}" @my_class_meta.func("customFunctionName") def another_method(self): pass
- Decorator to mark a method within a
BaseObject
The base class for all objects managed by OaaS. Your custom classes should inherit from BaseObject.
Instances of BaseObject can be either "local" or "remote". Remote objects (where obj.remote is True) will have their methods decorated with @your_cls_meta.func() executed via RPC to the OaaS platform. Local objects execute these methods directly.
Synchronous API (when Oparaca is initialized with async_mode=False)
get_data(key: int) -> Optional[bytes]- Retrieves data associated with the given key for the current object instance.
key: An integer key to identify the data.- Returns the data as
bytesif found, otherwiseNone.
set_data(key: int, value: bytes)- Sets data for the given key for the current object instance. This change is local until
commit()is called. key: An integer key.value: The data to store, asbytes.
- Sets data for the given key for the current object instance. This change is local until
commit()- Persists all local changes (made via
set_data) to the OaaS storage. In the mock environment, this updates the in-memory store.
- Persists all local changes (made via
Asynchronous API (when Oparaca is initialized with async_mode=True)
async get_data_async(key: int) -> Optional[bytes]- Asynchronously retrieves data associated with the given key.
async set_data_async(key: int, value: bytes)- Asynchronously sets data for the given key. This change is local until
commit_async()is called.
- Asynchronously sets data for the given key. This change is local until
async commit_async()- Asynchronously persists all local changes to the OaaS storage.
MockOaas
Provides a mock implementation of the OaaS environment for local testing. Obtained by calling oaas.mock().
Synchronous API (when Oparaca is initialized with async_mode=False)
create_object(cls_meta: ClsMetadata, obj_id: int) -> T- Creates an instance of an object in the mock environment.
cls_meta: TheClsMetadataof the class to instantiate.obj_id: A unique integer ID for the new object.- Returns an instance of the object (type
T, whereTis a subclass ofBaseObject).
load_object(cls_meta: ClsMetadata, obj_id: int) -> T- Loads an existing object instance from the mock environment.
cls_meta: TheClsMetadataof the class.obj_id: The ID of the object to load.- Returns an instance of the object. Raises an error if the object does not exist.
Asynchronous API (when Oparaca is initialized with async_mode=True)
async create_object_async(cls_meta: ClsMetadata, obj_id: int) -> T- Asynchronously creates an instance of an object in the mock environment.
async load_object_async(cls_meta: ClsMetadata, obj_id: int) -> T- Asynchronously loads an existing object instance from the mock environment.
Run on OaaS
Prerequisites
- cargo (install via rust)
- oprc-cli
cargo install --git https://github.com/pawissanutt/oaas-rs.git oprc-cli - OaaS Platform (Oparaca)
- Kubernetes Cluster (e.g., k3d with Docker runtime)
TODO
Build the project
You don't need to follow this guide unless you want to build the Python package on your own.
Prerequisites
Build
uv sync
uv build
Project details
Release history Release notifications | RSS feed
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 oaas_sdk2_py-0.2.1.tar.gz.
File metadata
- Download URL: oaas_sdk2_py-0.2.1.tar.gz
- Upload date:
- Size: 28.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b0ad2d1273d091ec8503a93be7780df51b4e8898cefc08498ab2d56729ded31e
|
|
| MD5 |
00783609e25856258ea2cfb88e265752
|
|
| BLAKE2b-256 |
61061beaa9554d0e96e75c8e2b89a3545b1f9b7f1c18fd342a64e827366a33c0
|
File details
Details for the file oaas_sdk2_py-0.2.1-py3-none-any.whl.
File metadata
- Download URL: oaas_sdk2_py-0.2.1-py3-none-any.whl
- Upload date:
- Size: 30.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb1b230e9011f82bf6c70bf45b0db81e544a6c83e81916b9c3f3e4de21929a41
|
|
| MD5 |
a58be260fa996c2e16247a86026f0578
|
|
| BLAKE2b-256 |
197a919ebb56415f038ac4f90c6550eb8dd129714f5fbd69f72d46e751d007d8
|