Skip to content

Instantly share code, notes, and snippets.

@ljmartin
Created October 17, 2020 06:55
Show Gist options
  • Select an option

  • Save ljmartin/89d373fc5f11fab2698db86316bf0b62 to your computer and use it in GitHub Desktop.

Select an option

Save ljmartin/89d373fc5f11fab2698db86316bf0b62 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# increase search space by 2 to find largest success"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 success\n",
"2 success\n",
"4 success\n",
"8 success\n",
"16 success\n",
"32 success\n",
"64 success\n",
"128 success\n",
"256 success\n",
"512 success\n",
"1024 success\n"
]
}
],
"source": [
"import httplib2\n",
"\n",
"base = 'http://zinc.docking.org/catalogs/molport/protomers/subsets/for-sale.db2.gz?page='\n",
"\n",
"i = 1\n",
"h = httplib2.Http()\n",
"resp = h.request(base+str(i), 'HEAD')\n",
"while int(resp[0]['status']) < 400:\n",
" print(i, 'success')\n",
" i*=2\n",
" resp = h.request(base+str(i), 'HEAD')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# iteratively divide search space by 2 "
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"min_success = i//2\n",
"min_failure = i\n",
"j = ( min_failure - min_success ) //2"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"512 1536\n",
"256 1536\n",
"128 1536\n",
"64 1536\n",
"32 1504\n",
"16 1504\n",
"8 1496\n",
"4 1496\n",
"2 1496\n",
"1 1495\n"
]
}
],
"source": [
"while j>1:\n",
" #recalculate j:\n",
" j = ( min_failure - min_success ) //2\n",
" #ping URL:\n",
" resp = h.request(base+str(j+min_success), 'HEAD')\n",
" \n",
" if int(resp[0]['status'])<400:\n",
" #success! Increase min_success\n",
" min_success = j + min_success \n",
" else:\n",
" #failed. Reduce min_failure\n",
" min_failure = j + min_success\n",
" \n",
" print(j, j+min_success)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1494"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"min_success"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1495"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"min_failure"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment