class ComplexDocument
A class representing a complex document with multiple sections, supporting section management, references, metadata, and serialization capabilities.
/tf/active/vicechatdev/vice_ai/complex_app.py
272 - 343
moderate
Purpose
ComplexDocument provides a structured way to manage documents with multiple sections. It handles document metadata (title, author, timestamps), section ordering and manipulation, reference tracking across sections, and serialization to/from dictionary format. This class is designed for applications that need to build, edit, and export structured documents with hierarchical content.
Source Code
class ComplexDocument:
"""Represents a complex document with multiple sections"""
def __init__(self, doc_id, title='Untitled Document', author=''):
self.id = doc_id
self.title = title
self.author = author
self.sections = [] # List of DocumentSection objects
self.global_references = []
self.metadata = {}
self.created_at = datetime.now()
self.updated_at = datetime.now()
def add_section(self, section):
"""Add a section to the document"""
self.sections.append(section)
self.updated_at = datetime.now()
def remove_section(self, section_id):
"""Remove a section from the document"""
original_count = len(self.sections)
self.sections = [s for s in self.sections if s.id != section_id]
self.updated_at = datetime.now()
return len(self.sections) < original_count # Return True if section was removed
def get_section(self, section_id):
"""Get a section by ID"""
for section in self.sections:
if section.id == section_id:
return section
return None
def move_section(self, section_id, direction):
"""Move a section up or down in the order"""
for i, section in enumerate(self.sections):
if section.id == section_id:
if direction == 'up' and i > 0:
self.sections[i], self.sections[i-1] = self.sections[i-1], self.sections[i]
elif direction == 'down' and i < len(self.sections) - 1:
self.sections[i], self.sections[i+1] = self.sections[i+1], self.sections[i]
self.updated_at = datetime.now()
return True
return False
def get_all_references(self):
"""Get all references from all sections"""
all_refs = list(self.global_references)
for section in self.sections:
all_refs.extend(section.references)
return all_refs
def to_dict(self):
return {
'id': self.id,
'title': self.title,
'author': self.author,
'sections': [section.to_dict() for section in self.sections],
'global_references': self.global_references,
'metadata': self.metadata,
'created_at': self.created_at.isoformat(),
'updated_at': self.updated_at.isoformat()
}
@classmethod
def from_dict(cls, data):
doc = cls(data['id'], data.get('title', 'Untitled'), data.get('author', ''))
doc.sections = [DocumentSection.from_dict(s) for s in data.get('sections', [])]
doc.global_references = data.get('global_references', [])
doc.metadata = data.get('metadata', {})
doc.created_at = datetime.fromisoformat(data.get('created_at', datetime.now().isoformat()))
doc.updated_at = datetime.fromisoformat(data.get('updated_at', datetime.now().isoformat()))
return doc
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
doc_id: Unique identifier for the document. Required parameter that should be a unique value (string or integer) to distinguish this document from others.
title: The title of the document. Optional parameter with default value 'Untitled Document'. Should be a string representing the document's name.
author: The author of the document. Optional parameter with default value '' (empty string). Should be a string containing the author's name or identifier.
Return Value
Instantiation returns a ComplexDocument object with initialized attributes. Key method returns: add_section() returns None, remove_section() returns boolean (True if section was removed), get_section() returns DocumentSection object or None, move_section() returns boolean (True if move was successful), get_all_references() returns list of all references, to_dict() returns dictionary representation, from_dict() class method returns ComplexDocument instance.
Class Interface
Methods
__init__(self, doc_id, title='Untitled Document', author='') -> None
Purpose: Initialize a new ComplexDocument instance with basic metadata and empty sections list
Parameters:
doc_id: Unique identifier for the document (required)title: Document title, defaults to 'Untitled Document'author: Document author, defaults to empty string
Returns: None (constructor)
add_section(self, section) -> None
Purpose: Add a DocumentSection object to the document and update the timestamp
Parameters:
section: DocumentSection object to be added to the document
Returns: None
remove_section(self, section_id) -> bool
Purpose: Remove a section from the document by its ID and update the timestamp
Parameters:
section_id: The ID of the section to remove
Returns: Boolean: True if a section was removed, False if no section with that ID was found
get_section(self, section_id) -> DocumentSection | None
Purpose: Retrieve a section by its ID
Parameters:
section_id: The ID of the section to retrieve
Returns: DocumentSection object if found, None if no section with that ID exists
move_section(self, section_id, direction) -> bool
Purpose: Move a section up or down in the document order by one position
Parameters:
section_id: The ID of the section to movedirection: String: 'up' to move section earlier in order, 'down' to move later
Returns: Boolean: True if section was successfully moved, False if section not found or move not possible (already at boundary)
get_all_references(self) -> list
Purpose: Collect and return all references from both global references and all sections
Returns: List containing all global references plus references from all sections
to_dict(self) -> dict
Purpose: Serialize the document to a dictionary representation for storage or transmission
Returns: Dictionary containing all document data including id, title, author, sections (as dicts), global_references, metadata, and ISO format timestamps
from_dict(cls, data) -> ComplexDocument
Purpose: Class method to deserialize a dictionary into a ComplexDocument instance
Parameters:
data: Dictionary containing document data with keys: id, title, author, sections, global_references, metadata, created_at, updated_at
Returns: ComplexDocument instance reconstructed from the dictionary data
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
id |
any (typically string or int) | Unique identifier for the document | instance |
title |
str | The title of the document | instance |
author |
str | The author of the document | instance |
sections |
list | List of DocumentSection objects representing the document's sections in order | instance |
global_references |
list | List of references that apply to the entire document (not section-specific) | instance |
metadata |
dict | Dictionary for storing arbitrary metadata key-value pairs about the document | instance |
created_at |
datetime | Timestamp when the document was created | instance |
updated_at |
datetime | Timestamp when the document was last modified (automatically updated by add_section, remove_section, and move_section) | instance |
Dependencies
datetime
Required Imports
from datetime import datetime
Usage Example
from datetime import datetime
# Assuming DocumentSection class exists
class DocumentSection:
def __init__(self, section_id, content=''):
self.id = section_id
self.content = content
self.references = []
def to_dict(self):
return {'id': self.id, 'content': self.content, 'references': self.references}
@classmethod
def from_dict(cls, data):
section = cls(data['id'], data.get('content', ''))
section.references = data.get('references', [])
return section
# Create a new document
doc = ComplexDocument(doc_id='doc_001', title='My Research Paper', author='John Doe')
# Add sections
section1 = DocumentSection('sec_1', 'Introduction content')
section2 = DocumentSection('sec_2', 'Methods content')
doc.add_section(section1)
doc.add_section(section2)
# Add global references
doc.global_references.append('Reference 1')
# Add metadata
doc.metadata['category'] = 'research'
doc.metadata['version'] = '1.0'
# Get a specific section
section = doc.get_section('sec_1')
# Move section down
doc.move_section('sec_1', 'down')
# Get all references
all_refs = doc.get_all_references()
# Serialize to dictionary
doc_dict = doc.to_dict()
# Deserialize from dictionary
restored_doc = ComplexDocument.from_dict(doc_dict)
# Remove a section
was_removed = doc.remove_section('sec_2')
Best Practices
- Always provide a unique doc_id when instantiating to avoid conflicts in document tracking systems
- The sections list expects DocumentSection objects with at least 'id', 'references', 'to_dict()', and 'from_dict()' members
- The updated_at timestamp is automatically updated when sections are added, removed, or moved
- Use get_section() to safely retrieve sections by ID rather than directly accessing the sections list
- When using from_dict(), ensure the dictionary contains valid ISO format datetime strings for created_at and updated_at
- The move_section() method only moves sections one position at a time; call multiple times for larger moves
- Global references are stored separately from section-level references; use get_all_references() to retrieve both
- The metadata dictionary can store arbitrary key-value pairs for custom document properties
- Section order is preserved in the sections list; use move_section() to reorder rather than manual list manipulation
- The remove_section() method returns a boolean indicating success, useful for validation and error handling
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class DocumentSection 86.6% similar
-
class Document 77.3% similar
-
class DocumentSection_v1 72.1% similar
-
class DocumentService 65.6% similar
-
class TextSection 63.0% similar