class TextSectionVersion
A dataclass representing a single version in the history of a text section's content, tracking changes, authorship, and timestamps.
/tf/active/vicechatdev/vice_ai/models.py
241 - 269
simple
Purpose
TextSectionVersion serves as a version control record for text content, storing snapshots of content changes along with metadata like author, timestamp, and whether the content was AI-generated. It provides serialization methods for persistence and retrieval, enabling version history tracking and rollback capabilities for text sections.
Source Code
class TextSectionVersion:
"""Version history for text section content"""
version_id: str
content: str
timestamp: datetime
author: str
change_summary: str = ""
generated_by_ai: bool = False
def to_dict(self) -> Dict:
return {
'version_id': self.version_id,
'content': self.content,
'timestamp': self.timestamp.isoformat(),
'author': self.author,
'change_summary': self.change_summary,
'generated_by_ai': self.generated_by_ai
}
@classmethod
def from_dict(cls, data: Dict) -> 'TextSectionVersion':
return cls(
version_id=data['version_id'],
content=data['content'],
timestamp=datetime.fromisoformat(data['timestamp']),
author=data['author'],
change_summary=data.get('change_summary', ''),
generated_by_ai=data.get('generated_by_ai', False)
)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
version_id: Unique identifier for this version, typically a UUID string. Used to reference and retrieve specific versions.
content: The actual text content stored in this version snapshot. This is the complete text as it existed at the time of this version.
timestamp: A datetime object indicating when this version was created. Used for chronological ordering and audit trails.
author: String identifier of the person or system that created this version. Could be a username, user ID, or system identifier.
change_summary: Optional string describing what changed in this version. Defaults to empty string if not provided. Useful for understanding version history.
generated_by_ai: Boolean flag indicating whether this content was generated by AI (True) or created by a human (False). Defaults to False.
Return Value
Instantiation returns a TextSectionVersion object with all specified attributes. The to_dict() method returns a dictionary with all attributes serialized (timestamp converted to ISO format string). The from_dict() class method returns a new TextSectionVersion instance reconstructed from a dictionary.
Class Interface
Methods
to_dict(self) -> Dict
Purpose: Serializes the TextSectionVersion instance to a dictionary format suitable for JSON storage or transmission
Returns: Dictionary containing all instance attributes with timestamp converted to ISO format string. Keys: 'version_id', 'content', 'timestamp', 'author', 'change_summary', 'generated_by_ai'
from_dict(cls, data: Dict) -> TextSectionVersion
Purpose: Class method that creates a TextSectionVersion instance from a dictionary, typically used for deserialization from storage
Parameters:
data: Dictionary containing version data with keys 'version_id', 'content', 'timestamp' (ISO format string), 'author', and optionally 'change_summary' and 'generated_by_ai'
Returns: New TextSectionVersion instance populated with data from the dictionary, with timestamp converted from ISO string to datetime object
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
version_id |
str | Unique identifier for this version, typically a UUID string | instance |
content |
str | The text content stored in this version snapshot | instance |
timestamp |
datetime | When this version was created, stored as a datetime object | instance |
author |
str | Identifier of the person or system that created this version | instance |
change_summary |
str | Optional description of what changed in this version, defaults to empty string | instance |
generated_by_ai |
bool | Flag indicating whether this content was AI-generated (True) or human-created (False), defaults to False | instance |
Dependencies
datetimetyping
Required Imports
from datetime import datetime
from typing import Dict
from dataclasses import dataclass
Usage Example
from datetime import datetime
from typing import Dict
from dataclasses import dataclass
import uuid
# Create a new version
version = TextSectionVersion(
version_id=str(uuid.uuid4()),
content="This is the updated text content.",
timestamp=datetime.now(),
author="user123",
change_summary="Fixed typos and improved clarity",
generated_by_ai=False
)
# Serialize to dictionary for storage
version_dict = version.to_dict()
print(version_dict)
# Output: {'version_id': '...', 'content': '...', 'timestamp': '2024-01-15T10:30:00', ...}
# Deserialize from dictionary
restored_version = TextSectionVersion.from_dict(version_dict)
print(restored_version.content)
# Output: This is the updated text content.
# Access attributes
print(f"Version by {restored_version.author} at {restored_version.timestamp}")
print(f"AI generated: {restored_version.generated_by_ai}")
Best Practices
- Always generate unique version_id values (e.g., using uuid.uuid4()) to avoid conflicts
- Use datetime.now() for timestamp when creating new versions to ensure accurate chronological ordering
- Provide meaningful change_summary values to make version history understandable
- Set generated_by_ai flag correctly to distinguish between human and AI-generated content
- Use to_dict() for serialization before storing in databases or JSON files
- Use from_dict() class method for deserialization when loading from storage
- This is an immutable data record - create new instances rather than modifying existing ones
- Store version_id as strings even if using UUIDs for better serialization compatibility
- The timestamp is stored as a datetime object internally but serialized to ISO format string
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class TextSection 81.5% similar
-
class DocumentVersion 76.1% similar
-
class DocumentSection_v1 70.4% similar
-
class DataSection 63.9% similar
-
class DocumentSection 63.3% similar