Skip to content

Commit

Permalink
Replace urllib with the requests package
Browse files Browse the repository at this point in the history
  • Loading branch information
martinholmer committed Feb 8, 2019
1 parent 03eb645 commit 8edbd7d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ requirements:
- "numpy>=1.13"
- "pandas>=0.23"
- "bokeh>=0.13"
- requests
- numba

run:
- python
- "numpy>=1.13"
- "pandas>=0.23"
- "bokeh>=0.13"
- requests
- numba

test:
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies:
- "numpy>=1.13"
- "pandas>=0.23"
- "bokeh>=0.13"
- requests
- numba
- pytest
- pytest-pep8
Expand Down
14 changes: 10 additions & 4 deletions taxcalc/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import os
import re
import copy
import urllib
import requests
import numpy as np
import pandas as pd
from taxcalc.calcfunctions import (TaxInc, SchXYZTax, GainsTax, AGIsurtax,
Expand Down Expand Up @@ -1102,7 +1102,9 @@ def read_json_param_objects(reform, assump):
if os.path.isfile(assump):
txt = open(assump, 'r').read()
elif assump.startswith('http'):
txt = urllib.request.urlopen(assump).read().decode()
req = requests.get(assump)
req.raise_for_status()
txt = req.text
else:
txt = assump
(cons_dict,
Expand All @@ -1117,7 +1119,9 @@ def read_json_param_objects(reform, assump):
if os.path.isfile(reform):
txt = open(reform, 'r').read()
elif reform.startswith('http'):
txt = urllib.request.urlopen(reform).read().decode()
req = requests.get(reform)
req.raise_for_status()
txt = req.text
else:
txt = reform
rpol_dict = (
Expand Down Expand Up @@ -1159,7 +1163,9 @@ def read_json_assumptions(assump):
if os.path.isfile(assump):
txt = open(assump, 'r').read()
elif assump.startswith('http'):
txt = urllib.request.urlopen(assump).read().decode()
req = requests.get(assump)
req.raise_for_status()
txt = req.text
else:
txt = assump
# strip out //-comments without changing line numbers
Expand Down
4 changes: 2 additions & 2 deletions taxcalc/tests/test_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from io import StringIO
import tempfile
import copy
import urllib
import pytest
import requests
import numpy as np
import pandas as pd
from taxcalc import Policy, Records, Calculator, Consumption
Expand Down Expand Up @@ -1213,7 +1213,7 @@ def test_read_json_assumptions(assumption_file):
assert isinstance(Calculator.read_json_assumptions(None), dict)
with pytest.raises(ValueError):
Calculator.read_json_assumptions(list())
with pytest.raises(urllib.request.URLError):
with pytest.raises(requests.exceptions.ConnectionError):
Calculator.read_json_assumptions('http://unknown-url')
assump_filename = assumption_file.name
file_dict = Calculator.read_json_assumptions(assump_filename)
Expand Down

0 comments on commit 8edbd7d

Please sign in to comment.