pygazpar 0.1.21__py3-none-any.whl → 1.3.0b2__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,138 @@
1
+ import logging
2
+ from datetime import datetime
3
+ from typing import Any, Dict, List
4
+
5
+ from openpyxl import load_workbook
6
+ from openpyxl.cell.cell import Cell
7
+ from openpyxl.worksheet.worksheet import Worksheet
8
+
9
+ from pygazpar.enum import Frequency, PropertyName
10
+
11
+ FIRST_DATA_LINE_NUMBER = 10
12
+
13
+ Logger = logging.getLogger(__name__)
14
+
15
+
16
+ # ------------------------------------------------------------------------------------------------------------
17
+ class ExcelParser: # pylint: disable=too-few-public-methods
18
+
19
+ # ------------------------------------------------------
20
+ @staticmethod
21
+ def parse(dataFilename: str, dataReadingFrequency: Frequency) -> List[Dict[str, Any]]:
22
+
23
+ parseByFrequency = {
24
+ Frequency.HOURLY: ExcelParser.__parseHourly,
25
+ Frequency.DAILY: ExcelParser.__parseDaily,
26
+ Frequency.WEEKLY: ExcelParser.__parseWeekly,
27
+ Frequency.MONTHLY: ExcelParser.__parseMonthly,
28
+ }
29
+
30
+ Logger.debug(f"Loading Excel data file '{dataFilename}'...")
31
+
32
+ workbook = load_workbook(filename=dataFilename)
33
+
34
+ worksheet = workbook.active
35
+
36
+ res = parseByFrequency[dataReadingFrequency](worksheet) # type: ignore
37
+
38
+ workbook.close()
39
+
40
+ Logger.debug("Processed Excel %s data: %s", dataReadingFrequency, res)
41
+
42
+ return res
43
+
44
+ # ------------------------------------------------------
45
+ @staticmethod
46
+ def __fillRow(row: Dict, propertyName: str, cell: Cell, isNumber: bool):
47
+
48
+ if cell.value is not None:
49
+ if isNumber:
50
+ if type(cell.value) is str:
51
+ if len(cell.value.strip()) > 0:
52
+ row[propertyName] = float(cell.value.replace(",", "."))
53
+ else:
54
+ row[propertyName] = cell.value
55
+ else:
56
+ row[propertyName] = cell.value.strip() if type(cell.value) is str else cell.value
57
+
58
+ # ------------------------------------------------------
59
+ @staticmethod
60
+ def __parseHourly(worksheet: Worksheet) -> List[Dict[str, Any]]: # pylint: disable=unused-argument
61
+ return []
62
+
63
+ # ------------------------------------------------------
64
+ @staticmethod
65
+ def __parseDaily(worksheet: Worksheet) -> List[Dict[str, Any]]:
66
+
67
+ res = []
68
+
69
+ # Timestamp of the data.
70
+ data_timestamp = datetime.now().isoformat()
71
+
72
+ minRowNum = FIRST_DATA_LINE_NUMBER
73
+ maxRowNum = len(worksheet["B"])
74
+ for rownum in range(minRowNum, maxRowNum + 1):
75
+ row = dict[str, Any]()
76
+ if worksheet.cell(column=2, row=rownum).value is not None:
77
+ ExcelParser.__fillRow(row, PropertyName.TIME_PERIOD.value, worksheet.cell(column=2, row=rownum), False) # type: ignore
78
+ ExcelParser.__fillRow(row, PropertyName.START_INDEX.value, worksheet.cell(column=3, row=rownum), True) # type: ignore
79
+ ExcelParser.__fillRow(row, PropertyName.END_INDEX.value, worksheet.cell(column=4, row=rownum), True) # type: ignore
80
+ ExcelParser.__fillRow(row, PropertyName.VOLUME.value, worksheet.cell(column=5, row=rownum), True) # type: ignore
81
+ ExcelParser.__fillRow(row, PropertyName.ENERGY.value, worksheet.cell(column=6, row=rownum), True) # type: ignore
82
+ ExcelParser.__fillRow(row, PropertyName.CONVERTER_FACTOR.value, worksheet.cell(column=7, row=rownum), True) # type: ignore
83
+ ExcelParser.__fillRow(row, PropertyName.TEMPERATURE.value, worksheet.cell(column=8, row=rownum), True) # type: ignore
84
+ ExcelParser.__fillRow(row, PropertyName.TYPE.value, worksheet.cell(column=9, row=rownum), False) # type: ignore
85
+ row[PropertyName.TIMESTAMP.value] = data_timestamp
86
+ res.append(row)
87
+
88
+ Logger.debug(f"Daily data read successfully between row #{minRowNum} and row #{maxRowNum}")
89
+
90
+ return res
91
+
92
+ # ------------------------------------------------------
93
+ @staticmethod
94
+ def __parseWeekly(worksheet: Worksheet) -> List[Dict[str, Any]]:
95
+
96
+ res = []
97
+
98
+ # Timestamp of the data.
99
+ data_timestamp = datetime.now().isoformat()
100
+
101
+ minRowNum = FIRST_DATA_LINE_NUMBER
102
+ maxRowNum = len(worksheet["B"])
103
+ for rownum in range(minRowNum, maxRowNum + 1):
104
+ row = dict[str, Any]()
105
+ if worksheet.cell(column=2, row=rownum).value is not None:
106
+ ExcelParser.__fillRow(row, PropertyName.TIME_PERIOD.value, worksheet.cell(column=2, row=rownum), False) # type: ignore
107
+ ExcelParser.__fillRow(row, PropertyName.VOLUME.value, worksheet.cell(column=3, row=rownum), True) # type: ignore
108
+ ExcelParser.__fillRow(row, PropertyName.ENERGY.value, worksheet.cell(column=4, row=rownum), True) # type: ignore
109
+ row[PropertyName.TIMESTAMP.value] = data_timestamp
110
+ res.append(row)
111
+
112
+ Logger.debug(f"Weekly data read successfully between row #{minRowNum} and row #{maxRowNum}")
113
+
114
+ return res
115
+
116
+ # ------------------------------------------------------
117
+ @staticmethod
118
+ def __parseMonthly(worksheet: Worksheet) -> List[Dict[str, Any]]:
119
+
120
+ res = []
121
+
122
+ # Timestamp of the data.
123
+ data_timestamp = datetime.now().isoformat()
124
+
125
+ minRowNum = FIRST_DATA_LINE_NUMBER
126
+ maxRowNum = len(worksheet["B"])
127
+ for rownum in range(minRowNum, maxRowNum + 1):
128
+ row = dict[str, Any]()
129
+ if worksheet.cell(column=2, row=rownum).value is not None:
130
+ ExcelParser.__fillRow(row, PropertyName.TIME_PERIOD.value, worksheet.cell(column=2, row=rownum), False) # type: ignore
131
+ ExcelParser.__fillRow(row, PropertyName.VOLUME.value, worksheet.cell(column=3, row=rownum), True) # type: ignore
132
+ ExcelParser.__fillRow(row, PropertyName.ENERGY.value, worksheet.cell(column=4, row=rownum), True) # type: ignore
133
+ row[PropertyName.TIMESTAMP.value] = data_timestamp
134
+ res.append(row)
135
+
136
+ Logger.debug(f"Monthly data read successfully between row #{minRowNum} and row #{maxRowNum}")
137
+
138
+ return res
pygazpar/jsonparser.py ADDED
@@ -0,0 +1,53 @@
1
+ import json
2
+ import logging
3
+ from datetime import datetime
4
+ from typing import Any, Dict, List
5
+
6
+ from pygazpar.enum import PropertyName
7
+
8
+ INPUT_DATE_FORMAT = "%Y-%m-%d"
9
+
10
+ OUTPUT_DATE_FORMAT = "%d/%m/%Y"
11
+
12
+ Logger = logging.getLogger(__name__)
13
+
14
+
15
+ # ------------------------------------------------------------------------------------------------------------
16
+ class JsonParser: # pylint: disable=too-few-public-methods
17
+
18
+ # ------------------------------------------------------
19
+ @staticmethod
20
+ def parse(jsonStr: str, temperaturesStr: str, pceIdentifier: str) -> List[Dict[str, Any]]:
21
+
22
+ res = []
23
+
24
+ data = json.loads(jsonStr)
25
+
26
+ temperatures = json.loads(temperaturesStr)
27
+
28
+ # Timestamp of the data.
29
+ data_timestamp = datetime.now().isoformat()
30
+
31
+ for releve in data[pceIdentifier]["releves"]:
32
+ temperature = releve["temperature"]
33
+ if temperature is None and temperatures is not None and len(temperatures) > 0:
34
+ temperature = temperatures.get(releve["journeeGaziere"])
35
+
36
+ item = {}
37
+ item[PropertyName.TIME_PERIOD.value] = datetime.strftime(
38
+ datetime.strptime(releve["journeeGaziere"], INPUT_DATE_FORMAT), OUTPUT_DATE_FORMAT
39
+ )
40
+ item[PropertyName.START_INDEX.value] = releve["indexDebut"]
41
+ item[PropertyName.END_INDEX.value] = releve["indexFin"]
42
+ item[PropertyName.VOLUME.value] = releve["volumeBrutConsomme"]
43
+ item[PropertyName.ENERGY.value] = releve["energieConsomme"]
44
+ item[PropertyName.CONVERTER_FACTOR.value] = releve["coeffConversion"]
45
+ item[PropertyName.TEMPERATURE.value] = temperature
46
+ item[PropertyName.TYPE.value] = releve["qualificationReleve"]
47
+ item[PropertyName.TIMESTAMP.value] = data_timestamp
48
+
49
+ res.append(item)
50
+
51
+ Logger.debug("Daily data read successfully from Json")
52
+
53
+ return res