Team Ai
Apppublic

melghorab/code-assistant

sourceHugging Faceopenrailupdated 2y agoView on Hugging Face
0likes
code_analysis.py31 linesDownload Raw Back to root
1 2def read_python_file(file_path):3    try:4        with open(file_path, 'r', encoding='utf-8') as f:5            content = f.read()6        return content7    except FileNotFoundError:8        print(f"File not found: {file_path}")9        raise10    except IOError as e:11        print(f"Error reading file {file_path}: {str(e)}")12        raise13    except Exception as e:14        print(f"Unexpected error reading file {file_path}: {str(e)}")15        raise16 17def extract_imports(code, file_path):18    try:19            20        # Split into lines and find imports21        import_lines = []22        for line in code.split('\n'):23            line = line.strip()24            if line.startswith('import ') or line.startswith('from '):25                import_lines.append(line)26                27        return import_lines28        29    except Exception as e:30        print(f"Error extracting imports from file {file_path}: {str(e)}")31        return []