From b9c24cf76f51912d3f3a1e00bf245c84fba5a55e Mon Sep 17 00:00:00 2001 From: Angelina Tingaeva Date: Wed, 22 Nov 2023 00:23:37 +0500 Subject: [PATCH] refactor(asana_users): optimization of getting users.data from .json Asana --- asana_users.py | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/asana_users.py b/asana_users.py index fb2ed4c..fdbcd3c 100644 --- a/asana_users.py +++ b/asana_users.py @@ -2,36 +2,42 @@ import json 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-файла данных о пользователях """ - with open('asana_data', 'r') as file: - data = json.load(file) - - assignees = set() - followers = set() + unique_names = set() + unique_ids = set() for item in data: - assignee = item.get('assignee') - follower = item.get('followers') + assignee = item.get("assignee") + followers = item.get("followers") if assignee: - assignees.add(assignee) - if follower: - followers.add(follower) + unique_names.add(assignee.get("name")) + unique_ids.add(assignee.get("id")) - with open('output.csv', 'w', newline='') as file: - writer = csv.writer(file, delimiter=',') - writer.writerow(["Assignee", "Follower"]) + if followers: + for follower in followers: + unique_names.add(follower.get("name")) + unique_ids.add(follower.get("id")) - for assignee in assignees: - writer.writerow([assignee, ""]) - - for follower in followers: - writer.writerow(["", follower]) + 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)