Skip to content

BruinGrowly/Python-Code-Harmonizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Python Code Harmonizer

CI Status Version Python License Tests Framework

The world's first semantic code debugger with a mathematically proven foundation.


What Is This?

Python Code Harmonizer finds bugs other tools miss by analyzing what your code means, not just what it says.

The core question:

"Does your function DO what its name SAYS it does?"

Example: The Bug Others Miss

def get_user(user_id):
    """Retrieve user from database"""
    db.delete(user_id)  # BUG: Deletes instead of retrieving!
    return None

Traditional tools: βœ… Syntax valid, types correct, no security issues

Harmonizer: 🚨 CRITICAL DISHARMONY (1.22) - Function name says "get" but code does "delete"

This is a semantic bug - the kind that causes production incidents because the code lies about what it does.


Why This Matters

Every developer has encountered code where:

  • Function names lie about what they do
  • "Validate" functions secretly modify data
  • "Get" functions delete records
  • "Check" functions trigger side effects

These intent-execution mismatches cause bugs, confusion, and maintenance nightmares.

Python Code Harmonizer finds them automatically.


How It Works

The Mathematical Foundation

The tool is built on proven mathematics:

Four Semantic Dimensions (see MATHEMATICAL_FOUNDATION.md):

  • Love (L): Connection, integration, communication
  • Justice (J): Correctness, validation, truth
  • Power (P): Action, execution, transformation
  • Wisdom (W): Information, knowledge, analysis

Proven properties:

  1. βœ… Orthogonal: Linearly independent (each dimension is unique)
  2. βœ… Complete: Spans all semantic meaning (everything can be expressed)
  3. βœ… Minimal: All four necessary (can't remove any)
  4. βœ… Universal: Applies to all programming languages

Mapping Code to Meaning

Every programming operation maps to these dimensions:

Your Code Dimension Semantic Meaning
get(), read(), query() Wisdom Retrieving information
validate(), check(), assert Justice Verifying correctness
create(), update(), delete() Power Changing state
send(), connect(), notify() Love Integrating systems

184 programming verbs precisely mapped (see PROGRAMMING_LANGUAGE_SEMANTICS.md)

Detecting Disharmony

The tool compares intent (function name) with execution (what it actually does):

# HARMONIOUS (score: 0.05)
def calculate_total(items):
    return sum(item.price for item in items)
# Intent: Wisdom (calculate)
# Execution: Wisdom (sum/computation)
# βœ“ Aligned!

# DISHARMONIOUS (score: 1.18)
def validate_email(email):
    send_welcome_email(email)  # Side effect!
    return "@" in email
# Intent: Justice (validate)
# Execution: Love (send) + Justice (check)
# ⚠️ Validation shouldn't send emails!

Distance in 4D semantic space = disharmony score

High distance = intent contradicts execution = probable bug


Quick Start

Installation

git clone https://github.com/BruinGrowly/Python-Code-Harmonizer.git
cd Python-Code-Harmonizer
pip install .

Your First Analysis

harmonizer mycode.py

Output:

======================================================================
Python Code Harmonizer (v2.0)
Powered By: DIVE-V2 (Enhanced Programming Semantics)
Disharmony Threshold: 0.5
======================================================================

Analyzing: mycode.py
----------------------------------------------------------------------
FUNCTION NAME                | DISHARMONY SCORE | STATUS
-----------------------------|------------------|--------------------
validate_and_save_user       | 0.85             | !! DISHARMONY
get_cached_value             | 0.62             | !! DISHARMONY
calculate_total              | 0.05             | βœ“ HARMONIOUS
delete_expired_records       | 0.08             | βœ“ HARMONIOUS
======================================================================

Understanding Scores

Score Meaning Action
0.0-0.3 βœ… Excellent Code says what it means
0.3-0.5 ⚠️ Minor drift Review for clarity
0.5-0.8 ⚠️ Concerning Notable mismatch - investigate
0.8-1.2 ❗ High concern Significant contradiction
1.2+ 🚨 Critical Severe disharmony - fix now

πŸ—ΊοΈ Legacy Code Mapper - Understand Any Codebase

NEW: Complete semantic analysis of entire codebases with git history tracking, architectural debt estimation, and interactive visualizations.

What It Does

The Legacy Code Mapper answers the challenge: "Legacy code's real complexity still fights back" by providing:

  • Semantic Clustering: Maps all files to LJPW space and groups by semantic purpose
  • Architectural Smell Detection: Finds God Files, Mixed Concerns, High Disharmony, Semantic Confusion
  • Refactoring Opportunities: Ranks files by impact with specific actionable recommendations
  • Git History Tracking: Tracks how code evolved semantically over commits
  • Architecture Reality Check: Compares documentation vs actual implementation
  • Debt Estimation: Calculates technical debt in hours and dollars with priorities
  • Interactive Visualizations: 3D semantic maps, drift timelines, HTML exports

Quick Start

# Analyze entire codebase with all features
python -m harmonizer.legacy_mapper <path> --full

# Just the basics (clustering, smells, opportunities)
python -m harmonizer.legacy_mapper <path>

# With specific analyses
python -m harmonizer.legacy_mapper <path> --git-commits 100 --hourly-rate 200

# Export interactive HTML visualization
python -m harmonizer.legacy_mapper <path> --export-html

Example Output

πŸ” Analyzing codebase: myproject
Found 45 Python files

βœ… Analyzed 45 files successfully

======================================================================
SEMANTIC CODEBASE MAP - COMPREHENSIVE ANALYSIS
======================================================================

πŸ“š WISDOM CLUSTER (28 files)
   Avg Coordinates: L=0.15, J=0.20, P=0.01, W=0.48
   Files:
     - data_processor.py              (32 funcs, disharmony: 0.71)
     - analyzer.py                    (18 funcs, disharmony: 0.65)
     ...

βš–οΈ JUSTICE CLUSTER (12 files)
   Avg Coordinates: L=0.10, J=0.55, P=0.05, W=0.25
   Files:
     - validators.py                  (24 funcs, disharmony: 0.58)
     ...

πŸ“Š OVERALL METRICS
   Total files analyzed: 45
   Average disharmony: 0.52
   Codebase health: MODERATE ⚠️

🚨 ARCHITECTURAL SMELLS (8 detected)
   β€’ God File: main.py (82 functions)
   β€’ High Disharmony: legacy_module.py (avg: 0.95)
   β€’ Mixed Concerns: utils.py (4 semantic dimensions active)

πŸ’° ARCHITECTURAL DEBT ESTIMATION
   Total Estimated Debt: 127.5 hours ($19,125)

   HIGH (6 files) - 89.5hrs ($13,425):
     β€’ legacy_module.py: $4,500 (High Disharmony + Semantic Confusion)
     β€’ utils.py: $3,300 (God File + Mixed Concerns)

Advanced Features

Git History & Semantic Drift:

# Track how code evolved semantically
python -m harmonizer.legacy_mapper <path> --drift-timeline

Shows which files changed semantically over time, stability scores, and dimension-specific drift.

Architecture Documentation Check:

# Compare docs vs reality
python -m harmonizer.legacy_mapper <path> --docs-path README.md

Validates whether your documentation matches actual implementation.

Visualizations:

# All visualizations
python -m harmonizer.legacy_mapper <path> --semantic-map --debt-breakdown --export-html

Generates:

  • 3D semantic space map (ASCII)
  • Detailed debt breakdown with cost analysis
  • Interactive HTML visualization (open in browser)

Use Cases

  1. Understanding Legacy Code: Quickly grasp architecture and identify problem areas
  2. Refactoring Planning: Prioritize what to fix first based on impact and cost
  3. Code Review: Identify semantic issues before they become bugs
  4. Technical Debt: Quantify and communicate debt to stakeholders
  5. Onboarding: Help new developers understand codebase structure

What's New in v2.0

Major Enhancements

1. Mathematical Proof (MATHEMATICAL_FOUNDATION.md)

  • Proves LJPW forms complete semantic basis
  • Establishes theoretical foundation for semantic debugging
  • Information-theoretic and categorical perspectives

2. Programming Language Theory (PROGRAMMING_LANGUAGE_SEMANTICS.md)

  • Comprehensive 1000+ line framework
  • Proves all code operations map to LJPW
  • Demonstrates all four dimensions necessary for functional code

3. Enhanced Parser (V2) - 7.4x More Powerful

  • 184 programming verbs mapped (vs 25 in V1)
  • Compound pattern detection (get_user, send_notification)
  • Context-aware semantic analysis
  • 100% backward compatible

4. Semantic Programming Language (SEMANTIC_PROGRAMMING_LANGUAGE.md)

  • Vision for future: languages with semantic types as first-class citizens
  • Compile-time semantic verification
  • Revolutionary approach to eliminating entire bug classes

Test Results

Suite Tests Status
Enhanced Parser 8 βœ… 100%
Language Semantics 9 βœ… 100%
Integration 6 βœ… 100%
Legacy (pytest) 59 βœ… 100%
Total 82 βœ… 100%

Advanced Features

Semantic Trajectory Maps

See exactly WHERE code drifts in 4D semantic space:

harmonizer mycode.py --show-trajectories
delete_user: !! DISHARMONY (1.41)

πŸ“ SEMANTIC TRAJECTORY MAP:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Dimension    Intent   Execution   Ξ”           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Power (P)    1.00  β†’  0.00     -1.00  ⚠️      β”‚
β”‚ Wisdom (W)   0.00  β†’  1.00     +1.00  ⚠️      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ’‘ INTERPRETATION:
   Name suggests Power (transformation/control)
   but code operates in Wisdom domain (analysis/understanding)

Semantic Naming Suggestions

Get intelligent name suggestions based on what code actually does:

harmonizer mycode.py --suggest-names
delete_user: !! DISHARMONY (1.22)

πŸ’‘ SUGGESTED NAMES (based on execution semantics):
   Function emphasizes: 50% Love, 50% Wisdom

   Suggestions:
   β€’ notify_user         (match: 85%)
   β€’ inform_user         (match: 82%)
   β€’ communicate_user    (match: 80%)

Configuration

Create .harmonizer.yml in your project:

# Exclude files/directories
exclude:
  - "tests/**"
  - "venv/**"
  - "*.pyc"

# Custom vocabulary for your domain
custom_vocabulary:
  authenticate: justice
  serialize: wisdom
  broadcast: love
  execute: power

# Adjust threshold
disharmony_threshold: 0.6

See CONFIGURATION.md for full options.


Integration

GitHub Actions CI/CD

# .github/workflows/harmony-check.yml
- name: Check Code Harmony
  run: |
    pip install /path/to/Python-Code-Harmonizer
    harmonizer src/**/*.py

Pre-commit Hook

# .pre-commit-config.yaml
- repo: local
  hooks:
    - id: harmonizer
      name: Python Code Harmonizer
      entry: harmonizer
      language: system
      types: [python]

VS Code

Press Ctrl+Shift+P β†’ Tasks: Run Task β†’ Harmonizer: Check Current File

See integration templates for full setup.


How Is This Different?

Traditional tools check different things:

  • Pylint/Flake8: Style and patterns
  • MyPy: Type safety
  • Pytest: Test correctness
  • Bandit: Security vulnerabilities

Python Code Harmonizer checks: Does your code mean what it says?

It's the only tool that:

  1. βœ… Has a mathematically proven foundation
  2. βœ… Analyzes semantic meaning, not just syntax
  3. βœ… Detects when function names lie about implementation
  4. βœ… Maps code to universal semantic dimensions
  5. βœ… Finds bugs that pass all other checks

Complementary, not competitive - use it alongside existing tools for comprehensive code quality.


Documentation

Getting Started

Deep Dive

Examples


Contributing

We welcome contributions! Whether you:

  • Found a bug
  • Want to add features
  • See deeper patterns
  • Have questions or ideas

See CONTRIBUTING.md for guidelines.

Development setup:

git clone https://github.com/BruinGrowly/Python-Code-Harmonizer.git
cd Python-Code-Harmonizer
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install -e .
pytest  # Run tests

The Science

Peer-Reviewed Foundation

This isn't just a tool - it's applied philosophy with mathematical rigor:

  1. Orthogonality Proof: L, J, P, W are linearly independent
  2. Completeness Proof: They span all semantic meaning
  3. Minimality Proof: All four are necessary
  4. Empirical Validation: 0.000 error on controlled tests

See MATHEMATICAL_FOUNDATION.md for complete proofs.

Cross-Language Universal

The LJPW framework applies beyond Python:

  • JavaScript: Same patterns in async/await, promises, callbacks
  • Rust: Ownership system maps to Power/Justice dimensions
  • Go: Channels and goroutines map to Love (connection)
  • Any language: All use these four semantic primitives

Future: Harmonizer for JavaScript, Rust, Go, and more.


Real-World Impact

Case Study: Self-Analysis

The Harmonizer found its own semantic bugs during meta-analysis:

Before:

def visit_Raise(self, node):
    """Process raise statements"""
    self._concepts_found.add('raise')  # Bug: Unclear semantics

Analysis: Function name suggests Power (visiting/processing) but used generic "add" (ambiguous).

After Refactoring:

def visit_Raise(self, node):
    """Process raise statements"""
    self._record_concept('raise')  # Clear: recording for analysis

Result: Disharmony score improved from 1.18 β†’ 0.22

See META_ANALYSIS_V2.md for full story.


Philosophy

The Anchor Point (1,1,1,1) represents perfect harmony - the ideal where all four dimensions align perfectly.

Real code approaches this ideal but rarely reaches it. The goal isn't perfection - it's movement toward harmony.

Core insight:

When Intent aligns with Execution, you have harmony. When they contradict, you have disharmony. High disharmony predicts bugs.

This isn't metaphor. It's measurable, testable, mathematical structure.


License

MIT License - see LICENSE for details.


Citation

If you use this tool in research or production:

@software{python_code_harmonizer,
  title = {Python Code Harmonizer: Semantic Code Debugging with Mathematical Foundation},
  author = {BruinGrowly},
  year = {2025},
  version = {2.0},
  url = {https://github.com/BruinGrowly/Python-Code-Harmonizer},
  note = {First semantic debugger with proven LJPW basis}
}

Getting Help


May your code say what it means, and mean what it says. πŸ’›βš“


Quick Links

Start Here: Quick Reference | User Guide | Tutorial | UX Guide ✨

Theory: Philosophy | Math Foundation | Language Semantics

Examples: Real Bugs | Refactoring | Realistic Samples

Project: Changelog | Contributing | License | UX Design