| Conditions | 9 |
| Total Lines | 99 |
| Code Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| 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 | import itertools |
||
| 10 | def repo_graph_model(): # noqa: C901 |
||
| 11 | """ |
||
| 12 | Create a graph model for GitHub repositories. |
||
| 13 | |||
| 14 | Returns: |
||
| 15 | GraphModel: A graph model representing GitHub repositories with nodes and edges. |
||
| 16 | """ |
||
| 17 | |||
| 18 | graph_model = graphinate.model(name='GitHub Repository Graph') |
||
| 19 | |||
| 20 | @graph_model.edge() |
||
| 21 | def github(user_id: str | None = None, |
||
| 22 | repository_id: str | None = None, |
||
| 23 | commit_id: str | None = None, |
||
| 24 | file_id: str | None = None, |
||
| 25 | **kwargs): |
||
| 26 | user = github_user(user_id) |
||
| 27 | for repo in github_repositories(user_id, repository_id): |
||
| 28 | yield {'source': (user.login,), 'target': (user.login, repo.name)} |
||
| 29 | for commit in github_commits(repo, commit_id): |
||
| 30 | yield { |
||
| 31 | 'source': (user.login, repo.name), |
||
| 32 | 'target': (user.login, repo.name, commit.sha) |
||
| 33 | } |
||
| 34 | for file in github_files(commit, file_id): |
||
| 35 | yield { |
||
| 36 | 'source': (user.login, repo.name, commit.sha), |
||
| 37 | 'target': (user.login, repo.name, commit.sha, file.filename) |
||
| 38 | } |
||
| 39 | |||
| 40 | user_node = graph_model.node(key=operator.attrgetter('login'), |
||
| 41 | value=operator.attrgetter('raw_data'), |
||
| 42 | label=operator.itemgetter('name')) |
||
| 43 | |||
| 44 | repository_node = graph_model.node(parent_type='user', |
||
| 45 | key=operator.attrgetter('name'), |
||
| 46 | value=operator.attrgetter('raw_data'), |
||
| 47 | label=operator.itemgetter('name')) |
||
| 48 | |||
| 49 | def commit_label(commit): |
||
| 50 | return commit['sha'][-7:] |
||
| 51 | |||
| 52 | commit_node = graph_model.node(parent_type='repository', |
||
| 53 | key=operator.attrgetter('sha'), |
||
| 54 | value=operator.attrgetter('raw_data'), |
||
| 55 | label=commit_label) |
||
| 56 | |||
| 57 | file_node = graph_model.node(parent_type='commit', |
||
| 58 | unique=True, |
||
| 59 | key=operator.attrgetter('filename'), |
||
| 60 | value=operator.attrgetter('raw_data'), |
||
| 61 | label=operator.itemgetter('filename')) |
||
| 62 | |||
| 63 | @user_node |
||
| 64 | def user(user_id: str | None = None, **kwargs): |
||
| 65 | yield github_user(user_id) |
||
| 66 | |||
| 67 | @repository_node |
||
| 68 | def repository(user_id: str | None = None, |
||
| 69 | repository_id: str | None = None, |
||
| 70 | **kwargs): |
||
| 71 | repos = github_repositories(user_id, repository_id) |
||
| 72 | yield from repos |
||
| 73 | |||
| 74 | @commit_node |
||
| 75 | def commit(user_id: str | None = None, |
||
| 76 | repository_id: str | None = None, |
||
| 77 | commit_id: str | None = None, |
||
| 78 | **kwargs): |
||
| 79 | for repo in github_repositories(user_id, repository_id): |
||
| 80 | yield from github_commits(repo, commit_id) |
||
| 81 | |||
| 82 | def file_type(user_id: str | None = None, |
||
| 83 | repository_id: str | None = None, |
||
| 84 | commit_id: str | None = None, |
||
| 85 | file_type_id: str | None = None, |
||
| 86 | **kwargs): |
||
| 87 | def group_key(file): |
||
| 88 | return pathlib.PurePath(file).suffix |
||
| 89 | |||
| 90 | for repo in github_repositories(user_id, repository_id): |
||
| 91 | for commit in github_commits(repo, commit_id): |
||
| 92 | yield from ((k, list(g)) for k, g in |
||
| 93 | itertools.groupby( |
||
| 94 | sorted(github_files(commit), |
||
| 95 | key=group_key), group_key |
||
| 96 | )) |
||
| 97 | |||
| 98 | @file_node |
||
| 99 | def file(user_id: str | None = None, |
||
| 100 | repository_id: str | None = None, |
||
| 101 | commit_id: str | None = None, |
||
| 102 | file_id: str | None = None, |
||
| 103 | **kwargs): |
||
| 104 | for repo in github_repositories(user_id, repository_id): |
||
| 105 | for commit in github_commits(repo, commit_id): |
||
| 106 | yield from github_files(commit, file_id) |
||
| 107 | |||
| 108 | return graph_model |
||
| 109 | |||
| 126 |