help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
code-assistant-bot
/
static
/
css
RSK World
code-assistant-bot
Code Assistant Bot - Python + Flask + OpenAI API + Code Generation + Debugging + Code Analysis + GitHub Integration
css
  • style.css9 KB
code_comparator.pycode_documenter.py
utils/code_comparator.py
Raw Download
Find: Go to:
"""
Code Comparator Utility for Code Assistant Bot
Author: RSK World (https://rskworld.in)
Founder: Molla Samser
Designer & Tester: Rima Khatun
Contact: help@rskworld.in, +91 93305 39277
Year: 2026
"""

import difflib
from typing import Dict, List, Any, Tuple

class CodeComparator:
    """
    Utility class for comparing code differences
    """
    
    def __init__(self):
        pass
    
    def compare_code(self, code1: str, code2: str, language: str = 'auto') -> Dict[str, Any]:
        """
        Compare two code snippets and show differences
        """
        try:
            lines1 = code1.splitlines(keepends=True)
            lines2 = code2.splitlines(keepends=True)
            
            # Use difflib to find differences
            diff = list(difflib.unified_diff(
                lines1, lines2,
                fromfile='Original',
                tofile='Modified',
                lineterm=''
            ))
            
            # Calculate statistics
            added_lines = sum(1 for line in diff if line.startswith('+') and not line.startswith('+++'))
            removed_lines = sum(1 for line in diff if line.startswith('-') and not line.startswith('---'))
            modified_blocks = self._count_modified_blocks(diff)
            
            # Generate HTML diff
            html_diff = self._generate_html_diff(lines1, lines2)
            
            # Generate side-by-side comparison
            side_by_side = self._generate_side_by_side(lines1, lines2)
            
            return {
                'success': True,
                'diff': diff,
                'html_diff': html_diff,
                'side_by_side': side_by_side,
                'statistics': {
                    'total_lines_original': len(lines1),
                    'total_lines_modified': len(lines2),
                    'lines_added': added_lines,
                    'lines_removed': removed_lines,
                    'lines_unchanged': len(lines1) - removed_lines,
                    'modified_blocks': modified_blocks,
                    'similarity_ratio': difflib.SequenceMatcher(None, code1, code2).ratio()
                }
            }
            
        except Exception as e:
            return {
                'success': False,
                'error': f'Code comparison failed: {str(e)}',
                'diff': [],
                'statistics': {}
            }
    
    def _count_modified_blocks(self, diff: List[str]) -> int:
        """Count number of modified blocks in diff"""
        blocks = 0
        in_block = False
        
        for line in diff:
            if line.startswith('@@'):
                if in_block:
                    blocks += 1
                in_block = True
            elif line.startswith(' ') and in_block:
                in_block = False
        
        if in_block:
            blocks += 1
        
        return blocks
    
    def _generate_html_diff(self, lines1: List[str], lines2: List[str]) -> str:
        """Generate HTML formatted diff"""
        diff = list(difflib.unified_diff(
            lines1, lines2,
            fromfile='Original',
            tofile='Modified',
            lineterm=''
        ))
        
        html = '<div class="diff-container">\n'
        html += '<pre class="diff">\n'
        
        for line in diff:
            if line.startswith('+++') or line.startswith('---'):
                html += f'<span class="diff-header">{line}</span>\n'
            elif line.startswith('@@'):
                html += f'<span class="diff-hunk">{line}</span>\n'
            elif line.startswith('+'):
                html += f'<span class="diff-added">{line}</span>\n'
            elif line.startswith('-'):
                html += f'<span class="diff-removed">{line}</span>\n'
            else:
                html += f'<span class="diff-context">{line}</span>\n'
        
        html += '</pre>\n'
        html += '</div>\n'
        
        return html
    
    def _generate_side_by_side(self, lines1: List[str], lines2: List[str]) -> List[Dict[str, Any]]:
        """Generate side-by-side comparison"""
        matcher = difflib.SequenceMatcher(None, lines1, lines2)
        comparison = []
        
        for tag, i1, i2, j1, j2 in matcher.get_opcodes():
            if tag == 'equal':
                for i in range(i1, i2):
                    comparison.append({
                        'type': 'equal',
                        'left': lines1[i],
                        'right': lines2[i1 + (i - i1)],
                        'line_number_left': i + 1,
                        'line_number_right': i1 + (i - i1) + 1
                    })
            elif tag == 'delete':
                for i in range(i1, i2):
                    comparison.append({
                        'type': 'delete',
                        'left': lines1[i],
                        'right': '',
                        'line_number_left': i + 1,
                        'line_number_right': None
                    })
            elif tag == 'insert':
                for j in range(j1, j2):
                    comparison.append({
                        'type': 'insert',
                        'left': '',
                        'right': lines2[j],
                        'line_number_left': None,
                        'line_number_right': j + 1
                    })
            elif tag == 'replace':
                max_len = max(i2 - i1, j2 - j1)
                for k in range(max_len):
                    comparison.append({
                        'type': 'replace',
                        'left': lines1[i1 + k] if k < (i2 - i1) else '',
                        'right': lines2[j1 + k] if k < (j2 - j1) else '',
                        'line_number_left': i1 + k + 1 if k < (i2 - i1) else None,
                        'line_number_right': j1 + k + 1 if k < (j2 - j1) else None
                    })
        
        return comparison
164 lines•6 KB
python
utils/code_documenter.py
Raw Download
Find: Go to:
"""
Code Documentation Generator Utility for Code Assistant Bot
Author: RSK World (https://rskworld.in)
Founder: Molla Samser
Designer & Tester: Rima Khatun
Contact: help@rskworld.in, +91 93305 39277
Year: 2026
"""

import re
import ast
from typing import Dict, List, Any, Optional

class CodeDocumenter:
    """
    Utility class for generating code documentation
    """
    
    def __init__(self):
        self.supported_languages = [
            'python', 'javascript', 'typescript', 'java', 'cpp', 'c',
            'csharp', 'php', 'ruby', 'go', 'rust'
        ]
    
    def generate_documentation(self, code: str, language: str, doc_style: str = 'standard') -> Dict[str, Any]:
        """
        Generate documentation for code
        """
        try:
            if language not in self.supported_languages:
                return {
                    'success': False,
                    'error': f'Language {language} not supported for documentation',
                    'documented_code': code
                }
            
            if language == 'python':
                return self._document_python(code, doc_style)
            elif language in ['javascript', 'typescript']:
                return self._document_javascript(code, doc_style)
            elif language == 'java':
                return self._document_java(code, doc_style)
            else:
                return self._document_generic(code, language, doc_style)
                
        except Exception as e:
            return {
                'success': False,
                'error': f'Documentation generation failed: {str(e)}',
                'documented_code': code
            }
    
    def _document_python(self, code: str, doc_style: str) -> Dict[str, Any]:
        """Generate Python documentation"""
        try:
            documented_code = code
            changes = []
            
            # Parse the code
            try:
                tree = ast.parse(code)
                
                # Find functions and classes without docstrings
                for node in ast.walk(tree):
                    if isinstance(node, ast.FunctionDef):
                        if not ast.get_docstring(node):
                            # Generate docstring
                            func_name = node.name
                            params = [arg.arg for arg in node.args.args]
                            
                            docstring = f'    """\n'
                            docstring += f'    {self._generate_function_description(func_name, params)}\n'
                            docstring += f'\n'
                            
                            if params:
                                docstring += f'    Args:\n'
                                for param in params:
                                    if param != 'self':
                                        docstring += f'        {param}: Description of {param}\n'
                            
                            # Check for return statement
                            has_return = any(isinstance(n, ast.Return) for n in ast.walk(node))
                            if has_return:
                                docstring += f'\n    Returns:\n'
                                docstring += f'        Description of return value\n'
                            
                            docstring += f'    """'
                            
                            # Find the line after function definition
                            func_line = node.lineno - 1
                            lines = documented_code.split('\n')
                            
                            # Insert docstring after function definition
                            if func_line < len(lines):
                                indent = len(lines[func_line]) - len(lines[func_line].lstrip())
                                docstring_lines = docstring.split('\n')
                                indented_docstring = '\n'.join([' ' * indent + line.lstrip() for line in docstring_lines])
                                
                                lines.insert(func_line + 1, indented_docstring)
                                documented_code = '\n'.join(lines)
                                changes.append(f'Added docstring to function {func_name}')
                    
                    elif isinstance(node, ast.ClassDef):
                        if not ast.get_docstring(node):
                            # Generate class docstring
                            class_name = node.name
                            docstring = f'    """\n'
                            docstring += f'    {self._generate_class_description(class_name)}\n'
                            docstring += f'    """'
                            
                            class_line = node.lineno - 1
                            lines = documented_code.split('\n')
                            
                            if class_line >= 0 and class_line < len(lines):
                                indent = len(lines[class_line]) - len(lines[class_line].lstrip())
                                docstring_lines = docstring.split('\n')
                                indented_docstring = '\n'.join([' ' * indent + line.lstrip() for line in docstring_lines])
                                
                                lines.insert(class_line + 1, indented_docstring)
                                documented_code = '\n'.join(lines)
                                changes.append(f'Added docstring to class {class_name}')
            
            except SyntaxError:
                # If parsing fails, add basic comments
                documented_code = self._add_basic_comments(code)
                changes.append('Added basic comments (code parsing failed)')
            
            return {
                'success': True,
                'documented_code': documented_code,
                'changes_made': changes if changes else ['Code already has documentation']
            }
            
        except Exception as e:
            return {
                'success': False,
                'error': f'Python documentation failed: {str(e)}',
                'documented_code': code
            }
    
    def _document_javascript(self, code: str, doc_style: str) -> Dict[str, Any]:
        """Generate JavaScript/TypeScript documentation"""
        try:
            documented_code = code
            changes = []
            
            # Find functions without JSDoc comments
            function_pattern = r'(function\s+(\w+)\s*\([^)]*\)|const\s+(\w+)\s*=\s*(?:\([^)]*\)|function\s*\([^)]*\))\s*=>)'
            
            def add_jsdoc(match):
                func_match = match.group(0)
                func_name = match.group(2) if match.lastindex >= 2 and match.group(2) else (match.group(3) if match.lastindex >= 3 and match.group(3) else 'function')
                
                # Extract parameters
                params_match = re.search(r'\(([^)]*)\)', func_match)
                params = [p.strip().split('=')[0].strip() for p in params_match.group(1).split(',') if p.strip()] if params_match and params_match.group(1) else []
                
                jsdoc = '/**\n'
                jsdoc += f' * {self._generate_function_description(func_name, params)}\n'
                
                if params:
                    for param in params:
                        if param:
                            jsdoc += f' * @param {{type}} {param} - Description of {param}\n'
                
                jsdoc += ' * @returns {type} Description of return value\n'
                jsdoc += ' */\n'
                
                changes.append(f'Added JSDoc to function {func_name}')
                return jsdoc + func_match
            
            documented_code = re.sub(function_pattern, add_jsdoc, documented_code)
            
            return {
                'success': True,
                'documented_code': documented_code,
                'changes_made': changes if changes else ['Code already has documentation']
            }
            
        except Exception as e:
            return {
                'success': False,
                'error': f'JavaScript documentation failed: {str(e)}',
                'documented_code': code
            }
    
    def _document_java(self, code: str, doc_style: str) -> Dict[str, Any]:
        """Generate Java documentation"""
        try:
            documented_code = code
            changes = []
            
            # Find methods without JavaDoc comments
            method_pattern = r'(public|private|protected)?\s*(static)?\s*(\w+)\s+(\w+)\s*\([^)]*\)'
            
            def add_javadoc(match):
                method_name = match.group(4)
                return_type = match.group(3)
                
                javadoc = '/**\n'
                javadoc += f' * {self._generate_function_description(method_name, [])}\n'
                javadoc += ' *\n'
                
                # Extract parameters
                params_match = re.search(r'\(([^)]*)\)', match.group(0))
                if params_match:
                    params = [p.strip().split()[1] if len(p.strip().split()) > 1 else p.strip() 
                             for p in params_match.group(1).split(',') if p.strip()]
                    for param in params:
                        javadoc += f' * @param {param} Description of {param}\n'
                
                javadoc += f' * @return Description of return value\n'
                javadoc += ' */\n'
                
                changes.append(f'Added JavaDoc to method {method_name}')
                return javadoc + match.group(0)
            
            documented_code = re.sub(method_pattern, add_javadoc, documented_code)
            
            return {
                'success': True,
                'documented_code': documented_code,
                'changes_made': changes if changes else ['Code already has documentation']
            }
            
        except Exception as e:
            return {
                'success': False,
                'error': f'Java documentation failed: {str(e)}',
                'documented_code': code
            }
    
    def _document_generic(self, code: str, language: str, doc_style: str) -> Dict[str, Any]:
        """Generic documentation for other languages"""
        try:
            documented_code = self._add_basic_comments(code)
            
            return {
                'success': True,
                'documented_code': documented_code,
                'changes_made': ['Added basic comments']
            }
        except Exception as e:
            return {
                'success': False,
                'error': f'Documentation failed: {str(e)}',
                'documented_code': code
            }
    
    def _generate_function_description(self, func_name: str, params: List[str]) -> str:
        """Generate a description for a function"""
        # Simple heuristic-based description generation
        if 'get' in func_name.lower():
            return f'Gets {func_name.replace("get", "").lower()}'
        elif 'set' in func_name.lower():
            return f'Sets {func_name.replace("set", "").lower()}'
        elif 'is' in func_name.lower() or 'has' in func_name.lower():
            return f'Checks if {func_name.lower()}'
        elif 'create' in func_name.lower() or 'make' in func_name.lower():
            return f'Creates {func_name.lower()}'
        elif 'delete' in func_name.lower() or 'remove' in func_name.lower():
            return f'Removes {func_name.lower()}'
        else:
            return f'Performs {func_name} operation'
    
    def _generate_class_description(self, class_name: str) -> str:
        """Generate a description for a class"""
        return f'Represents a {class_name.lower()}'
    
    def _add_basic_comments(self, code: str) -> str:
        """Add basic comments to code"""
        lines = code.split('\n')
        commented_lines = []
        
        for i, line in enumerate(lines):
            stripped = line.strip()
            
            # Add comment before function/class definitions
            if stripped.startswith(('def ', 'class ', 'function ', 'class ')):
                if i > 0 and not lines[i-1].strip().startswith('#'):
                    indent = len(line) - len(line.lstrip())
                    commented_lines.append(' ' * indent + f'# {stripped}')
                else:
                    commented_lines.append(line)
            else:
                commented_lines.append(line)
        
        return '\n'.join(commented_lines)
289 lines•12.8 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer