refactor(asana_users): optimization of getting users.data from .json Asana

This commit is contained in:
Angelina Tingaeva 2023-11-22 00:23:37 +05:00
parent 15c3021005
commit b9c24cf76f

View File

@ -2,36 +2,42 @@ import json
import csv import csv
DATA_ASANA = './data/data_asana.json' asana_data = './data/data_asana.json'
def get_assignee_and_followers(asana_data): def extract_unique_names_and_ids(data):
""" Получение из json-файла данных о пользователях """ """ Получение из json-файла данных о пользователях """
with open('asana_data', 'r') as file: unique_names = set()
data = json.load(file) unique_ids = set()
assignees = set()
followers = set()
for item in data: for item in data:
assignee = item.get('assignee') assignee = item.get("assignee")
follower = item.get('followers') followers = item.get("followers")
if assignee: if assignee:
assignees.add(assignee) unique_names.add(assignee.get("name"))
if follower: unique_ids.add(assignee.get("id"))
followers.add(follower)
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file, delimiter=',')
writer.writerow(["Assignee", "Follower"])
for assignee in assignees:
writer.writerow([assignee, ""])
if followers:
for follower in followers: for follower in followers:
writer.writerow(["", follower]) unique_names.add(follower.get("name"))
unique_ids.add(follower.get("id"))
return unique_names, unique_ids
get_assignee_and_followers(DATA_ASANA) def export_to_csv(unique_names, unique_ids):
with open("output.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile, delimiter=",")
writer.writerow(["Name", "ID"])
for name, id in zip(unique_names, unique_ids):
writer.writerow([name, id])
with open('asana_data', 'r') as file:
json_data = json.load(file)
unique_names, unique_ids = extract_unique_names_and_ids(json_data)
export_to_csv(unique_names, unique_ids)