Projekt

Obecné

Profil

Stáhnout (1.71 KB) Statistiky
| Větev: | Revize:
1
import inspect
2
from shared_types import StringSetType
3
from Utilities.CSV import csv_data_line
4

    
5
# Path to processed data
6
PROCESSED_DATA_PATH = "ProcessedData/"
7

    
8

    
9
def get_unique_names_from_file(filename: str,
10
                               column_number: int) -> StringSetType:
11
    """
12
        Extract set of unique names from file
13
    Args:
14
        filename: path to processed file
15
        column_number: unique names are expected in csv file on column_number
16

    
17
    Returns:
18
        set of unique names
19
    """
20
    # create set of unique names
21
    name_set = set()
22

    
23
    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

    
31
    return name_set
32

    
33

    
34
def export_data_to_csv(filename: str, data_dict) -> None:
35
    """
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
    with open(PROCESSED_DATA_PATH + filename[12:], "w+") as file:
42

    
43
        for date in data_dict:
44
            if len(date) != 13:
45
                raise ValueError(
46
                    "Invalid date format for key value --> YYYY-mm-dd-hh expected!"
47
                )
48
            for data in data_dict[date]:
49
                csv_line = data_dict[date][data]
50
                if not isinstance(csv_line, csv_data_line.CSVDataLine):
51
                    raise ValueError(
52
                        "data_dict is expected to have CSVDataLine as values")
53
                file.write(csv_line.to_csv() + '\n')
(2-2/2)