Skip to content

Commit 1870b89

Browse files
committed
refactor: use black to clean up whitespace and improve code formatting in HTTPHandler and ExclusiveRemoteTask
1 parent 2927372 commit 1870b89

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

src/bloqade/analog/task/exclusive.py

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from bloqade.analog.builder.base import ParamType
2121

2222

23-
2423
class HTTPHandlerABC:
2524
@abc.abstractmethod
2625
def submit_task_via_zapier(task_ir: QuEraTaskSpecification, task_id: str):
@@ -75,17 +74,21 @@ def convert_preview_to_download(preview_url):
7574

7675

7776
class HTTPHandler(HTTPHandlerABC):
78-
def __init__(self, zapier_webhook_url: str = None,
79-
zapier_webhook_key: str = None,
80-
vercel_api_url: str = None):
77+
def __init__(
78+
self,
79+
zapier_webhook_url: str = None,
80+
zapier_webhook_key: str = None,
81+
vercel_api_url: str = None,
82+
):
8183
self.zapier_webhook_url = zapier_webhook_url or os.environ["ZAPIER_WEBHOOK_URL"]
8284
self.zapier_webhook_key = zapier_webhook_key or os.environ["ZAPIER_WEBHOOK_KEY"]
8385
self.verrcel_api_url = vercel_api_url or os.environ["VERCEL_API_URL"]
8486

85-
def submit_task_via_zapier(self, task_ir: QuEraTaskSpecification, task_id: str, task_note: str):
87+
def submit_task_via_zapier(
88+
self, task_ir: QuEraTaskSpecification, task_id: str, task_note: str
89+
):
8690
# implement http request logic to submit task via Zapier
87-
request_options = dict(
88-
params={"key": self.zapier_webhook_key, "note": task_id})
91+
request_options = dict(params={"key": self.zapier_webhook_key, "note": task_id})
8992

9093
# for metadata, task_ir in self._compile_single(shots, use_experimental, args):
9194
json_request_body = task_ir.json(exclude_none=True, exclude_unset=True)
@@ -98,8 +101,7 @@ def submit_task_via_zapier(self, task_ir: QuEraTaskSpecification, task_id: str,
98101
submit_status = response_data.get("status", None)
99102
return submit_status
100103
else:
101-
print(
102-
f"HTTP request failed with status code: {response.status_code}")
104+
print(f"HTTP request failed with status code: {response.status_code}")
103105
print("HTTP responce: ", response.text)
104106
return "Failed"
105107

@@ -131,7 +133,6 @@ def query_task_status(self, task_id: str):
131133
status = matches[0].get("status")
132134
return status
133135

134-
135136
def fetch_results(self, task_id: str):
136137
response = request(
137138
"GET",
@@ -143,8 +144,7 @@ def fetch_results(self, task_id: str):
143144
},
144145
)
145146
if response.status_code != 200:
146-
print(
147-
f"HTTP request failed with status code: {response.status_code}")
147+
print(f"HTTP request failed with status code: {response.status_code}")
148148
print("HTTP responce: ", response.text)
149149
return None
150150

@@ -164,8 +164,7 @@ def fetch_results(self, task_id: str):
164164
googledoc = record.get("resultsFileUrl")
165165

166166
# convert the preview URL to download URL
167-
googledoc = convert_preview_to_download(
168-
googledoc)
167+
googledoc = convert_preview_to_download(googledoc)
169168
res = get(googledoc)
170169
res.raise_for_status()
171170
data = res.json()
@@ -177,6 +176,7 @@ def fetch_results(self, task_id: str):
177176
class TestHTTPHandler(HTTPHandlerABC):
178177
pass
179178

179+
180180
@dataclass
181181
@Serializer.register
182182
class ExclusiveRemoteTask(CustomRemoteTaskABC):
@@ -186,7 +186,7 @@ class ExclusiveRemoteTask(CustomRemoteTaskABC):
186186
_http_handler: HTTPHandlerABC = field(default_factory=HTTPHandler)
187187
_task_id: str | None = None
188188
_task_result_ir: QuEraTaskResults | None = None
189-
189+
190190
def __post_init__(self):
191191
float_sites = list(
192192
map(lambda x: (float(x[0]), float(x[1])), self._task_ir.lattice.sites)
@@ -195,7 +195,6 @@ def __post_init__(self):
195195
float_sites, self._task_ir.lattice.filling, self._parallel_decoder
196196
)
197197

198-
199198
@classmethod
200199
def from_compile_results(cls, task_ir, metadata, parallel_decoder):
201200
return cls(
@@ -212,12 +211,19 @@ def _submit(self, force: bool = False) -> "ExclusiveRemoteTask":
212211
)
213212
self._task_id = str(uuid.uuid4())
214213

215-
if self._http_handler.submit_task_via_zapier(self._task_ir, self._task_id, None) == "success":
214+
if (
215+
self._http_handler.submit_task_via_zapier(
216+
self._task_ir, self._task_id, None
217+
)
218+
== "success"
219+
):
216220
self._task_result_ir = QuEraTaskResults(
217-
task_status=QuEraTaskStatusCode.Accepted)
221+
task_status=QuEraTaskStatusCode.Accepted
222+
)
218223
else:
219224
self._task_result_ir = QuEraTaskResults(
220-
task_status=QuEraTaskStatusCode.Failed)
225+
task_status=QuEraTaskStatusCode.Failed
226+
)
221227
return self
222228

223229
def fetch(self):
@@ -235,8 +241,7 @@ def fetch(self):
235241

236242
status = self.status()
237243
if status in [QuEraTaskStatusCode.Completed, QuEraTaskStatusCode.Partial]:
238-
self._task_result_ir = self._http_handler.fetch_results(
239-
self._task_id)
244+
self._task_result_ir = self._http_handler.fetch_results(self._task_id)
240245
else:
241246
self._task_result_ir = QuEraTaskResults(task_status=status)
242247

@@ -248,18 +253,15 @@ def pull(self):
248253
raise NotImplementedError(
249254
"Pulling is not supported. Please use fetch() instead."
250255
)
251-
256+
252257
def cancel(self):
253258
# This is not supported
254-
raise NotImplementedError(
255-
"Cancelling is not supported."
256-
)
259+
raise NotImplementedError("Cancelling is not supported.")
257260

258261
def status(self) -> QuEraTaskStatusCode:
259262
if self._task_id is None:
260263
return QuEraTaskStatusCode.Unsubmitted
261264
res = self._http_handler.query_task_status(self._task_id)
262-
#print("Query task status: ", res)
263265
if res == "Failed":
264266
raise ValueError("Query task status failed.")
265267
elif res == "Submitted":
@@ -335,15 +337,12 @@ def _deserializer(d: Dict[str, any]) -> ExclusiveRemoteTask:
335337
d1 = dict()
336338
d1["_task_ir"] = QuEraTaskSpecification(**d["task_ir"])
337339
d1["_parallel_decoder"] = (
338-
ParallelDecoder(**d["parallel_decoder"]
339-
) if d["parallel_decoder"] else None )
340+
ParallelDecoder(**d["parallel_decoder"]) if d["parallel_decoder"] else None
341+
)
340342
d1["_metadata"] = d["metadata"]
341343
d1["_task_result_ir"] = (
342-
QuEraTaskResults(**d["task_result_ir"])
343-
if d["task_result_ir"]
344-
else None
344+
QuEraTaskResults(**d["task_result_ir"]) if d["task_result_ir"] else None
345345
)
346346
d1["_task_id"] = d["task_id"]
347347

348348
return ExclusiveRemoteTask(**d1)
349-

0 commit comments

Comments
 (0)