PubChemPy
PubChemPy copied to clipboard
Getting synonyms for pubchem id with no synonyms crashes.
Here is some test code:-
import pubchempy
import json
pubchem_id = '171839766' # '102200539'
description = pubchempy.request(pubchem_id, operation='description')
print(description)
raw_data = description.read()
encoding = description.info().get_content_charset('utf8') # JSON default
description_data = json.loads(raw_data.decode(encoding))
print(description_data)
string_to_add_for_description = ""
for description_item in description_data['InformationList']['Information']:
if 'Description' in description_item.keys() and 'DescriptionSourceName' in description_item.keys():
formatted_string = '{}: {}'.format(description_item['DescriptionSourceName'],
description_item['Description'])
string_to_add_for_description += formatted_string
elif 'Title' in description_item.keys():
name = description_item['Title']
print(name)
print(string_to_add_for_description)
syn = pubchempy.request(pubchem_id, operation='synonyms')
raw_data = syn.read()
encoding = syn.info().get_content_charset('utf8') # JSON default
syn_data = json.loads(raw_data.decode(encoding))
print(syn_data)
results = pubchempy.Compound.from_cid(pubchem_id)
inchikey = results.to_dict(properties=['inchikey'])
if 'inchikey' in inchikey:
print(inchikey['inchikey'])
which when ran produces:-
python pubchem_api_test.py
<http.client.HTTPResponse object at 0x1029e50c0> {'InformationList': {'Information': [{'CID': 171839766, 'Title': '2-methyl-7-(2-methyloctan-2-yl)-2H-chromen-5-ol'}]}} 2-methyl-7-(2-methyloctan-2-yl)-2H-chromen-5-ol
Traceback (most recent call last): File "/usr/dir/harv-py-3.11/lib/python3.11/site-packages/pubchempy.py", line 271, in request response = urlopen(apiurl, postdata) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/Cellar/[email protected]/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 216, in urlopen return opener.open(url, data, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/Cellar/[email protected]/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 525, in open response = meth(req, response) ^^^^^^^^^^^^^^^^^^^ File "/usr/local/Cellar/[email protected]/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 634, in http_response response = self.parent.error( ^^^^^^^^^^^^^^^^^^ File "/usr/local/Cellar/[email protected]/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 563, in error return self._call_chain(*args) ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/Cellar/[email protected]/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 496, in _call_chain result = func(*args) ^^^^^^^^^^^ File "/usr/local/Cellar/[email protected]/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/urllib/request.py", line 643, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 404: PUGREST.NotFound
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/dir/pubchem_api_test.py", line 23, in
Would it be possible to put a try/except in the code and return the equivalent of an empty list.
Thanks.