🔍 Code Extractor

class FileType

Maturity: 35

An enumeration class that defines supported file types for document processing, including PDF, EPUB, notes, and unknown file types.

File:
/tf/active/vicechatdev/rmcl/const.py
Lines:
20 - 27
Complexity:
simple

Purpose

FileType is an enumeration that provides a type-safe way to represent and work with different file formats in a document processing system. It extends Python's enum.Enum to define four distinct file types (pdf, epub, notes, unknown) and provides a custom string representation. This enum is typically used for file type identification, validation, and routing logic in file processing workflows.

Source Code

class FileType(enum.Enum):
    pdf = 'pdf'
    epub = 'epub'
    notes = 'notes'
    unknown = 'unknown'

    def __str__(self):
        return self.name

Parameters

Name Type Default Kind
bases enum.Enum -

Parameter Details

bases: Inherits from enum.Enum, which provides the enumeration functionality. No constructor parameters are needed as this is an enum class with predefined members.

Return Value

Instantiation returns a FileType enum member (e.g., FileType.pdf, FileType.epub). The __str__ method returns the name of the enum member as a string (e.g., 'pdf', 'epub'). Each enum member has a 'value' attribute that matches its name.

Class Interface

Methods

__str__(self) -> str

Purpose: Returns the string representation of the enum member, specifically its name

Returns: The name of the enum member as a string (e.g., 'pdf', 'epub', 'notes', 'unknown')

Attributes

Name Type Description Scope
pdf FileType Enum member representing PDF file type with value 'pdf' class
epub FileType Enum member representing EPUB file type with value 'epub' class
notes FileType Enum member representing notes file type with value 'notes' class
unknown FileType Enum member representing unknown or unrecognized file type with value 'unknown' class
name str Inherited from Enum - the name of the enum member (e.g., 'pdf', 'epub') instance
value str Inherited from Enum - the value associated with the enum member (e.g., 'pdf', 'epub') instance

Dependencies

  • enum

Required Imports

import enum

Usage Example

# Import the enum module
import enum

# Define the FileType class
class FileType(enum.Enum):
    pdf = 'pdf'
    epub = 'epub'
    notes = 'notes'
    unknown = 'unknown'

    def __str__(self):
        return self.name

# Access enum members
file_type = FileType.pdf
print(file_type)  # Output: pdf
print(file_type.value)  # Output: pdf
print(file_type.name)  # Output: pdf

# Compare enum members
if file_type == FileType.pdf:
    print("This is a PDF file")

# Iterate over all file types
for ft in FileType:
    print(f"Type: {ft}, Value: {ft.value}")

# Get enum member by value
file_type_from_value = FileType('epub')
print(file_type_from_value)  # Output: epub

# Get enum member by name
file_type_from_name = FileType['notes']
print(file_type_from_name)  # Output: notes

Best Practices

  • Use enum members for type-safe comparisons instead of string literals (e.g., use 'FileType.pdf' instead of 'pdf')
  • Access enum members using dot notation (FileType.pdf) or bracket notation (FileType['pdf'])
  • Use the 'value' attribute to get the string representation of the enum member
  • The custom __str__ method returns the name, making it easy to convert to string for logging or display
  • Enum members are immutable and singleton - comparing with 'is' or '==' both work
  • Use FileType.unknown as a fallback for unrecognized file types
  • When serializing to JSON or databases, use the .value attribute to get the string representation
  • Enum members can be iterated over using 'for member in FileType'
  • Do not instantiate FileType directly; use the predefined class attributes

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class SectionType 59.7% similar

    An enumeration class that defines the types of sections that can be added to documents, providing standardized section type identifiers.

    From: /tf/active/vicechatdev/vice_ai/models.py
  • class CommentType 53.8% similar

    An enumeration class that defines standardized types for comments used in review and approval workflows.

    From: /tf/active/vicechatdev/CDocs single class/models/workflow_base.py
  • class GraphicType 52.5% 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 NodeType 51.5% similar

    An enumeration class that defines the three types of nodes that can exist in the reMarkable cloud storage system.

    From: /tf/active/vicechatdev/e-ink-llm/cloudtest/local_replica.py
  • class AnalysisType 49.2% similar

    An enumeration class that defines standardized types of statistical analyses available in the system.

    From: /tf/active/vicechatdev/vice_ai/models.py
← Back to Browse