# 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
```python
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.
```python
temporal_object = TemporalObject(temporal_depth=100)
```
### Adding States
Use the `add` method to append a state to the buffer.
```python
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.
```python
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.
```python
state = temporal_object.get(key="state1", relative_index=0)
```
### Accessing the Current State
Get the current state using the `current` property.
```python
current_state = temporal_object.current
```
### Checking Buffer Length
Check the number of states in the buffer.
```python
buffer_length = len(temporal_object)
```
### Iterating Over States
Iterate over the states in the buffer.
```python
for state in temporal_object:
print(state)
```
### Accessing States by Index
Access states by their index, temporal ID, or slice.
```python
state_by_index = temporal_object[0]
state_by_id = temporal_object["state1"]
```
## License
This project is licensed under the MIT License.
Raw data
{
"_id": null,
"home_page": "https://pypi.org/project/temporalobject/",
"name": "temporalobject",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": null,
"author": "Chris Mangum",
"author_email": "csmangum@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/44/ae/6e23629a21fade173a3ed2c7effd4dfda9fa38a65bb630d443042b281e39/temporalobject-0.2.0.tar.gz",
"platform": null,
"description": "# TemporalObject\n\n`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.\n\n## Installation\n\nTo use `TemporalObject`, ensure you have the required dependencies installed. You can install `LimitedDict` from the `temporal.util` module.\n\n## Usage\n\n### Class Definition\n\n```python\nimport uuid\nfrom collections import deque\nfrom typing import Any\nfrom temporal.util import LimitedDict\n\nclass TemporalObject:\n \"\"\"\n An custom object for storing and managing its states in a temporal sequence.\n\n This class utilizes a deque with a fixed maximum length (maxlen) to maintain\n a rolling buffer of states.\n\n Each state can be indexed by an integer, a string ID, or a slice. The class\n also tracks the current state index within the buffer.\n\n Parameters\n ----------\n temporal_depth : int\n The maximum number of states to store.\n\n Attributes\n ----------\n buffer : deque\n A deque with a maxlen.\n id_index : LimitedDict\n A dictionary with a limit on the number of items it can store.\n\n Methods\n -------\n add(id: str, state: dict) -> None:\n Appends a state to the buffer.\n update(object) -> None:\n Adds the object's state to the buffer.\n get(key: str, relative_index: int = 0) -> dict:\n Returns the value of the object with the given key and relative index.\n current() -> dict:\n Returns the current state.\n \"\"\"\n```\n\n### Initialization\n\nCreate an instance of `TemporalObject` by specifying the maximum number of states to store.\n\n```python\ntemporal_object = TemporalObject(temporal_depth=100)\n```\n\n### Adding States\n\nUse the `add` method to append a state to the buffer.\n\n```python\ntemporal_object.add(id=\"state1\", state={\"key\": \"value\"})\n```\n\n### Updating States\n\nUse the `update` method to add the object's state to the buffer. If no temporal ID is provided, a new UUID will be generated.\n\n```python\nstate = {\"key\": \"new_value\"}\ntemporal_id = temporal_object.update(object_state=state)\n```\n\n### Retrieving States\n\nRetrieve a state using the `get` method by specifying the key and an optional relative index.\n\n```python\nstate = temporal_object.get(key=\"state1\", relative_index=0)\n```\n\n### Accessing the Current State\n\nGet the current state using the `current` property.\n\n```python\ncurrent_state = temporal_object.current\n```\n\n### Checking Buffer Length\n\nCheck the number of states in the buffer.\n\n```python\nbuffer_length = len(temporal_object)\n```\n\n### Iterating Over States\n\nIterate over the states in the buffer.\n\n```python\nfor state in temporal_object:\n print(state)\n```\n\n### Accessing States by Index\n\nAccess states by their index, temporal ID, or slice.\n\n```python\nstate_by_index = temporal_object[0]\nstate_by_id = temporal_object[\"state1\"]\n```\n\n## License\n\nThis project is licensed under the MIT License.\n",
"bugtrack_url": null,
"license": null,
"summary": "A custom python object for storing and managing states in a temporal sequence.",
"version": "0.2.0",
"project_urls": {
"Homepage": "https://pypi.org/project/temporalobject/"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f45c5fcad290245b32883f97cb1f01df2bff90c95c651ed69cf05c124f74ea10",
"md5": "5a39ca52b1a3412bd6c6cbff8334252a",
"sha256": "6c84ee83336b10be8bbeda9544831e3304932103b2e0d10ecaaa2a1499afe2d6"
},
"downloads": -1,
"filename": "temporalobject-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5a39ca52b1a3412bd6c6cbff8334252a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 10364,
"upload_time": "2024-07-22T04:34:38",
"upload_time_iso_8601": "2024-07-22T04:34:38.521817Z",
"url": "https://files.pythonhosted.org/packages/f4/5c/5fcad290245b32883f97cb1f01df2bff90c95c651ed69cf05c124f74ea10/temporalobject-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "44ae6e23629a21fade173a3ed2c7effd4dfda9fa38a65bb630d443042b281e39",
"md5": "bd7109260326b35824468750235e244e",
"sha256": "2ca30a434f9ee50d45d939fff00063cba10c3edc67f845fd10fea422aeaebca6"
},
"downloads": -1,
"filename": "temporalobject-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "bd7109260326b35824468750235e244e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 8588,
"upload_time": "2024-07-22T04:34:39",
"upload_time_iso_8601": "2024-07-22T04:34:39.781964Z",
"url": "https://files.pythonhosted.org/packages/44/ae/6e23629a21fade173a3ed2c7effd4dfda9fa38a65bb630d443042b281e39/temporalobject-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-22 04:34:39",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "temporalobject"
}