Spaces:
Runtime error
Runtime error
add graph to CodeImportsAnalyzer using dependency injection
Browse files- README.md +1 -0
- py_code_analyzer/code_imports_analyzer.py +8 -7
- py_code_analyzer/graph_analyzer.py +19 -0
README.md
CHANGED
|
@@ -53,3 +53,4 @@ input to one GitHub public repo's URL(input)
|
|
| 53 |
## References
|
| 54 |
|
| 55 |
- [How to set up a perfect Python project](https://sourcery.ai/blog/python-best-practices/)
|
|
|
|
|
|
| 53 |
## References
|
| 54 |
|
| 55 |
- [How to set up a perfect Python project](https://sourcery.ai/blog/python-best-practices/)
|
| 56 |
+
- [Benchmark of popular graph/network packages](https://www.timlrx.com/blog/benchmark-of-popular-graph-network-packages)
|
py_code_analyzer/code_imports_analyzer.py
CHANGED
|
@@ -3,9 +3,10 @@ to get what modules are imported in given python files, then uses networkx to ge
|
|
| 3 |
"""
|
| 4 |
import ast
|
| 5 |
|
| 6 |
-
import networkx as nx
|
| 7 |
import requests
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
class CodeImportsAnalyzer:
|
| 11 |
class _NodeVisitor(ast.NodeVisitor):
|
|
@@ -28,7 +29,7 @@ class CodeImportsAnalyzer:
|
|
| 28 |
|
| 29 |
def __init__(self, python_files):
|
| 30 |
self.python_imports = []
|
| 31 |
-
self.
|
| 32 |
self.python_files = python_files
|
| 33 |
self._node_visitor = CodeImportsAnalyzer._NodeVisitor(self.python_imports)
|
| 34 |
|
|
@@ -48,9 +49,9 @@ class CodeImportsAnalyzer:
|
|
| 48 |
|
| 49 |
def _add_edges(self, nodes):
|
| 50 |
for first_node, second_node in zip(nodes, nodes[1:]):
|
| 51 |
-
self.
|
| 52 |
-
self.
|
| 53 |
-
self.
|
| 54 |
|
| 55 |
def generate_imports_graph(self):
|
| 56 |
for python_import in self.python_imports:
|
|
@@ -66,7 +67,7 @@ class CodeImportsAnalyzer:
|
|
| 66 |
del _nodes[-1]
|
| 67 |
self._add_edges(_nodes)
|
| 68 |
else:
|
| 69 |
-
self.
|
| 70 |
|
| 71 |
# generate graph based on imported modules in each file
|
| 72 |
if python_import["file_name"] != "__init__.py":
|
|
@@ -82,7 +83,7 @@ class CodeImportsAnalyzer:
|
|
| 82 |
_new_nodes = _import_names + [_nodes[-1]]
|
| 83 |
self._add_edges(_new_nodes)
|
| 84 |
|
| 85 |
-
return self.
|
| 86 |
|
| 87 |
def report(self):
|
| 88 |
from pprint import pprint
|
|
|
|
| 3 |
"""
|
| 4 |
import ast
|
| 5 |
|
|
|
|
| 6 |
import requests
|
| 7 |
|
| 8 |
+
from .graph_analyzer import GraphAnalyzer
|
| 9 |
+
|
| 10 |
|
| 11 |
class CodeImportsAnalyzer:
|
| 12 |
class _NodeVisitor(ast.NodeVisitor):
|
|
|
|
| 29 |
|
| 30 |
def __init__(self, python_files):
|
| 31 |
self.python_imports = []
|
| 32 |
+
self.graph_analyzer = GraphAnalyzer(is_directed=True)
|
| 33 |
self.python_files = python_files
|
| 34 |
self._node_visitor = CodeImportsAnalyzer._NodeVisitor(self.python_imports)
|
| 35 |
|
|
|
|
| 49 |
|
| 50 |
def _add_edges(self, nodes):
|
| 51 |
for first_node, second_node in zip(nodes, nodes[1:]):
|
| 52 |
+
self.graph_analyzer.add_node(first_node, color="gray")
|
| 53 |
+
self.graph_analyzer.add_node(second_node, color="gray")
|
| 54 |
+
self.graph_analyzer.add_edge(first_node, second_node)
|
| 55 |
|
| 56 |
def generate_imports_graph(self):
|
| 57 |
for python_import in self.python_imports:
|
|
|
|
| 67 |
del _nodes[-1]
|
| 68 |
self._add_edges(_nodes)
|
| 69 |
else:
|
| 70 |
+
self.graph_analyzer.add_node(_nodes[0])
|
| 71 |
|
| 72 |
# generate graph based on imported modules in each file
|
| 73 |
if python_import["file_name"] != "__init__.py":
|
|
|
|
| 83 |
_new_nodes = _import_names + [_nodes[-1]]
|
| 84 |
self._add_edges(_new_nodes)
|
| 85 |
|
| 86 |
+
return self.graph_analyzer.graph
|
| 87 |
|
| 88 |
def report(self):
|
| 89 |
from pprint import pprint
|
py_code_analyzer/graph_analyzer.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import networkx as nx
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class GraphAnalyzer:
|
| 5 |
+
def __init__(self, is_directed: bool = False, is_multi_edges: bool = False):
|
| 6 |
+
if not is_directed and not is_multi_edges:
|
| 7 |
+
self.graph = nx.Graph()
|
| 8 |
+
elif is_directed and not is_multi_edges:
|
| 9 |
+
self.graph = nx.DiGraph()
|
| 10 |
+
elif not is_directed and is_multi_edges:
|
| 11 |
+
self.graph = nx.MultiGraph()
|
| 12 |
+
else:
|
| 13 |
+
self.graph = nx.MultiDiGraph()
|
| 14 |
+
|
| 15 |
+
def add_node(self, node, **kwargs):
|
| 16 |
+
self.graph.add_node(node, **kwargs)
|
| 17 |
+
|
| 18 |
+
def add_edge(self, first_node, second_node):
|
| 19 |
+
self.graph.add_edge(first_node, second_node)
|