Skip to content

Commit

Permalink
Change requests to httpx
Browse files Browse the repository at this point in the history
  • Loading branch information
maximusunc committed Aug 10, 2023
1 parent 44e9599 commit 4ea8a67
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
71 changes: 36 additions & 35 deletions app/novelty/extr_smile_molpro_by_id.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import requests
import httpx


def mol_to_smile_molpro(molecules):
Expand All @@ -18,42 +18,43 @@ def mol_to_smile_molpro(molecules):

data_mol = list(set(molecules))
# print(f'init data: {len(data_mol)}')
while data_mol:
# print(f'before: {len(data_mol)}')
data_mol_before = len(data_mol)
response = requests.post(url, headers=headers, json=data_mol)

if response.status_code == 200:
json_response = response.json()
collec_url = json_response["url"]
temp_collec_response = requests.get(collec_url)
if temp_collec_response.status_code == 200:
collec_response = temp_collec_response.json()

for i in range(json_response["size"]):
key_list = ["identifiers"]
if set(key_list).issubset(collec_response["elements"][i].keys()):
identifiers = collec_response["elements"][i]["identifiers"]
smile = identifiers.get("smiles", "No SMILES could be found")
smiles[data_mol[i]] = smile
else:
smiles[data_mol[i]] = "No identifiers could be found"

# Remove molecules with successfully retrieved smiles from data_mol
data_mol = [mol for mol in data_mol if mol not in smiles]
data_mol_after = len(data_mol)
# print(f'after: {len(data_mol)}')

if data_mol_after == data_mol_before:
with httpx.Client(timeout=30) as client:
while data_mol:
# print(f'before: {len(data_mol)}')
data_mol_before = len(data_mol)
response = client.post(url, headers=headers, json=data_mol)

if response.status_code == 200:
json_response = response.json()
collec_url = json_response["url"]
temp_collec_response = client.get(collec_url)
if temp_collec_response.status_code == 200:
collec_response = temp_collec_response.json()

for i in range(json_response["size"]):
key_list = ["identifiers"]
if set(key_list).issubset(collec_response["elements"][i].keys()):
identifiers = collec_response["elements"][i]["identifiers"]
smile = identifiers.get("smiles", "No SMILES could be found")
smiles[data_mol[i]] = smile
else:
smiles[data_mol[i]] = "No identifiers could be found"

# Remove molecules with successfully retrieved smiles from data_mol
data_mol = [mol for mol in data_mol if mol not in smiles]
data_mol_after = len(data_mol)
# print(f'after: {len(data_mol)}')

if data_mol_after == data_mol_before:
break

else:
print(
f"Error: {temp_collec_response.status_code} - {temp_collec_response.text}"
)
break

else:
print(
f"Error: {temp_collec_response.status_code} - {temp_collec_response.text}"
)
print(f"Error: {response.status_code} - {response.text}")
break
else:
print(f"Error: {response.status_code} - {response.text}")
break

return smiles
2 changes: 1 addition & 1 deletion app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

openapi_args = dict(
title="Answer Appraiser",
version="0.3.0",
version="0.3.1",
terms_of_service="",
translator_component="Utility",
translator_teams=["Standards Reference Implementation Team"],
Expand Down

0 comments on commit 4ea8a67

Please sign in to comment.