🔍 Code Extractor

class GraphicSpec

Maturity: 50

A dataclass that defines the specification for a graphic to be generated, including its type, description, parameters, style preferences, and optional image data.

File:
/tf/active/vicechatdev/e-ink-llm/graphics_generator.py
Lines:
34 - 43
Complexity:
simple

Purpose

GraphicSpec serves as a data container for specifying all the necessary information required to generate a graphic. It encapsulates the graphic's identity, type, descriptive information, generation parameters, styling preferences, and can store the resulting image data after generation. This class is typically used in graphic generation pipelines where specifications need to be passed between components, stored, or serialized.

Source Code

class GraphicSpec:
    """Specification for a graphic to be generated"""
    id: str
    type: GraphicType
    description: str
    parameters: Dict[str, Any]
    style_preferences: Dict[str, Any]
    image_data: Optional[str] = None  # Base64 encoded image after generation
    width: Optional[int] = None
    height: Optional[int] = None

Parameters

Name Type Default Kind
bases - -

Parameter Details

id: Unique identifier string for the graphic specification. Used to track and reference specific graphics throughout the generation pipeline.

type: GraphicType enum value that specifies the category or kind of graphic to be generated (e.g., chart, diagram, plot).

description: Human-readable string describing what the graphic should represent or display. Provides context for the graphic generation process.

parameters: Dictionary containing key-value pairs of generation-specific parameters. These control the data, layout, and functional aspects of the graphic (e.g., data points, axes labels, dimensions).

style_preferences: Dictionary containing key-value pairs of styling options. These control the visual appearance of the graphic (e.g., colors, fonts, themes, line styles).

image_data: Optional string containing the Base64 encoded image data after the graphic has been generated. Defaults to None before generation.

width: Optional integer specifying the width of the generated graphic in pixels. Defaults to None if not specified.

height: Optional integer specifying the height of the generated graphic in pixels. Defaults to None if not specified.

Return Value

Instantiation returns a GraphicSpec object containing all the specified attributes. As a dataclass, it automatically generates __init__, __repr__, and __eq__ methods. The object serves as an immutable-by-convention data container for graphic specifications.

Class Interface

Attributes

Name Type Description Scope
id str Unique identifier for the graphic specification instance
type GraphicType Enum value specifying the type/category of graphic to generate instance
description str Human-readable description of what the graphic represents instance
parameters Dict[str, Any] Dictionary of generation parameters controlling data and functional aspects instance
style_preferences Dict[str, Any] Dictionary of styling options controlling visual appearance instance
image_data Optional[str] Base64 encoded image string after generation, None before generation instance
width Optional[int] Width of the graphic in pixels, None if not specified instance
height Optional[int] Height of the graphic in pixels, None if not specified instance

Dependencies

  • dataclasses
  • typing
  • enum

Required Imports

from dataclasses import dataclass
from typing import Dict, Any, Optional

Conditional/Optional Imports

These imports are only needed under specific conditions:

from enum import Enum

Condition: Required to define or use the GraphicType enum that is referenced by the 'type' attribute

Required (conditional)

Usage Example

from dataclasses import dataclass
from typing import Dict, Any, Optional
from enum import Enum

class GraphicType(Enum):
    CHART = 'chart'
    DIAGRAM = 'diagram'
    PLOT = 'plot'

@dataclass
class GraphicSpec:
    id: str
    type: GraphicType
    description: str
    parameters: Dict[str, Any]
    style_preferences: Dict[str, Any]
    image_data: Optional[str] = None
    width: Optional[int] = None
    height: Optional[int] = None

# Create a graphic specification
spec = GraphicSpec(
    id='chart_001',
    type=GraphicType.CHART,
    description='Sales data bar chart',
    parameters={'data': [10, 20, 30], 'labels': ['Q1', 'Q2', 'Q3']},
    style_preferences={'color': 'blue', 'theme': 'dark'},
    width=800,
    height=600
)

# Access attributes
print(spec.id)  # 'chart_001'
print(spec.type)  # GraphicType.CHART
print(spec.description)  # 'Sales data bar chart'

# After generation, set image data
spec.image_data = 'base64_encoded_image_string_here'

# Dataclass provides automatic __repr__
print(spec)  # Shows all attributes

Best Practices

  • This is a dataclass, so it's designed to be a simple data container. Avoid adding complex methods or business logic.
  • The image_data field should be populated after graphic generation, not during initialization unless loading a pre-generated graphic.
  • Use the parameters dict for data and functional settings, and style_preferences for visual styling to maintain clear separation of concerns.
  • The id should be unique within your application context to avoid confusion when tracking multiple graphics.
  • Width and height are optional but should be set if you need consistent sizing across graphics.
  • Since dataclasses are mutable by default, be cautious when sharing instances across threads or modifying after creation.
  • Consider using frozen=True in the @dataclass decorator if immutability is desired: @dataclass(frozen=True).
  • The GraphicType enum must be defined before using this class; ensure it's imported or defined in the same module.
  • When serializing to JSON, you'll need custom handling for the GraphicType enum (convert to string) and ensure image_data is properly encoded.
  • This class has no methods, so lifecycle is simple: instantiate, read/write attributes, and pass to other components.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class GraphicType 59.8% similar

    An enumeration class that defines the types of graphics that can be generated in the system.

    From: /tf/active/vicechatdev/e-ink-llm/graphics_generator.py
  • class GraphicPlaceholder 59.3% similar

    A dataclass that represents a placeholder for graphics (charts, diagrams, etc.) embedded within text responses, storing metadata about the graphic's type, description, parameters, and position.

    From: /tf/active/vicechatdev/e-ink-llm/hybrid_response_handler.py
  • class GraphicsGenerator 58.1% similar

    GraphicsGenerator is a coordinator class that orchestrates the generation of different types of graphics (charts, diagrams, illustrations, and sketches) by delegating to specialized generator classes.

    From: /tf/active/vicechatdev/e-ink-llm/graphics_generator.py
  • function get_spec 57.0% similar

    Extracts a specification tuple from a labeled data object, consisting of the class name, group, and label attributes.

    From: /tf/active/vicechatdev/patches/util.py
  • class CompactSection 55.1% similar

    A dataclass representing a section in compact format with an icon, title, content, and priority level.

    From: /tf/active/vicechatdev/e-ink-llm/compact_formatter.py
← Back to Browse