Skip to content

Commit

Permalink
Chasing Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbolster authored Feb 21, 2024
1 parent 8601c6a commit 70eae4d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
49 changes: 48 additions & 1 deletion notebooks/test_niwater.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2185,9 +2185,56 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"id": "0bdb2d78",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "HTTPError.__init__() missing 3 required positional arguments: 'msg', 'hdrs', and 'fp'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[14], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01murllib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01merror\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m HTTPError\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28misinstance\u001b[39m(\u001b[43mHTTPError\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m501\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mheh\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m, \u001b[38;5;167;01mBaseException\u001b[39;00m)\n",
"\u001b[0;31mTypeError\u001b[0m: HTTPError.__init__() missing 3 required positional arguments: 'msg', 'hdrs', and 'fp'"
]
}
],
"source": [
"from urllib.error import HTTPError\n",
"\n",
"isinstance(HTTPError), BaseException)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "f2d1ee31",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Boo!\n"
]
}
],
"source": [
"_error_ = BaseException\n",
"\n",
"try: \n",
" raise ValueError(\"Boo!\")\n",
"except _error_ as e:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5481275f",
"metadata": {},
"outputs": [],
"source": []
}
Expand Down
7 changes: 6 additions & 1 deletion src/bolster/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from itertools import chain, islice, groupby
from operator import itemgetter
from pathlib import Path
from urllib.error import HTTPError, URLError
from typing import (
Sequence,
Generator,
Expand All @@ -38,6 +39,7 @@
Set,
Hashable,
Any,
TypeAlias,
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -172,7 +174,7 @@ def wrapper(*args, **kwargs):

# noinspection PyShadowingNames
def backoff(
exception_to_check: Union[BaseException, Sequence[BaseException]],
exception_to_check: Union[Any, Sequence[Any]] = BaseException,
tries: SupportsInt = 5,
delay: SupportsFloat = 0.2,
backoff: SupportsFloat = 2,
Expand All @@ -183,6 +185,9 @@ def backoff(
http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
Can't Type-Annotate Exceptions because
[it's verboten](https://peps.python.org/pep-0484/#exceptions)
Args:
exception_to_check: the exception to check. may be a tuple of
exceptions to check
Expand Down
2 changes: 1 addition & 1 deletion src/bolster/data_sources/ni_water.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_postcode_to_water_supply_zone() -> Dict[str, str]:


# This may throw 503's on occasion which annoyingly makes it stocastic
@backoff((HTTPError, ConnectionError))
@backoff(HTTPError)
def get_water_quality_by_zone(zone_code: str, strict=False) -> pd.Series:
"""
Get the latest Water Quality for a given Water Supply Zone
Expand Down

0 comments on commit 70eae4d

Please sign in to comment.