Skip to content

Commit 202552c

Browse files
committed
ENH: py2 => py2/3 migration
1 parent 5077d64 commit 202552c

11 files changed

+72
-72
lines changed

README.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1255,8 +1255,8 @@
12551255
"input": [
12561256
"def func(url):\n",
12571257
" # Basic function that doesn't do any caching\n",
1258-
" import urllib2\n",
1259-
" return urllib2.urlopen(url).read()\n",
1258+
" from six.moves.urllib import request\n",
1259+
" return request.urlopen(url).read()\n",
12601260
"\n",
12611261
"# Either pass it in on instantiation...\n",
12621262
"ind_api = wbpy.IndicatorAPI(fetch=func)\n",

README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -890,8 +890,8 @@ web page as a string.
890890
891891
def func(url):
892892
# Basic function that doesn't do any caching
893-
import urllib2
894-
return urllib2.urlopen(url).read()
893+
from six.moves.urllib import request
894+
return request.urlopen(url).read()
895895
896896
# Either pass it in on instantiation...
897897
ind_api = wbpy.IndicatorAPI(fetch=func)

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pycountry==1.20
22
ddt==1.0.1
33
mock==1.3.0
44
tox==2.3.1
5+
six

wbpy/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from indicators import IndicatorAPI, IndicatorDataset
2-
from climate import ClimateAPI, InstrumentalDataset, ModelledDataset
1+
from wbpy.indicators import IndicatorAPI, IndicatorDataset
2+
from wbpy.climate import ClimateAPI, InstrumentalDataset, ModelledDataset
33

44
__name__ = "wbpy"
55
__version__ = "2.0.1"
@@ -13,4 +13,4 @@
1313
ClimateAPI,
1414
InstrumentalDataset,
1515
ModelledDataset,
16-
]
16+
]

wbpy/climate.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import itertools
77

88
import pycountry
9+
import six
910

1011
from . import utils
1112

@@ -19,7 +20,7 @@ def __init__(self, api_calls, data_type, data_interval, call_date):
1920
responses can form one dataset.
2021
2122
:param data_type:
22-
eg. ``pr``, ``tas``, ``tmin_means``
23+
eg. ``pr``, ``tas``, ``tmin_means``
2324
2425
:param data_interval:
2526
eg. ``mavg``, ``decade``
@@ -212,10 +213,10 @@ def as_dict(self, sres="a2", use_datetime=False):
212213

213214
class ClimateAPI(object):
214215

215-
"""Request data from the World Bank Climate API.
216-
216+
"""Request data from the World Bank Climate API.
217+
217218
You can override the default tempfile cache by passing a function
218-
``fetch``, which requests a URL and returns the response as a string.
219+
``fetch``, which requests a URL and returns the response as a string.
219220
"""
220221

221222
_gcm = dict(
@@ -302,7 +303,7 @@ class ClimateAPI(object):
302303
aanom="annualanom",
303304
aavg="annualavg",
304305
)
305-
for _k, _d_key in _shorthand_codes.iteritems():
306+
for _k, _d_key in _shorthand_codes.items():
306307
for _d in [_instrumental_types, _modelled_types,
307308
_instrumental_intervals, _modelled_intervals]:
308309
if _d_key in _d:

wbpy/indicators.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import re
33
import datetime
44
import pprint
5-
import urllib
5+
from six.moves.urllib.parse import urlencode
66
try:
77
import simplejson as json
88
except ImportError:
@@ -122,13 +122,13 @@ def as_dict(self, use_datetime=False):
122122

123123
class IndicatorAPI(object):
124124

125-
"""Request data from the World Bank Indicators API.
126-
125+
"""Request data from the World Bank Indicators API.
126+
127127
You can override the default tempfile cache by passing a function
128-
``fetch``, which requests a URL and returns the response as a string.
128+
``fetch``, which requests a URL and returns the response as a string.
129129
"""
130130

131-
BASE_URL = "http://api.worldbank.org/"
131+
BASE_URL = "http://api.worldbank.org/v2/"
132132

133133
# The API uses some non-ISO 2-digit and 3-digit codes. Make them available.
134134
NON_STANDARD_REGIONS = utils.NON_STANDARD_REGIONS
@@ -237,7 +237,7 @@ def get_indicators(self, indicator_codes=None, search=None,
237237
def get_countries(self, country_codes=None, search=None,
238238
search_full=False, **kwargs):
239239
"""Request country metadata.
240-
240+
241241
eg. ISO code, coordinates, capital, income level, etc.
242242
243243
:param country_codes:
@@ -433,7 +433,7 @@ def print_codes(self, results, search=None, search_key=None):
433433
:param search:
434434
Regexp string to filter out non-matching results.
435435
By default, this searches the main name of the entity.
436-
436+
437437
:param search_key:
438438
A second-level KEY in your dict, eg. ``{foo: {KEY: val}}``.
439439
If given, will only search the value corresponding to the key.
@@ -465,7 +465,7 @@ def natural_keys(item):
465465
main_value = v.get("name", v.get("value", v))
466466
else:
467467
main_value = v
468-
print u"{0:30} {1}".format(k, main_value)
468+
print(u"{0:30} {1}".format(k, main_value))
469469

470470
def search_results(self, regexp, results, key=None):
471471
"""For a given dict of ``get_`` results, filter out all keys that do
@@ -555,7 +555,7 @@ def _generate_indicators_url(
555555
# always generate the same URL (for caching purposes), so need to
556556
# convert to a sorted list before passing to urlencode().
557557
sorted_kwargs = sorted([(k, v) for k, v in kwargs.items()])
558-
query_string = urllib.urlencode(sorted_kwargs)
558+
query_string = urlencode(sorted_kwargs)
559559

560560
new_url = "".join([self.BASE_URL, rest_url, query_string])
561561
return new_url

wbpy/tests/indicator_data.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ class Yearly(TestData):
137137
"id": "2",
138138
"value": "World Development Indicators"
139139
},
140-
"sourceNote": "Total population is based on the de facto definition of population, which counts all residents regardless of legal status or citizenship--except for refugees not permanently settled in the country of asylum, who are generally considered part of the population of their country of origin. The values shown are midyear estimates.",
141-
"sourceOrganization": "(1) United Nations Population Division. World Population Prospects, (2) United Nations Statistical Division. Population and Vital Statistics Report (various years), (3) Census reports and other statistical publications from national statistical offices, (4) Eurostat: Demographic Statistics, (5) Secretariat of the Pacific Community: Statistics and Demography Programme, and (6) U.S. Census Bureau: International Database.",
140+
"sourceNote": "Total population is based on the de facto definition of population, which counts all residents regardless of legal status or citizenship. The values shown are midyear estimates.",
141+
"sourceOrganization": "(1) United Nations Population Division. World Population Prospects: 2019 Revision. (2) Census reports and other statistical publications from national statistical offices, (3) Eurostat: Demographic Statistics, (4) United Nations Statistical Division. Population and Vital Statistics Reprot (various years), (5) U.S. Census Bureau: International Database, and (6) Secretariat of the Pacific Community: Statistics and Demography Programme.",
142142
"topics": [
143143
{
144144
"id": "8",

wbpy/tests/test_climate.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
from ddt import ddt, data
1111

1212
import wbpy
13-
from climate_data import (
13+
from wbpy.tests.climate_data import (
1414
InstrumentalMonth,
1515
InstrumentalYear,
1616
InstrumentalDecade,
1717
ModelledVarMAVG,
1818
ModelledVarAANOM,
1919
ModelledStat,
20-
)
20+
)
2121

2222
@ddt
2323
class TestClimateDataBasicAttrs(unittest.TestCase):
@@ -76,11 +76,11 @@ def test_api_call_regions_decade(self):
7676
@data(InstrumentalMonth(), InstrumentalYear(), InstrumentalDecade())
7777
def test_api_call_date_attr(self, data):
7878
self.assertEqual(data.dataset.api_call_date, data.date)
79-
79+
8080
@data(InstrumentalMonth(), InstrumentalYear(), InstrumentalDecade())
8181
def test_dates_attr(self, data):
8282
self.assertIn("1901", data.dataset.dates)
83-
83+
8484

8585
@ddt
8686
class TestInstrumentalModelDictFn(unittest.TestCase):
@@ -178,17 +178,17 @@ def test_stat_datetime(self):
178178

179179
@ddt
180180
class TestModelledModelDictFn(unittest.TestCase):
181-
181+
182182
@data(ModelledVarMAVG(), ModelledVarAANOM())
183183
def test_gcm_key(self, data):
184184
self.assertIn("ingv_echam4", data.dataset.as_dict())
185-
185+
186186
def test_gcm_key_ensemble(self):
187187
data = ModelledStat()
188188
self.assertIn("ensemble_90", data.dataset.as_dict())
189-
190-
@data([ModelledVarMAVG(), ("BR",)],
191-
[ModelledVarAANOM(), ("JP",)],
189+
190+
@data([ModelledVarMAVG(), ("BR",)],
191+
[ModelledVarAANOM(), ("JP",)],
192192
[ModelledStat(), ("AU", "NZ")],
193193
)
194194
def test_region_key(self, foo):
@@ -270,8 +270,8 @@ def test_temp_type(self):
270270
locs = ["AF"]
271271
dataset = self.api.get_modelled("tas", "mavg", locs)
272272
self.assertIn("tas", dataset.data_type)
273-
274-
@data("ppt_days", "tmin_means", "ppt_days90th")
273+
274+
@data("ppt_days", "tmin_means", "ppt_days90th")
275275
def test_derived_statistic_type(self, stat):
276276
locs = ["AF", 303]
277277
dataset = self.api.get_modelled(stat, "mavg", locs)
@@ -296,7 +296,7 @@ def test_value(self):
296296
locs = ["nzl"]
297297
dataset = self.api.get_modelled("tmin_means", "mavg", locs)
298298
res = dataset.as_dict(sres="b1")
299-
self.assertEqual(res["ensemble_90"]["NZ"]["2065"][11],
299+
self.assertEqual(res["ensemble_90"]["NZ"]["2065"][11],
300300
14.541015999650355)
301301

302302
def test_bad_request_raises_exc(self):
@@ -312,7 +312,7 @@ def test_multiple_locations(self):
312312
regions = [resp["region"][0] for resp in dataset.api_calls]
313313
self.assertIn("GB", regions)
314314
self.assertIn("302", regions)
315-
315+
316316

317317
class TestLocationCodes(TestClimateAPI):
318318
def test_alpha2_codes_work_as_location_arg(self):

0 commit comments

Comments
 (0)