1 |
2d129043
|
petrh
|
import inspect
|
2 |
af7609b5
|
Tomáš Ballák
|
from shared_types import StringSetType
|
3 |
d6ca840d
|
petrh
|
from Utilities.CSV import csv_data_line
|
4 |
2d129043
|
petrh
|
|
5 |
04a2b5a4
|
petrh
|
# Path to processed data
|
6 |
c8f3051b
|
petrh
|
PROCESSED_DATA_PATH = "ProcessedData/"
|
7 |
|
|
|
8 |
04a2b5a4
|
petrh
|
|
9 |
af7609b5
|
Tomáš Ballák
|
def get_unique_names_from_file(filename: str,
|
10 |
|
|
column_number: int) -> StringSetType:
|
11 |
04a2b5a4
|
petrh
|
"""
|
12 |
34cf65cd
|
petrh
|
Extract set of unique names from file
|
13 |
04a2b5a4
|
petrh
|
Args:
|
14 |
34cf65cd
|
petrh
|
filename: path to processed file
|
15 |
|
|
column_number: unique names are expected in csv file on column_number
|
16 |
04a2b5a4
|
petrh
|
|
17 |
|
|
Returns:
|
18 |
34cf65cd
|
petrh
|
set of unique names
|
19 |
04a2b5a4
|
petrh
|
"""
|
20 |
c8f3051b
|
petrh
|
# create set of unique names
|
21 |
|
|
name_set = set()
|
22 |
|
|
|
23 |
34cf65cd
|
petrh
|
with open(filename, "r") as file:
|
24 |
|
|
# go through every line of line
|
25 |
|
|
for x in file:
|
26 |
|
|
# split by csv splitter ;
|
27 |
|
|
array = x.split(";")
|
28 |
|
|
# add string from chosen column to set
|
29 |
|
|
name_set.add(array[column_number])
|
30 |
c8f3051b
|
petrh
|
|
31 |
|
|
return name_set
|
32 |
|
|
|
33 |
|
|
|
34 |
af7609b5
|
Tomáš Ballák
|
def export_data_to_csv(filename: str, data_dict) -> None:
|
35 |
34cf65cd
|
petrh
|
"""
|
36 |
|
|
Takes data_dict and export it into a csv file
|
37 |
|
|
Args:
|
38 |
|
|
filename: name of exported file
|
39 |
|
|
data_dict: dictionary containing data from DatasetProcessor
|
40 |
|
|
"""
|
41 |
c8f3051b
|
petrh
|
with open(PROCESSED_DATA_PATH + filename[12:], "w+") as file:
|
42 |
|
|
|
43 |
|
|
for date in data_dict:
|
44 |
d6d75a03
|
petrh
|
if len(date) != 13:
|
45 |
af7609b5
|
Tomáš Ballák
|
raise ValueError(
|
46 |
|
|
"Invalid date format for key value --> YYYY-mm-dd-hh expected!"
|
47 |
|
|
)
|
48 |
c8f3051b
|
petrh
|
for data in data_dict[date]:
|
49 |
2d129043
|
petrh
|
csv_line = data_dict[date][data]
|
50 |
af7609b5
|
Tomáš Ballák
|
if not isinstance(csv_line, csv_data_line.CSVDataLine):
|
51 |
|
|
raise ValueError(
|
52 |
|
|
"data_dict is expected to have CSVDataLine as values")
|
53 |
2d129043
|
petrh
|
file.write(csv_line.to_csv() + '\n')
|