Skip to content

Commit a8f0227

Browse files
authored
Add linters and auto formatter (#5)
* Black-ify codebase * Apply `ruff` * Add more configuration rules to `ruff` * Add back previously removed imports and add them to `__all__`
1 parent 9b14152 commit a8f0227

File tree

9 files changed

+532
-269
lines changed

9 files changed

+532
-269
lines changed

openpaygo/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from .token_encode import OpenPAYGOTokenEncoder
2-
from .token_decode import OpenPAYGOTokenDecoder
3-
from .token_shared import TokenType
41
from .metrics_request import MetricsRequestHandler
52
from .metrics_response import MetricsResponseHandler
63
from .metrics_shared import AuthMethod
4+
from .token_decode import OpenPAYGOTokenDecoder
5+
from .token_encode import OpenPAYGOTokenEncoder
6+
from .token_shared import TokenType
77

88

99
def generate_token(**kwargs):
@@ -12,3 +12,13 @@ def generate_token(**kwargs):
1212

1313
def decode_token(**kwargs):
1414
return OpenPAYGOTokenDecoder.decode_token(**kwargs)
15+
16+
17+
__all__ = [
18+
MetricsRequestHandler,
19+
MetricsResponseHandler,
20+
AuthMethod,
21+
OpenPAYGOTokenDecoder,
22+
OpenPAYGOTokenEncoder,
23+
TokenType,
24+
]

openpaygo/metrics_request.py

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
1-
from .metrics_shared import OpenPAYGOMetricsShared
21
import copy
32

3+
from .metrics_shared import OpenPAYGOMetricsShared
4+
45

56
class MetricsRequestHandler(object):
6-
7-
def __init__(self, serial_number, data_format=None, secret_key=None, auth_method=None):
7+
def __init__(
8+
self, serial_number, data_format=None, secret_key=None, auth_method=None
9+
):
810
self.secret_key = secret_key
911
self.auth_method = auth_method
1012
self.request_dict = {
11-
'serial_number': serial_number,
13+
"serial_number": serial_number,
1214
}
1315
self.data_format = data_format
1416
if self.data_format:
15-
if self.data_format.get('id'):
16-
self.request_dict['data_format_id'] = data_format.get('id')
17+
if self.data_format.get("id"):
18+
self.request_dict["data_format_id"] = data_format.get("id")
1719
else:
18-
self.request_dict['data_format'] = data_format
20+
self.request_dict["data_format"] = data_format
1921
self.data = {}
2022
self.historical_data = {}
2123

2224
def set_request_count(self, request_count):
23-
self.request_dict['request_count'] = request_count
25+
self.request_dict["request_count"] = request_count
2426

2527
def set_timestamp(self, timestamp):
26-
self.request_dict['timestamp'] = timestamp
28+
self.request_dict["timestamp"] = timestamp
2729

2830
def set_data(self, data):
2931
self.data = data
3032

3133
def set_historical_data(self, historical_data):
32-
if not self.data_format.get('historical_data_interval'):
34+
if not self.data_format.get("historical_data_interval"):
3335
for time_step in historical_data:
34-
if not time_step.get('timestamp'):
35-
raise ValueError('Historical Data objects must have a time stamp if no historical_data_interval is defined.')
36+
if not time_step.get("timestamp"):
37+
raise ValueError(
38+
"Historical Data objects must have a time stamp if no "
39+
"historical_data_interval is defined."
40+
)
3641
self.historical_data = historical_data
3742

3843
def get_simple_request_payload(self):
@@ -41,11 +46,15 @@ def get_simple_request_payload(self):
4146

4247
def get_simple_request_dict(self):
4348
simple_request = self.request_dict
44-
simple_request['data'] = self.data
45-
simple_request['historical_data'] = self.historical_data
49+
simple_request["data"] = self.data
50+
simple_request["historical_data"] = self.historical_data
4651
# We prepare the auth
4752
if self.auth_method:
48-
simple_request['auth'] = OpenPAYGOMetricsShared.generate_request_signature_from_data(simple_request, self.auth_method, self.secret_key)
53+
simple_request[
54+
"auth"
55+
] = OpenPAYGOMetricsShared.generate_request_signature_from_data(
56+
simple_request, self.auth_method, self.secret_key
57+
)
4958
return simple_request
5059

5160
def get_condensed_request_payload(self):
@@ -54,36 +63,55 @@ def get_condensed_request_payload(self):
5463

5564
def get_condensed_request_dict(self):
5665
if not self.data_format:
57-
raise ValueError('No Data Format provided for condensed request')
58-
data_order = self.data_format.get('data_order')
66+
raise ValueError("No Data Format provided for condensed request")
67+
data_order = self.data_format.get("data_order")
5968
if self.data and not data_order:
60-
raise ValueError('Data Format does not contain data_order')
61-
historical_data_order = self.data_format.get('historical_data_order')
69+
raise ValueError("Data Format does not contain data_order")
70+
historical_data_order = self.data_format.get("historical_data_order")
6271
if self.historical_data and not historical_data_order:
63-
raise ValueError('Data Format does not contain historical_data_order')
72+
raise ValueError("Data Format does not contain historical_data_order")
6473
condensed_request = copy.deepcopy(self.request_dict)
65-
condensed_request['data'] = []
66-
condensed_request['historical_data'] = []
74+
condensed_request["data"] = []
75+
condensed_request["historical_data"] = []
6776
# We add the data
6877
data_copy = copy.deepcopy(self.data)
6978
for var in data_order:
70-
condensed_request['data'].append(data_copy.pop(var) if var in data_copy else None)
79+
condensed_request["data"].append(
80+
data_copy.pop(var) if var in data_copy else None
81+
)
7182
if len(data_copy) > 0:
72-
raise ValueError('Additional variables not present in the data format: '+str(data_copy))
73-
condensed_request['data'] = OpenPAYGOMetricsShared.remove_trailing_empty_elements(condensed_request['data'])
83+
raise ValueError(
84+
"Additional variables not present in the data format: " + str(data_copy)
85+
)
86+
condensed_request[
87+
"data"
88+
] = OpenPAYGOMetricsShared.remove_trailing_empty_elements(
89+
condensed_request["data"]
90+
)
7491
# We add the historical data
7592
historical_data_copy = copy.deepcopy(self.historical_data)
7693
for time_step in historical_data_copy:
7794
time_step_data = []
7895
for var in historical_data_order:
7996
time_step_data.append(time_step.pop(var) if var in time_step else None)
8097
if len(time_step) > 0:
81-
raise ValueError('Additional variables not present in the historical data format: '+str(time_step))
82-
time_step_data = OpenPAYGOMetricsShared.remove_trailing_empty_elements(time_step_data)
83-
condensed_request['historical_data'].append(time_step_data)
98+
raise ValueError(
99+
"Additional variables not present in the historical data format: "
100+
+ str(time_step)
101+
)
102+
time_step_data = OpenPAYGOMetricsShared.remove_trailing_empty_elements(
103+
time_step_data
104+
)
105+
condensed_request["historical_data"].append(time_step_data)
84106
# We prepare the auth
85107
if self.auth_method:
86-
condensed_request['auth'] = OpenPAYGOMetricsShared.generate_request_signature_from_data(condensed_request, self.auth_method, self.secret_key)
108+
condensed_request[
109+
"auth"
110+
] = OpenPAYGOMetricsShared.generate_request_signature_from_data(
111+
condensed_request, self.auth_method, self.secret_key
112+
)
87113
# We replace the key names by the condensed ones
88-
condensed_request = OpenPAYGOMetricsShared.convert_dict_keys_to_condensed(condensed_request)
114+
condensed_request = OpenPAYGOMetricsShared.convert_dict_keys_to_condensed(
115+
condensed_request
116+
)
89117
return condensed_request

0 commit comments

Comments
 (0)