| Conditions | 3 |
| Total Lines | 55 |
| Code Lines | 34 |
| Lines | 55 |
| Ratio | 100 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | """ |
||
| 46 | View Code Duplication | def ast_graph_model(): |
|
| 47 | """ |
||
| 48 | Create an abstract syntax tree (AST) graph model. |
||
| 49 | |||
| 50 | Returns: |
||
| 51 | GraphModel: A graph model representing the AST nodes and their relationships. |
||
| 52 | """ |
||
| 53 | |||
| 54 | code_object = graphinate.builders.D3Builder |
||
| 55 | |||
| 56 | graph_model = graphinate.model(name=f'AST Graph - {code_object.__qualname__}',) |
||
| 57 | |||
| 58 | root_ast_node = ast.parse(inspect.getsource(code_object)) |
||
| 59 | |||
| 60 | def node_type(ast_node): |
||
| 61 | return ast_node.__class__.__name__ |
||
| 62 | |||
| 63 | def node_label(ast_node) -> str: |
||
| 64 | label = ast_node.__class__.__name__ |
||
| 65 | |||
| 66 | for field_name in ('name', 'id'): |
||
| 67 | if field_name in ast_node._fields: |
||
| 68 | value = operator.attrgetter(field_name)(ast_node) |
||
| 69 | label = f"{label}\n{field_name}: {value}" |
||
| 70 | |||
| 71 | return label |
||
| 72 | |||
| 73 | def key(value): |
||
| 74 | # noinspection InsecureHash |
||
| 75 | return hashlib.shake_128(pickle.dumps(value)).hexdigest(20) |
||
| 76 | |||
| 77 | def endpoint(value, endpoint_name): |
||
| 78 | return key(value[endpoint_name]) |
||
| 79 | |||
| 80 | def source(value): |
||
| 81 | return endpoint(value, 'source') |
||
| 82 | |||
| 83 | def target(value): |
||
| 84 | return endpoint(value, 'target') |
||
| 85 | |||
| 86 | @graph_model.node(type_=node_type, |
||
| 87 | key=key, |
||
| 88 | label=node_label, |
||
| 89 | unique=True) |
||
| 90 | def ast_node(**kwargs): |
||
| 91 | yield from _ast_nodes([root_ast_node]) |
||
| 92 | |||
| 93 | @graph_model.edge(type_='edge', |
||
| 94 | source=source, |
||
| 95 | target=target, |
||
| 96 | label=operator.itemgetter('type')) |
||
| 97 | def ast_edge(**kwargs): |
||
| 98 | yield from _ast_edge(root_ast_node) |
||
| 99 | |||
| 100 | return graph_model |
||
| 101 | |||
| 149 |