| Conditions | 3 |
| Total Lines | 53 |
| Code Lines | 33 |
| Lines | 53 |
| 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 | graph_model = graphinate.model(name='AST Graph') |
||
| 55 | |||
| 56 | root_ast_node = ast.parse(inspect.getsource(graphinate.builders.D3Builder)) |
||
| 57 | |||
| 58 | def node_type(ast_node): |
||
| 59 | return ast_node.__class__.__name__ |
||
| 60 | |||
| 61 | def node_label(ast_node) -> str: |
||
| 62 | label = ast_node.__class__.__name__ |
||
| 63 | |||
| 64 | for field_name in ('name', 'id'): |
||
| 65 | if field_name in ast_node._fields: |
||
| 66 | value = operator.attrgetter(field_name)(ast_node) |
||
| 67 | label = f"{label}\n{field_name}: {value}" |
||
| 68 | |||
| 69 | return label |
||
| 70 | |||
| 71 | def key(value): |
||
| 72 | # noinspection InsecureHash |
||
| 73 | return hashlib.shake_128(pickle.dumps(value)).hexdigest(20) |
||
| 74 | |||
| 75 | def endpoint(value, endpoint_name): |
||
| 76 | return key(value[endpoint_name]) |
||
| 77 | |||
| 78 | def source(value): |
||
| 79 | return endpoint(value, 'source') |
||
| 80 | |||
| 81 | def target(value): |
||
| 82 | return endpoint(value, 'target') |
||
| 83 | |||
| 84 | @graph_model.node(type_=node_type, |
||
| 85 | key=key, |
||
| 86 | label=node_label, |
||
| 87 | unique=True) |
||
| 88 | def ast_node(**kwargs): |
||
| 89 | yield from _ast_nodes([root_ast_node]) |
||
| 90 | |||
| 91 | @graph_model.edge(type_='edge', |
||
| 92 | source=source, |
||
| 93 | target=target, |
||
| 94 | label=operator.itemgetter('type')) |
||
| 95 | def ast_edge(**kwargs): |
||
| 96 | yield from _ast_edge(root_ast_node) |
||
| 97 | |||
| 98 | return graph_model |
||
| 99 | |||
| 147 |