Overview
The Refactron class is the main entry point for all code analysis and refactoring operations.
Constructor
from refactron import Refactron
from refactron.core.config import RefactronConfig
# Default configuration
refactron = Refactron()
# Custom configuration
config = RefactronConfig(
max_function_complexity = 15 ,
enabled_analyzers = [ "security" , "complexity" ]
)
refactron = Refactron(config)
config
RefactronConfig
default: "None"
Configuration object. If None, uses default configuration.
Methods
analyze()
Analyze a file or directory for issues.
def analyze ( target : Union[ str , Path]) -> AnalysisResult
Path to file or directory to analyze
Returns: AnalysisResult containing detected issues and metrics
Example:
refactron = Refactron()
result = refactron.analyze( "myproject/" )
print ( f "Files analyzed: { result.summary[ 'files_analyzed' ] } " )
print ( f "Total issues: { result.summary[ 'total_issues' ] } " )
for issue in result.issues:
print ( f " { issue.level.value } : { issue.message } " )
refactor()
Refactor a file or directory with intelligent transformations.
def refactor (
target : Union[ str , Path],
preview : bool = True ,
operation_types : Optional[List[ str ]] = None
) -> RefactorResult
Path to file or directory to refactor
If True, show changes without applying them
Specific refactoring types to apply. None = all types. Available types:
extract_constant
add_docstring
simplify_conditionals
reduce_parameters
Returns: RefactorResult containing operations and status
Example:
# Preview all refactorings
result = refactron.refactor( "myfile.py" , preview = True )
result.show_diff()
# Apply specific refactorings
result = refactron.refactor(
"myfile.py" ,
preview = False ,
operation_types = [ "extract_constant" , "add_docstring" ]
)
if result.success:
print ( f "Applied { len (result.operations) } refactorings" )
print ( f "Backup: { result.backup_path } " )
record_feedback()
Record developer feedback on a refactoring suggestion.
def record_feedback (
operation_id : str ,
action : str ,
reason : Optional[ str ] = None ,
operation : Optional[RefactoringOperation] = None
) -> None
Unique identifier for the refactoring operation
Feedback action: "accepted", "rejected", or "ignored"
Optional reason for the feedback
operation
RefactoringOperation
default: "None"
The refactoring operation object (for pattern learning)
Example:
result = refactron.refactor( "myfile.py" , preview = True )
for op in result.operations:
refactron.record_feedback(
operation_id = op.operation_id,
action = "accepted" ,
reason = "Improved readability" ,
operation = op
)
get_python_files()
Get all Python files in a directory, respecting exclude patterns.
def get_python_files ( directory : Path) -> List[Path]
Directory to search for Python files
Returns: List of Python file paths
Example:
from pathlib import Path
refactron = Refactron()
files = refactron.get_python_files(Path( "myproject" ))
print ( f "Found { len (files) } Python files" )
for file in files:
print ( file )
Get performance statistics from optimization components.
def get_performance_stats () -> Dict[ str , Any]
Returns: Dictionary containing performance statistics
Example:
stats = refactron.get_performance_stats()
print ( f "AST cache hits: { stats[ 'ast_cache' ][ 'hits' ] } " )
print ( f "AST cache misses: { stats[ 'ast_cache' ][ 'misses' ] } " )
print ( f "Parallel jobs: { stats[ 'parallel' ][ 'max_workers' ] } " )
clear_caches()
Clear all performance-related caches.
def clear_caches () -> None
Example:
# Clear caches after major code changes
refactron.clear_caches()
detect_project_root ()
Detect project root by looking for common markers.
def detect_project_root ( file_path : Path) -> Path
File path to start searching from
Returns: Path to project root
Example:
from pathlib import Path
refactron = Refactron()
root = refactron.detect_project_root(Path( "src/module/file.py" ))
print ( f "Project root: { root } " )
Properties
config
Access current configuration.
config = refactron.config
print ( f "Max complexity: { config.max_function_complexity } " )
analyzers
List of enabled analyzers.
analyzers = refactron.analyzers
print ( f "Enabled analyzers: { [a. __class__ . __name__ for a in analyzers] } " )
refactorers
List of enabled refactorers.
refactorers = refactron.refactorers
print ( f "Enabled refactorers: { [r. __class__ . __name__ for r in refactorers] } " )
Complete Example
from refactron import Refactron
from refactron.core.config import RefactronConfig
from pathlib import Path
# Configure Refactron
config = RefactronConfig(
max_function_complexity = 15 ,
max_function_length = 100 ,
enabled_analyzers = [ "security" , "complexity" , "code_smell" ],
enabled_refactorers = [ "extract_constant" , "add_docstring" ],
enable_pattern_learning = True
)
# Initialize
refactron = Refactron(config)
# Analyze project
print ( "Analyzing project..." )
analysis = refactron.analyze( "myproject/" )
# Print summary
print ( f " \n Analysis Summary:" )
print ( f " Files analyzed: { analysis.summary[ 'files_analyzed' ] } " )
print ( f " Total issues: { analysis.summary[ 'total_issues' ] } " )
# Print critical issues
critical = [i for i in analysis.issues if i.level.value == "CRITICAL" ]
if critical:
print ( f " \n Critical Issues ( { len (critical) } ):" )
for issue in critical:
print ( f " { issue.file_path } : { issue.line_number } - { issue.message } " )
# Refactor with preview
print ( " \n Generating refactoring suggestions..." )
result = refactron.refactor( "myproject/" , preview = True )
# Review and apply specific operations
for op in result.operations:
print ( f " \n Operation: { op.operation_type } " )
print ( f " File: { op.file_path } : { op.line_number } " )
print ( f " Risk: { op.risk_score } " )
print ( f " Description: { op.description } " )
# Record feedback
refactron.record_feedback(
operation_id = op.operation_id,
action = "accepted" ,
reason = "Approved after review" ,
operation = op
)
# Get performance stats
stats = refactron.get_performance_stats()
print ( f " \n Performance:" )
print ( f " AST cache hit rate: { stats[ 'ast_cache' ][ 'hit_rate' ] :.1%} " )
print ( " \n Refactoring complete!" )
Next Steps
Code Analysis Guide Learn about analysis features
Refactoring Guide Master refactoring workflows