Skip to main content

TemporalObject

TemporalObject is a custom Python class for storing and managing object states in a temporal sequence. It uses a deque with a fixed maximum length to maintain a rolling buffer of states, providing an efficient way to track the history of changes. Each state can be indexed by an integer, a string ID, or a slice, and the class keeps track of the current state index within the buffer.

Installation

To use TemporalObject, ensure you have the required dependencies installed. You can install LimitedDict from the temporal.util module.

Usage

Class Definition

import uuid
from collections import deque
from typing import Any
from temporal.util import LimitedDict

class TemporalObject:
    """
    An custom object for storing and managing its states in a temporal sequence.

    This class utilizes a deque with a fixed maximum length (maxlen) to maintain
    a rolling buffer of states.

    Each state can be indexed by an integer, a string ID, or a slice. The class
    also tracks the current state index within the buffer.

    Parameters
    ----------
    temporal_depth : int
        The maximum number of states to store.

    Attributes
    ----------
    buffer : deque
        A deque with a maxlen.
    id_index : LimitedDict
        A dictionary with a limit on the number of items it can store.

    Methods
    -------
    add(id: str, state: dict) -> None:
        Appends a state to the buffer.
    update(object) -> None:
        Adds the object's state to the buffer.
    get(key: str, relative_index: int = 0) -> dict:
        Returns the value of the object with the given key and relative index.
    current() -> dict:
        Returns the current state.
    """

Initialization

Create an instance of TemporalObject by specifying the maximum number of states to store.

temporal_object = TemporalObject(temporal_depth=100)

Adding States

Use the add method to append a state to the buffer.

temporal_object.add(id="state1", state={"key": "value"})

Updating States

Use the update method to add the object's state to the buffer. If no temporal ID is provided, a new UUID will be generated.

state = {"key": "new_value"}
temporal_id = temporal_object.update(object_state=state)

Retrieving States

Retrieve a state using the get method by specifying the key and an optional relative index.

state = temporal_object.get(key="state1", relative_index=0)

Accessing the Current State

Get the current state using the current property.

current_state = temporal_object.current

Checking Buffer Length

Check the number of states in the buffer.

buffer_length = len(temporal_object)

Iterating Over States

Iterate over the states in the buffer.

for state in temporal_object:
    print(state)

Accessing States by Index

Access states by their index, temporal ID, or slice.

state_by_index = temporal_object[0]
state_by_id = temporal_object["state1"]

License

This project is licensed under the MIT License.

Metadata

Release files for temporalobject 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for temporalobject 0.2.0
File Size Uploaded
temporalobject-0.2.0.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for temporalobject 0.2.0
File Interpreter ABI Platform
temporalobject-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.0 kB

Release files / temporalobject-0.2.0.tar.gz

Download URL temporalobject-0.2.0.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2ca30a434f9ee50d45d939fff00063cba10c3edc67f845fd10fea422aeaebca6
BLAKE2b-256 checksum
How to use checksums
44ae6e23629a21fade173a3ed2c7effd4dfda9fa38a65bb630d443042b281e39
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.19

Release files / temporalobject-0.2.0-py3-none-any.whl

Download URL temporalobject-0.2.0-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6c84ee83336b10be8bbeda9544831e3304932103b2e0d10ecaaa2a1499afe2d6
BLAKE2b-256 checksum
How to use checksums
f45c5fcad290245b32883f97cb1f01df2bff90c95c651ed69cf05c124f74ea10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.19

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page