Advertisement
AlexG2230954

Untitled

Jun 10th, 2022
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. from dataclasses import dataclass
  2. from typing import Iterable, Dict
  3. import json
  4. import time
  5.  
  6.  
  7. File = str # typedef для имен файла ("явное лучше неявного" python-zen)
  8. Username = str
  9. Time = time.struct_time
  10.  
  11.  
  12. @dataclass
  13. class Employee:
  14.     username: str
  15.     commits: int
  16.     changed_lines: int
  17.     new_files: int
  18.  
  19.  
  20. @dataclass
  21. class FileChanges:
  22.     name: File
  23.     changed_lines: int
  24.  
  25.  
  26. @dataclass
  27. class Commit:
  28.     username: Username
  29.     commit_time: Time
  30.     files: Iterable[FileChanges]
  31.  
  32.  
  33. def parse_file(file: dict) -> FileChanges:
  34.     return FileChanges(
  35.         name=file["name"],
  36.         changed_lines=int(file["changed_lines"])
  37.     )
  38.  
  39.  
  40. def parse_commit(data: dict) -> Commit:
  41.     return Commit(
  42.         username=data["username"],
  43.         commit_time=time.strptime(data["commit_time"], "%Y/%m/%d %H:%M:%S"),
  44.         files=tuple(parse_file(file) for file in data["files"])
  45.     )
  46.  
  47.  
  48. def get_commits_from_json(filename: str) -> Iterable[Commit]:
  49.     with open(filename, "r") as json_file:
  50.         data = json.load(json_file)
  51.  
  52.     return map(parse_commit, data)
  53.  
  54.  
  55. def get_usernames(commits: Iterable[Commit]) -> Iterable[Username]:
  56.     return set(commit.username for commit in commits)
  57.  
  58.  
  59. def get_file_creators(commits: Iterable[Commit]) -> Dict[File, Username]:
  60.     file_creators = dict()
  61.  
  62.     for commit in sorted(commits, key=lambda commit: commit.commit_time):
  63.         for file in commit.files:
  64.             if file.name not in file_creators:
  65.                 file_creators[file.name] = commit.username
  66.  
  67.     return file_creators
  68.  
  69.  
  70. def get_changed_lines(commits: Iterable[Commit]) -> int:
  71.     changed_lines = 0
  72.  
  73.     for commit in commits:
  74.         changed_lines += sum(file.changed_lines for file in commit.files)
  75.  
  76.     return changed_lines
  77.  
  78.  
  79. def get_employee(username: Username, commits: Iterable[Commit], file_creators: Dict[File, Username]) -> Employee:
  80.     his_commits = tuple(commit for commit in commits if commit.username == username)
  81.  
  82.     return Employee(
  83.         username=username,
  84.         commits=len(his_commits),
  85.         changed_lines=get_changed_lines(his_commits),
  86.         new_files=sum(creator == username for creator in file_creators.values())
  87.     )
  88.  
  89. commits = tuple(get_commits_from_json("./input.json"))
  90. usernames = get_usernames(commits)
  91. file_creators = get_file_creators(commits)
  92. employeers = []
  93.  
  94. for username in usernames:
  95.     employeers.append(get_employee(username, commits, file_creators))
  96.  
  97. print(employeers)
  98.  
  99. header = ["username", "commits", "changed_lines", "new_files"]
  100.  
  101. with open("output.tsv", "w") as file:
  102.     file.write("{}\n".format("\t".join(header)))
  103.  
  104.     for employee in sorted(employeers, key=lambda el: el.username):
  105.         file.write("{}\n".format("\t".join(map(str, [
  106.             employee.username,
  107.             employee.commits,
  108.             employee.changed_lines,
  109.             employee.new_files
  110.         ]))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement