pygazpar 1.2.3__py312-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,136 @@
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)
37
+
38
+ workbook.close()
39
+
40
+ return res
41
+
42
+ # ------------------------------------------------------
43
+ @staticmethod
44
+ def __fillRow(row: Dict, propertyName: str, cell: Cell, isNumber: bool):
45
+
46
+ if cell.value is not None:
47
+ if isNumber:
48
+ if type(cell.value) is str:
49
+ if len(cell.value.strip()) > 0:
50
+ row[propertyName] = float(cell.value.replace(',', '.'))
51
+ else:
52
+ row[propertyName] = cell.value
53
+ else:
54
+ row[propertyName] = cell.value.strip() if type(cell.value) is str else cell.value
55
+
56
+ # ------------------------------------------------------
57
+ @staticmethod
58
+ def __parseHourly(worksheet: Worksheet) -> List[Dict[str, Any]]:
59
+ return []
60
+
61
+ # ------------------------------------------------------
62
+ @staticmethod
63
+ def __parseDaily(worksheet: Worksheet) -> List[Dict[str, Any]]:
64
+
65
+ res = []
66
+
67
+ # Timestamp of the data.
68
+ data_timestamp = datetime.now().isoformat()
69
+
70
+ minRowNum = FIRST_DATA_LINE_NUMBER
71
+ maxRowNum = len(worksheet['B'])
72
+ for rownum in range(minRowNum, maxRowNum + 1):
73
+ row = {}
74
+ if worksheet.cell(column=2, row=rownum).value is not None:
75
+ ExcelParser.__fillRow(row, PropertyName.TIME_PERIOD.value, worksheet.cell(column=2, row=rownum), False) # type: ignore
76
+ ExcelParser.__fillRow(row, PropertyName.START_INDEX.value, worksheet.cell(column=3, row=rownum), True) # type: ignore
77
+ ExcelParser.__fillRow(row, PropertyName.END_INDEX.value, worksheet.cell(column=4, row=rownum), True) # type: ignore
78
+ ExcelParser.__fillRow(row, PropertyName.VOLUME.value, worksheet.cell(column=5, row=rownum), True) # type: ignore
79
+ ExcelParser.__fillRow(row, PropertyName.ENERGY.value, worksheet.cell(column=6, row=rownum), True) # type: ignore
80
+ ExcelParser.__fillRow(row, PropertyName.CONVERTER_FACTOR.value, worksheet.cell(column=7, row=rownum), True) # type: ignore
81
+ ExcelParser.__fillRow(row, PropertyName.TEMPERATURE.value, worksheet.cell(column=8, row=rownum), True) # type: ignore
82
+ ExcelParser.__fillRow(row, PropertyName.TYPE.value, worksheet.cell(column=9, row=rownum), False) # type: ignore
83
+ row[PropertyName.TIMESTAMP.value] = data_timestamp
84
+ res.append(row)
85
+
86
+ Logger.debug(f"Daily data read successfully between row #{minRowNum} and row #{maxRowNum}")
87
+
88
+ return res
89
+
90
+ # ------------------------------------------------------
91
+ @staticmethod
92
+ def __parseWeekly(worksheet: Worksheet) -> List[Dict[str, Any]]:
93
+
94
+ res = []
95
+
96
+ # Timestamp of the data.
97
+ data_timestamp = datetime.now().isoformat()
98
+
99
+ minRowNum = FIRST_DATA_LINE_NUMBER
100
+ maxRowNum = len(worksheet['B'])
101
+ for rownum in range(minRowNum, maxRowNum + 1):
102
+ row = {}
103
+ if worksheet.cell(column=2, row=rownum).value is not None:
104
+ ExcelParser.__fillRow(row, PropertyName.TIME_PERIOD.value, worksheet.cell(column=2, row=rownum), False) # type: ignore
105
+ ExcelParser.__fillRow(row, PropertyName.VOLUME.value, worksheet.cell(column=3, row=rownum), True) # type: ignore
106
+ ExcelParser.__fillRow(row, PropertyName.ENERGY.value, worksheet.cell(column=4, row=rownum), True) # type: ignore
107
+ row[PropertyName.TIMESTAMP.value] = data_timestamp
108
+ res.append(row)
109
+
110
+ Logger.debug(f"Weekly data read successfully between row #{minRowNum} and row #{maxRowNum}")
111
+
112
+ return res
113
+
114
+ # ------------------------------------------------------
115
+ @staticmethod
116
+ def __parseMonthly(worksheet: Worksheet) -> List[Dict[str, Any]]:
117
+
118
+ res = []
119
+
120
+ # Timestamp of the data.
121
+ data_timestamp = datetime.now().isoformat()
122
+
123
+ minRowNum = FIRST_DATA_LINE_NUMBER
124
+ maxRowNum = len(worksheet['B'])
125
+ for rownum in range(minRowNum, maxRowNum + 1):
126
+ row = {}
127
+ if worksheet.cell(column=2, row=rownum).value is not None:
128
+ ExcelParser.__fillRow(row, PropertyName.TIME_PERIOD.value, worksheet.cell(column=2, row=rownum), False) # type: ignore
129
+ ExcelParser.__fillRow(row, PropertyName.VOLUME.value, worksheet.cell(column=3, row=rownum), True) # type: ignore
130
+ ExcelParser.__fillRow(row, PropertyName.ENERGY.value, worksheet.cell(column=4, row=rownum), True) # type: ignore
131
+ row[PropertyName.TIMESTAMP.value] = data_timestamp
132
+ res.append(row)
133
+
134
+ Logger.debug(f"Monthly data read successfully between row #{minRowNum} and row #{maxRowNum}")
135
+
136
+ 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:
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