pygazpar 1.2.8__py313-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
pygazpar/enum.py ADDED
@@ -0,0 +1,35 @@
1
+ from enum import Enum
2
+
3
+
4
+ # ------------------------------------------------------------------------------------------------------------
5
+ class PropertyName(Enum):
6
+ TIME_PERIOD = "time_period"
7
+ START_INDEX = "start_index_m3"
8
+ END_INDEX = "end_index_m3"
9
+ VOLUME = "volume_m3"
10
+ ENERGY = "energy_kwh"
11
+ CONVERTER_FACTOR = "converter_factor_kwh/m3"
12
+ TEMPERATURE = "temperature_degC"
13
+ TYPE = "type"
14
+ TIMESTAMP = "timestamp"
15
+
16
+ def __str__(self):
17
+ return self.value
18
+
19
+ def __repr__(self):
20
+ return self.__str__()
21
+
22
+
23
+ # ------------------------------------------------------------------------------------------------------------
24
+ class Frequency(Enum):
25
+ HOURLY = "hourly"
26
+ DAILY = "daily"
27
+ WEEKLY = "weekly"
28
+ MONTHLY = "monthly"
29
+ YEARLY = "yearly"
30
+
31
+ def __str__(self):
32
+ return self.value
33
+
34
+ def __repr__(self):
35
+ return self.__str__()
@@ -0,0 +1,138 @@
1
+ import logging
2
+ from datetime import datetime
3
+ from pygazpar.enum import Frequency
4
+ from pygazpar.enum import PropertyName
5
+ from openpyxl.worksheet.worksheet import Worksheet
6
+ from openpyxl.cell.cell import Cell
7
+ from openpyxl import load_workbook
8
+ from typing import Any, List, Dict
9
+
10
+
11
+ FIRST_DATA_LINE_NUMBER = 10
12
+
13
+ Logger = logging.getLogger(__name__)
14
+
15
+
16
+ # ------------------------------------------------------------------------------------------------------------
17
+ class ExcelParser:
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]]:
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 = {}
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 = {}
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 = {}
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,50 @@
1
+ import json
2
+ import logging
3
+ from datetime import datetime
4
+ from pygazpar.enum import PropertyName
5
+ from typing import Any, List, Dict
6
+
7
+ INPUT_DATE_FORMAT = "%Y-%m-%d"
8
+
9
+ OUTPUT_DATE_FORMAT = "%d/%m/%Y"
10
+
11
+ Logger = logging.getLogger(__name__)
12
+
13
+
14
+ # ------------------------------------------------------------------------------------------------------------
15
+ class JsonParser:
16
+
17
+ # ------------------------------------------------------
18
+ @staticmethod
19
+ def parse(jsonStr: str, temperaturesStr: str, pceIdentifier: str) -> List[Dict[str, Any]]:
20
+
21
+ res = []
22
+
23
+ data = json.loads(jsonStr)
24
+
25
+ temperatures = json.loads(temperaturesStr)
26
+
27
+ # Timestamp of the data.
28
+ data_timestamp = datetime.now().isoformat()
29
+
30
+ for releve in data[pceIdentifier]['releves']:
31
+ temperature = releve['temperature']
32
+ if temperature is None and temperatures is not None and len(temperatures) > 0:
33
+ temperature = temperatures.get(releve['journeeGaziere'])
34
+
35
+ item = {}
36
+ item[PropertyName.TIME_PERIOD.value] = datetime.strftime(datetime.strptime(releve['journeeGaziere'], INPUT_DATE_FORMAT), OUTPUT_DATE_FORMAT)
37
+ item[PropertyName.START_INDEX.value] = releve['indexDebut']
38
+ item[PropertyName.END_INDEX.value] = releve['indexFin']
39
+ item[PropertyName.VOLUME.value] = releve['volumeBrutConsomme']
40
+ item[PropertyName.ENERGY.value] = releve['energieConsomme']
41
+ item[PropertyName.CONVERTER_FACTOR.value] = releve['coeffConversion']
42
+ item[PropertyName.TEMPERATURE.value] = temperature
43
+ item[PropertyName.TYPE.value] = releve['qualificationReleve']
44
+ item[PropertyName.TIMESTAMP.value] = data_timestamp
45
+
46
+ res.append(item)
47
+
48
+ Logger.debug("Daily data read successfully from Json")
49
+
50
+ return res