Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from dataclasses import dataclass
- from typing import Iterable, Dict
- import json
- import time
- File = str # typedef для имен файла ("явное лучше неявного" python-zen)
- Username = str
- Time = time.struct_time
- @dataclass
- class Employee:
- username: str
- commits: int
- changed_lines: int
- new_files: int
- @dataclass
- class FileChanges:
- name: File
- changed_lines: int
- @dataclass
- class Commit:
- username: Username
- commit_time: Time
- files: Iterable[FileChanges]
- def parse_file(file: dict) -> FileChanges:
- return FileChanges(
- name=file["name"],
- changed_lines=int(file["changed_lines"])
- )
- def parse_commit(data: dict) -> Commit:
- return Commit(
- username=data["username"],
- commit_time=time.strptime(data["commit_time"], "%Y/%m/%d %H:%M:%S"),
- files=tuple(parse_file(file) for file in data["files"])
- )
- def get_commits_from_json(filename: str) -> Iterable[Commit]:
- with open(filename, "r") as json_file:
- data = json.load(json_file)
- return map(parse_commit, data)
- def get_usernames(commits: Iterable[Commit]) -> Iterable[Username]:
- return set(commit.username for commit in commits)
- def get_file_creators(commits: Iterable[Commit]) -> Dict[File, Username]:
- file_creators = dict()
- for commit in sorted(commits, key=lambda commit: commit.commit_time):
- for file in commit.files:
- if file.name not in file_creators:
- file_creators[file.name] = commit.username
- return file_creators
- def get_changed_lines(commits: Iterable[Commit]) -> int:
- changed_lines = 0
- for commit in commits:
- changed_lines += sum(file.changed_lines for file in commit.files)
- return changed_lines
- def get_employee(username: Username, commits: Iterable[Commit], file_creators: Dict[File, Username]) -> Employee:
- his_commits = tuple(commit for commit in commits if commit.username == username)
- return Employee(
- username=username,
- commits=len(his_commits),
- changed_lines=get_changed_lines(his_commits),
- new_files=sum(creator == username for creator in file_creators.values())
- )
- commits = tuple(get_commits_from_json("./input.json"))
- usernames = get_usernames(commits)
- file_creators = get_file_creators(commits)
- employeers = []
- for username in usernames:
- employeers.append(get_employee(username, commits, file_creators))
- print(employeers)
- header = ["username", "commits", "changed_lines", "new_files"]
- with open("output.tsv", "w") as file:
- file.write("{}\n".format("\t".join(header)))
- for employee in sorted(employeers, key=lambda el: el.username):
- file.write("{}\n".format("\t".join(map(str, [
- employee.username,
- employee.commits,
- employee.changed_lines,
- employee.new_files
- ]))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement