-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
78 lines (68 loc) · 2.46 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Title : Utils
Author : Dimitra Paraskevopoulou
Created : 06 December 2020
"""
import csv
import logging
import requests
import settings
import os
from pprint import pformat
class Utils:
"""
This class holds some helper methods
"""
def __init__(self, file_path: str = ''):
self.csv_upload_uri = settings.CSV_UPLOAD_URI
dir_path = os.path.dirname(os.path.realpath(__file__))
file_path = file_path if file_path != '' else settings.CSV_FILE
self.__cvc_file_path = os.path.join(dir_path, file_path)
@staticmethod
def sort_by_height(characters: list) -> list:
"""
Method to sort a list of dictionaries by the value of a specific key in the dictionaries
in descending order
:param characters: list of dictionaries
:return: sorted list of dictionaries by height value
"""
try:
return sorted(characters, key=lambda k: int(k['height']), reverse=True)
except Exception as e:
logging.error(
f"[{type(e).__name__}] exception occurred with arguments: {e.args!r}.")
raise e
def output_results(self, characters: list) -> None:
"""
Method to dump input data into a csv file
:param characters: list of dictionaries
"""
try:
csvfile = None
with open(self.__cvc_file_path, 'w', newline='') as csvfile:
fieldnames = ['name', 'species', 'height', 'appearances']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in characters:
writer.writerow(row)
except Exception as e:
if csvfile is not None:
csvfile.close()
logging.error(
f"[{type(e).__name__}] exception occurred with arguments: {e.args!r}.")
raise e
def send_csv(self):
"""
Upload the csv file to httpbin.org
:return:
"""
try:
with open(self.__cvc_file_path, 'rb') as csvfile:
r = requests.post(self.csv_upload_uri, files={'file': csvfile})
r.raise_for_status()
# The following log is just for visualization of the final step
logging.info(pformat(r.json()))
except Exception as e:
logging.error(
f"[{type(e).__name__}] exception occurred with arguments: {e.args!r}.")
raise e