Proxy helpers for serializing and deserializing objects
Project description
Proxy helpers
% start main % start synopsis
This module provides proxy helpers for serializing and deserializing objects across process boundaries or network connections. It includes:
- Transparent proxy objects that can be serialized and reconstructed
- Object registration and name mapping for proxied objects
- Data-carrying proxies (DProxy) for including object state
- Integration with CBOR, msgpack, and other serialization formats
% end synopsis
Usage
Basic proxy registration
from moat.lib.proxy import name2obj, obj2name, Proxy
# Register a class for proxying
class MyService:
def do_something(self):
return "result"
# Register the class with a name
name2obj("myapp.MyService", MyService)
# Get the proxy name for an object
service = MyService()
proxy_name = obj2name(service) # Returns "myapp.MyService"
Using proxies
from moat.lib.proxy import as_proxy, get_proxy
# Create a proxy reference to an object
obj = MyService()
proxy = as_proxy(obj) # Returns a Proxy object
# Later, retrieve the original object
original = get_proxy(proxy) # Returns the MyService instance
Data-carrying proxies (DProxy)
from moat.lib.proxy import DProxy
class MyData(DProxy):
"""A proxy that includes object state"""
def __init__(self, value):
self.value = value
def __reduce__(self):
# Define how to serialize the object
return (self.__class__, (self.value,))
# The object can be serialized with its state
data = MyData(42)
# When serialized and deserialized, the value is preserved
Working with serialization
from moat.lib.proxy import wrap_obj, unwrap_obj
# Serialize an object for transmission
class MyObject:
pass
name2obj("myapp.MyObject", MyObject)
obj = MyObject()
# Wrap for serialization
wrapped = wrap_obj(obj) # Creates a serializable representation
# Later, unwrap to reconstruct
reconstructed = unwrap_obj(wrapped) # Returns a MyObject instance
Integration with Serialization Formats
This module integrates with MoaT's serialization libraries:
- CBOR:
moat.lib.codec.cbor - Msgpack:
moat.lib.codec.msgpack - YAML: Supports proxy references in configuration files
% end main
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 moat_lib_proxy-0.1.2.tar.gz.
File metadata
- Download URL: moat_lib_proxy-0.1.2.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09fab7a532e0dece32636f772a6f1ef2193f5af5e6d249edb527ae13c88cc64b
|
|
| MD5 |
12b9e34d332f2985bd109eadfbe14f83
|
|
| BLAKE2b-256 |
7a96a2d6a5dc17e3b77c1fc03cb91ec46dff321b487e1ca1778c59730963b21b
|
File details
Details for the file moat_lib_proxy-0.1.2-py3-none-any.whl.
File metadata
- Download URL: moat_lib_proxy-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dad20d3b7e6f40cc49e3e758aa97e11ae9108c22c772221196e978dd9384efe4
|
|
| MD5 |
80894ea51ff6c2341a2e4556eab9b311
|
|
| BLAKE2b-256 |
30cf8e1e8e66acad535a847b2b697798683903c172e0fc3a5a0236dbec8b333d
|