-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutputs.py
More file actions
48 lines (35 loc) · 1.16 KB
/
outputs.py
File metadata and controls
48 lines (35 loc) · 1.16 KB
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
import csv
import datetime as dt
import logging
from prettytable import PrettyTable
from constants import BASE_DIR, DATETIME_FORMAT
def control_output(results, cli_args):
output = cli_args.output
match output:
case "pretty":
pretty_output(results)
case "file":
file_output(results, cli_args)
case _:
default_output(results)
def default_output(results):
for row in results:
print(*row)
def file_output(results, cli_args):
results_dir = BASE_DIR / "results"
results_dir.mkdir(exist_ok=True)
parser_mode = cli_args.mode
now = dt.datetime.now()
now_formatted = now.strftime(DATETIME_FORMAT)
file_name = f"{parser_mode}_{now_formatted}.csv"
file_path = results_dir / file_name
with open(file_path, "w", encoding="utf-8") as f:
writer = csv.writer(f, dialect="unix")
writer.writerows(results)
logging.info(f"Файл с результатами был сохранён: {file_path}")
def pretty_output(results):
table = PrettyTable()
table.field_names = results[0]
table.align = "l"
table.add_rows(results[1:])
print(table)