Skip to content

Commit 9bf1699

Browse files
authored
Chore: Format code using Ruff. Satisfy and enable linter on CI. (#26)
1 parent 8203173 commit 9bf1699

File tree

7 files changed

+149
-177
lines changed

7 files changed

+149
-177
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
run: uv pip install --system --requirement=requirements.txt --requirement=requirements-dev.txt
5656

5757
- name: Run linters and software tests
58-
run: poe test
58+
run: poe check
5959

6060
# https://github.com/codecov/codecov-action
6161
- name: Upload coverage results to Codecov

cratedb.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import requests
33
except ImportError:
44
import urequests as requests
5-
5+
66
from base64 import b64encode
77

88
# IDs of CrateDB supported data types.
@@ -78,75 +78,76 @@
7878
CRATEDB_ERROR_SNAPSHOT_CREATION_FAILED = 5004
7979
CRATEDB_ERROR_QUERY_KILLED = 5030
8080

81+
8182
class NetworkError(Exception):
8283
pass
8384

85+
8486
class CrateDBError(Exception):
8587
pass
8688

89+
8790
class CrateDB:
88-
def __init__(self, host, port=4200, user=None, password=None, schema="doc", use_ssl=True):
91+
def __init__(
92+
self, host, port=4200, user=None, password=None, schema="doc", use_ssl=True
93+
):
8994
self.user = user
9095
self.password = password
9196
self.schema = schema
9297
self.host = host
9398
self.port = port
9499
self.use_ssl = use_ssl
95100

96-
self.cratedb_url = f"{'https' if self.use_ssl == True else 'http'}://{self.host}:{self.port}/_sql"
101+
self.cratedb_url = f"{'https' if self.use_ssl is True else 'http'}://{self.host}:{self.port}/_sql"
97102

98103
if self.user is not None and self.password is not None:
99104
self.encoded_credentials = self.__encode_credentials(self.user, self.password)
100105

101-
102106
def __encode_credentials(self, user, password):
103107
creds_str = f"{user}:{password}"
104108
return b64encode(creds_str.encode("UTF-8")).decode("UTF-8")
105-
106109

107-
def __make_request(self, sql, args=None, with_types = False, return_response = True):
108-
headers = {
109-
"Content-Type": "text/json",
110-
"Default-Schema": self.schema
111-
}
110+
def __make_request(self, sql, args=None, with_types=False, return_response=True):
111+
headers = {"Content-Type": "text/json", "Default-Schema": self.schema}
112112

113113
if hasattr(self, "encoded_credentials"):
114114
headers["Authorization"] = f"Basic {self.encoded_credentials}"
115115

116-
request_url = self.cratedb_url if with_types == False else f"{self.cratedb_url}?types"
116+
request_url = (
117+
self.cratedb_url if with_types is False else f"{self.cratedb_url}?types"
118+
)
117119

118-
payload = {
119-
"stmt": sql
120-
}
120+
payload = {"stmt": sql}
121121

122122
if args is not None:
123123
for arg in args:
124124
if not isinstance(arg, list):
125125
payload["args"] = args
126126
break
127127

128-
if not "args" in payload:
128+
if "args" not in payload:
129129
payload["bulk_args"] = args
130130

131131
try:
132-
response = requests.post(
133-
request_url,
134-
headers = headers,
135-
json = payload
136-
)
132+
response = requests.post(request_url, headers=headers, json=payload)
137133
except OSError as o:
138-
raise NetworkError(o)
134+
raise NetworkError(o) # noqa: B904
139135

140-
if response.status_code == 400 or response.status_code == 404 or response.status_code == 409:
136+
if (
137+
response.status_code == 400
138+
or response.status_code == 404
139+
or response.status_code == 409
140+
):
141141
error_doc = response.json()
142142
raise CrateDBError(error_doc)
143-
elif response.status_code != 200:
144-
raise NetworkError(f"Error {response.status_code}: {response.reason.decode('UTF-8')}")
143+
if response.status_code != 200:
144+
raise NetworkError(
145+
f"Error {response.status_code}: {response.reason.decode('UTF-8')}"
146+
)
145147

146-
if return_response == True:
148+
if return_response is True:
147149
return response.json()
150+
return None
148151

149-
150-
def execute(self, sql, args = None, with_types = False, return_response = True):
152+
def execute(self, sql, args=None, with_types=False, return_response=True):
151153
return self.__make_request(sql, args, with_types, return_response)
152-

examples/example_usage.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# ruff: noqa: W291 Trailing whitespace
2+
13
# Example script showing different types of interactions with
2-
# CrateDB. This has no hardware dependencies so should run
4+
# CrateDB. This has no hardware dependencies so should run
35
# in any MicroPython environment. You will need to edit the
46
# code below to use your CrateDB credentials.
57

@@ -20,7 +22,9 @@
2022
try:
2123
# Create a table.
2224
print("Create table.")
23-
response = crate.execute("create table driver_test(id TEXT, val1 bigint, val2 bigint, val3 boolean)")
25+
response = crate.execute(
26+
"create table driver_test(id TEXT, val1 bigint, val2 bigint, val3 boolean)"
27+
)
2428
# response:
2529
# {'rows': [[]], 'rowcount': 1, 'cols': [], 'duration': 119.652275}
2630
print(response)
@@ -29,10 +33,7 @@
2933
print("Bulk insert.")
3034
response = crate.execute(
3135
"insert into driver_test (id, val1, val2, val3) values (?, ?, ?, ?)",
32-
[
33-
[ "a", 2, 3, True ],
34-
[ "b", 3, 4, False ]
35-
]
36+
[["a", 2, 3, True], ["b", 3, 4, False]],
3637
)
3738
# response:
3839
# {'results': [{'rowcount': 1}, {'rowcount': 1}], 'cols': [], 'duration': 6.265751}
@@ -42,14 +43,14 @@
4243
print("Select with column data types.")
4344
response = crate.execute("select * from driver_test", with_types=True)
4445
# response:
45-
# {'col_types': [4, 10, 10, 3], 'cols': ['id', 'val1', 'val2', 'val3'], 'rowcount': 2, 'rows': [['b', 3, 4, False], ['a', 2, 3, True]], 'duration': 4.378391}
46+
# {'col_types': [4, 10, 10, 3], 'cols': ['id', 'val1', 'val2', 'val3'], 'rowcount': 2,
47+
# 'rows': [['b', 3, 4, False], ['a', 2, 3, True]], 'duration': 4.378391}
4648
print(response)
4749

4850
# SELECT with parameter substitution.
4951
print("Select with parameter substitution.")
5052
response = crate.execute(
51-
"select val1, val2 from driver_test where val1 > ? and val2 < ?",
52-
[ 1, 4 ]
53+
"select val1, val2 from driver_test where val1 > ? and val2 < ?", [1, 4]
5354
)
5455
# response:
5556
# {'rows': [[2, 3]], 'rowcount': 1, 'cols': ['val1', 'val2'], 'duration': 3.266117}
@@ -59,7 +60,7 @@
5960
print("Insert with parameter substitution.")
6061
response = crate.execute(
6162
"insert into driver_test (id, val1, val2, val3) values (?, ?, ?, ?)",
62-
[ "d", 1, 9, False ]
63+
["d", 1, 9, False],
6364
)
6465
# response:
6566
# {'rows': [[]], 'rowcount': 1, 'cols': [], 'duration': 5.195949}
@@ -69,8 +70,8 @@
6970
print("Insert with parameter substitution and no response processing.")
7071
response = crate.execute(
7172
"insert into driver_test (id, val1, val2, val3) values (?, ?, ?, ?)",
72-
[ "e", 4, 12, True ],
73-
return_response=False
73+
["e", 4, 12, True],
74+
return_response=False,
7475
)
7576
# response:
7677
# None

0 commit comments

Comments
 (0)