This guide will help you install DazzleTreeLib and start using it for tree traversal operations.
- Installation
- Quick Start
- Basic Concepts
- Choosing Sync vs Async
- Common Use Cases
- Migrating from Sync to Async
- Next Steps
pip install dazzletreelib# Clone the repository
git clone https://github.com/yourusername/DazzleTreeLib.git
cd DazzleTreeLib
# Install in development mode
pip install -e .
# Or install with development dependencies
pip install -e ".[dev]"- Python 3.8 or higher
- aiofiles (for async file operations)
- No other external dependencies!
from pathlib import Path
from dazzletreelib.sync import FileSystemNode, FileSystemAdapter, traverse_tree
# Create a root node for your directory
root = FileSystemNode("/path/to/directory")
# Create an adapter for filesystem traversal
adapter = FileSystemAdapter()
# Traverse the tree
for node, depth in traverse_tree(root, adapter, max_depth=3):
indent = " " * depth
if node.is_file():
print(f"{indent}📄 {node.path.name}")
else:
print(f"{indent}📁 {node.path.name}/")import asyncio
from dazzletreelib.aio import traverse_tree_async
async def main():
# Even simpler - no adapter needed!
async for node in traverse_tree_async("/path/to/directory", max_depth=3):
if node.path.is_file():
print(f"📄 {node.path.name}")
else:
print(f"📁 {node.path.name}/")
# Run the async function
asyncio.run(main())Nodes represent items in your tree structure:
# Synchronous node
node = FileSystemNode("/path/to/file.txt")
print(node.identifier()) # Unique ID
print(node.is_leaf()) # True for files
print(node.size()) # File size in bytes
# Asynchronous node
async_node = AsyncFileSystemNode("/path/to/file.txt")
print(await async_node.identifier()) # Must await
print(await async_node.is_leaf()) # Must await
print(await async_node.size()) # Must awaitAdapters define how to navigate your tree:
# Synchronous adapter
adapter = FileSystemAdapter(follow_symlinks=False)
# Async adapter (usually created automatically)
async_adapter = AsyncFileSystemAdapter(
batch_size=256, # Process children in batches
max_concurrent=100 # Limit concurrent operations
)Choose how to traverse your tree:
# Breadth-first (level by level)
traverse_tree(root, adapter, strategy='bfs')
# Depth-first pre-order (parent before children)
traverse_tree(root, adapter, strategy='dfs_pre')
# Depth-first post-order (children before parent)
traverse_tree(root, adapter, strategy='dfs_post')Use the synchronous API when:
- Working with small trees (<1000 nodes)
- Integrating with synchronous code
- Debugging is important
- Simplicity is preferred
# Synchronous - Simple and predictable
from dazzletreelib.sync import traverse_tree
def count_files(directory):
root = FileSystemNode(directory)
adapter = FileSystemAdapter()
file_count = 0
for node, _ in traverse_tree(root, adapter):
if node.is_file():
file_count += 1
return file_countUse the asynchronous API when:
- Working with large trees (>1000 nodes)
- I/O operations dominate
- Performance is critical
- Building async applications
# Asynchronous - Fast and scalable
from dazzletreelib.aio import traverse_tree_async
async def count_files_async(directory):
file_count = 0
async for node in traverse_tree_async(directory):
if node.path.is_file():
file_count += 1
return file_count
# 3x+ faster for large trees!import asyncio
from dazzletreelib.aio import traverse_tree_async
async def find_large_files(directory, min_size_mb=10):
large_files = []
async for node in traverse_tree_async(directory):
if node.path.is_file():
size = await node.size()
if size and size > min_size_mb * 1024 * 1024:
large_files.append((node.path, size))
return sorted(large_files, key=lambda x: x[1], reverse=True)
# Usage
files = asyncio.run(find_large_files("/home/user", min_size_mb=100))
for path, size in files[:10]:
print(f"{size/1024/1024:.1f} MB: {path}")from dazzletreelib.sync import traverse_tree, FileSystemNode, FileSystemAdapter
def get_directory_stats(directory):
root = FileSystemNode(directory)
adapter = FileSystemAdapter()
stats = {
'total_files': 0,
'total_dirs': 0,
'total_size': 0,
'max_depth': 0,
'file_types': {}
}
for node, depth in traverse_tree(root, adapter):
if node.is_file():
stats['total_files'] += 1
stats['total_size'] += node.size() or 0
# Track file types
ext = node.path.suffix.lower()
stats['file_types'][ext] = stats['file_types'].get(ext, 0) + 1
else:
stats['total_dirs'] += 1
stats['max_depth'] = max(stats['max_depth'], depth)
return statsimport asyncio
from pathlib import Path
from dazzletreelib.aio import traverse_tree_async
async def search_files(directory, pattern="*.py", content_search=None):
matching_files = []
async for node in traverse_tree_async(directory):
if node.path.is_file() and node.path.match(pattern):
if content_search:
try:
content = node.path.read_text()
if content_search in content:
matching_files.append(node.path)
except:
pass # Skip files that can't be read
else:
matching_files.append(node.path)
return matching_files
# Find all Python files containing "async def"
files = asyncio.run(search_files(
"/project",
pattern="*.py",
content_search="async def"
))# Original synchronous code
from dazzletreelib.sync import traverse_tree, FileSystemNode, FileSystemAdapter
def process_tree_sync(directory):
root = FileSystemNode(directory)
adapter = FileSystemAdapter()
results = []
for node, depth in traverse_tree(root, adapter):
if node.is_file():
# Synchronous I/O
size = node.size()
results.append({
'path': node.path,
'size': size,
'depth': depth
})
return results# Converted to asynchronous
from dazzletreelib.aio import traverse_tree_async
import asyncio
async def process_tree_async(directory):
results = []
# Note: async version doesn't return depth directly
async for node in traverse_tree_async(directory):
if node.path.is_file():
# Asynchronous I/O
size = await node.size()
results.append({
'path': node.path,
'size': size
})
return results# Synchronous caller
results = process_tree_sync("/data")
# Asynchronous caller
results = asyncio.run(process_tree_async("/data"))| Aspect | Synchronous | Asynchronous |
|---|---|---|
| Import | from dazzletreelib.sync import ... |
from dazzletreelib.aio import ... |
| Function | traverse_tree() |
traverse_tree_async() |
| Adapter | Required | Optional (auto-created) |
| Returns | (node, depth) tuples |
Just nodes |
| Methods | Direct calls | Must await |
| Loop | for node, depth in ... |
async for node in ... |
| Speed | Baseline | 3x+ faster |
# Use generators for memory efficiency
def process_files(directory):
root = FileSystemNode(directory)
adapter = FileSystemAdapter()
# Generator - memory efficient
for node, _ in traverse_tree(root, adapter):
if should_process(node):
yield process(node) # Yield instead of accumulating# Tune batch size and concurrency for your use case
async def optimized_traversal(directory):
# For small trees
if is_small_tree(directory):
async for node in traverse_tree_async(
directory,
batch_size=32, # Smaller batches
max_concurrent=10 # Lower concurrency
):
await process(node)
# For large trees
else:
async for node in traverse_tree_async(
directory,
batch_size=512, # Larger batches
max_concurrent=200 # Higher concurrency
):
await process(node)Both APIs handle errors gracefully:
# Synchronous error handling
try:
for node, depth in traverse_tree(root, adapter):
try:
process(node)
except Exception as e:
print(f"Error processing {node.path}: {e}")
continue # Continue with next node
except Exception as e:
print(f"Fatal traversal error: {e}")
# Asynchronous error handling
try:
async for node in traverse_tree_async(directory):
try:
await process(node)
except Exception as e:
print(f"Error processing {node.path}: {e}")
continue
except Exception as e:
print(f"Fatal traversal error: {e}")Now that you understand the basics:
- Learn the Architecture: Read the Architecture Overview to understand the design
- Explore the API: Check the API Reference for detailed documentation
- Create Custom Adapters: Follow the Adapter Development Guide to support new tree types
- Optimize Performance: Read the Performance Guide for tuning tips
- Study Examples: Browse the Examples directory for real-world usage
- Documentation: You're reading it!
- Examples: Check the examples directory
- Issues: Report bugs on GitHub Issues
- Discussions: Ask questions in GitHub Discussions
Q: Why do I need an adapter for sync but not async? A: The async API automatically creates a filesystem adapter when given a path. The sync API requires explicit adapter creation for flexibility.
Q: How much faster is async really? A: Typically 3-4x faster for I/O-heavy operations on large trees. See the Performance Guide for benchmarks.
Q: Can I use both sync and async in the same application? A: Yes! They're completely separate and can be used together. See the Compatibility Guide.
Q: What Python versions are supported? A: Python 3.8 and higher. Async features require Python's asyncio support.
Q: Is it production-ready? A: Yes! Version 0.6.0+ has comprehensive tests and is used in production systems.