Protocols API Reference¶
This page provides API documentation for the pydapter.protocols
module.
Installation¶
Overview¶
The protocols module provides composable interfaces for specialized model behavior:
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Identifiable │ │ Temporal │ │ Embeddable │
│ (id: UUID) │ │ (timestamps) │ │ (content + │
│ │ │ │ │ embedding) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Invokable │ │ Cryptographical │ │ Auditable │
│ (execution) │ │ (hashing) │ │ (tracking) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
┌─────────────────┐
│ SoftDeletable │
│ (soft delete) │
└─────────────────┘
Quick Start¶
from pydapter.protocols import IdentifiableMixin, TemporalMixin
from pydantic import BaseModel
class User(BaseModel, IdentifiableMixin, TemporalMixin):
name: str
email: str
user = User(name="John", email="john@example.com")
user.update_timestamp() # Temporal behavior
print(user.id) # UUID from Identifiable
Event System¶
from pydapter.protocols.event import as_event
@as_event(event_type="api_call")
async def process_request(data: dict) -> dict:
return {"result": "processed", "input": data}
event = await process_request({"user_id": 123})
print(event.event_type) # "api_call"
Protocol Factory¶
from pydapter.protocols.factory import create_protocol_model_class
from pydapter.protocols.constants import IDENTIFIABLE, TEMPORAL
User = create_protocol_model_class(
"User",
IDENTIFIABLE,
TEMPORAL,
name=FieldTemplate(base_type=str),
email=FieldTemplate(base_type=str)
)
API Reference¶
Core Protocols¶
pydapter.protocols.identifiable
¶
pydapter.protocols.temporal
¶
Classes¶
TemporalMixin
¶
Source code in src/pydapter/protocols/temporal.py
pydapter.protocols.embeddable
¶
Classes¶
EmbeddableMixin
¶
Mixin class for embedding functionality.
Source code in src/pydapter/protocols/embeddable.py
pydapter.protocols.invokable
¶
Classes¶
Invokable
¶
Bases: Protocol
An object that can be invoked with a request
Source code in src/pydapter/protocols/invokable.py
InvokableMixin
¶
An executable can be invoked with a request
Source code in src/pydapter/protocols/invokable.py
Functions¶
pydapter.protocols.cryptographical
¶
pydapter.protocols.auditable
¶
pydapter.protocols.soft_deletable
¶
Event System¶
pydapter.protocols.event
¶
Classes¶
Event
¶
Bases: _BaseEvent
, IdentifiableMixin
, InvokableMixin
, TemporalMixin
, EmbeddableMixin
, CryptographicalMixin
Source code in src/pydapter/protocols/event.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
Functions¶
to_log(event_type=None, hash_content=False)
¶
Convert the event to a log entry.
Source code in src/pydapter/protocols/event.py
Functions¶
as_event(*, event_type=None, request_arg=None, embed_content=False, embed_function=None, adapt=False, adapter=None, content_parser=None, strict_content=False, **kw)
¶
- event_type, for example, "api_call", "message", "task", etc.
- request_arg, the name of the request argument in the function signature (will be passed to the event as part of content if content_function is not provided)
- embed_content, if True, the content will be embedded using the embed_function
- embed_function, a function that takes the content and returns an embedding
- adapt, if True, the event will be adapted to the specified adapter
- adapter, the adapter class to adapt the event to
- content_function, a function that takes the response_obj and returns the content (if not provided, the content {"request": request, "response": response}) where response is a dict representation of the response object **kw, additional keyword arguments to pass to the adapter
Source code in src/pydapter/protocols/event.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
|
Factory and Utilities¶
pydapter.protocols.factory
¶
Factory functions for creating protocol-compliant models.
Functions¶
combine_with_mixins(model_class, *protocols, name=None)
¶
Add protocol mixins to an existing model class.
This is useful when you already have a model with the required fields (e.g., from create_protocol_model) and want to add behavioral methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[BaseModel]
|
The model class to enhance with mixins |
required |
*protocols
|
Union[ProtocolType, str]
|
Protocol names whose mixins to add |
()
|
name
|
str
|
Optional name for the new class (defaults to original name) |
None
|
Returns:
Type | Description |
---|---|
type[BaseModel]
|
A new model class with the added behavioral mixins |
Example
from pydapter.fields import create_protocol_model
from pydapter.protocols import combine_with_mixins, IDENTIFIABLE, TEMPORAL
# First create structure
UserStructure = create_protocol_model(
"UserStructure",
IDENTIFIABLE,
TEMPORAL,
username=FieldTemplate(base_type=str)
)
# Then add behaviors
User = combine_with_mixins(UserStructure, IDENTIFIABLE, TEMPORAL)
Source code in src/pydapter/protocols/factory.py
create_protocol_model_class(name, *protocols, base_model=BaseModel, **namespace)
¶
Create a model class with both structural fields and behavioral methods.
This is a convenience function that combines create_protocol_model (for fields) with the appropriate protocol mixins (for behavior) to create a fully functional protocol-compliant model class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name for the generated model class |
required |
*protocols
|
Union[ProtocolType, str]
|
Protocol names to implement (e.g., IDENTIFIABLE, TEMPORAL) |
()
|
base_model
|
type[BaseModel]
|
Base model class to inherit from (default: BaseModel) |
BaseModel
|
**namespace
|
Any
|
Additional class attributes/methods to include |
{}
|
Returns:
Type | Description |
---|---|
type[BaseModel]
|
A new model class with both protocol fields and behaviors |
Example
from pydapter.protocols import create_protocol_model_class, IDENTIFIABLE, TEMPORAL
from pydapter.fields import FieldTemplate
# Create a model with both fields and behaviors
User = create_protocol_model_class(
"User",
IDENTIFIABLE,
TEMPORAL,
username=FieldTemplate(base_type=str),
email=FieldTemplate(base_type=str)
)
# Now you can use it
user = User(username="john", email="john@example.com")
user.update_timestamp() # Method from TemporalMixin
Source code in src/pydapter/protocols/factory.py
pydapter.protocols.registry
¶
Classes¶
Functions¶
get_mixin_registry()
¶
Get the registry of mixin classes for protocols.
Returns:
Type | Description |
---|---|
dict[str, type]
|
A dictionary mapping protocol names to their corresponding mixin classes. |
register_mixin(protocol_name, mixin_class)
¶
Register a new mixin class for a protocol.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protocol_name
|
str
|
The name of the protocol (e.g., "identifiable"). |
required |
mixin_class
|
type
|
The mixin class to register. |
required |
Source code in src/pydapter/protocols/registry.py
pydapter.protocols.constants
¶
Protocol constants for type-safe protocol selection.
pydapter.protocols.types
¶
Basic types for protocols - maintained for backwards compatibility.
Classes¶
Log
¶
Bases: BaseModel
Base Log model
Source code in src/pydapter/protocols/types.py
pydapter.protocols.utils
¶
Functions¶
as_async_fn(fn)
cached
¶
force_async(fn)
¶
force a function to be async.
Source code in src/pydapter/protocols/utils.py
get_bins(input_, /, upper)
¶
Organizes indices of items into bins based on a cumulative upper limit length.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_
|
list[str]
|
The list of strings to be binned. |
required |
upper
|
int
|
The cumulative length upper limit for each bin. |
required |
Returns:
Type | Description |
---|---|
list[Bin]
|
list[list[int]]: A list of bins, each bin is a list of indices from the input list. |
Source code in src/pydapter/protocols/utils.py
import_module(package_name, module_name=None, import_name=None)
¶
Import a module by its path.
Source code in src/pydapter/protocols/utils.py
is_coroutine_function(fn)
cached
¶
sha256_of_dict(obj)
¶
Deterministic SHA-256 of an arbitrary mapping.
Source code in src/pydapter/protocols/utils.py
validate_model_to_dict(v)
¶
Serialize a Pydantic model to a dictionary. kwargs are passed to model_dump.